Skip to content

Hw1 weather app #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ plugins {
}

android {
compileSdk 31
compileSdk 32

defaultConfig {
applicationId "ru.itis.karakurik.androidLab2"
minSdk 23
targetSdk 31
targetSdk 32
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
viewBinding true
viewBinding = true
}

buildTypes {
Expand All @@ -46,10 +46,18 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"

//Timber
implementation 'com.jakewharton.timber:timber:5.0.1'

//Coil
implementation "io.coil-kt:coil:1.1.1"

def nav_version = "2.4.1"
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
implementation "androidx.navigation:navigation-fragment:2.4.1"
implementation "androidx.navigation:navigation-ui:2.4.1"


implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.itis.karakurik.androidLab2" >

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -10,7 +12,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.Androidlab2" >
<activity
android:name=".MainActivity"
android:name=".ui.activities.MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
12 changes: 0 additions & 12 deletions app/src/main/java/ru/itis/karakurik/androidLab2/MainActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ru.itis.karakurik.androidLab2.data

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import ru.itis.karakurik.androidLab2.BuildConfig
import ru.itis.karakurik.androidLab2.data.api.Api
import ru.itis.karakurik.androidLab2.data.api.interceptors.ApiKeyInterceptor
import ru.itis.karakurik.androidLab2.data.api.interceptors.LangInterceptor
import ru.itis.karakurik.androidLab2.data.api.interceptors.UnitsInterceptor
import ru.itis.karakurik.androidLab2.data.api.response.citiesResponse.City
import ru.itis.karakurik.androidLab2.data.api.response.weatherResponse.WeatherResponse
import ru.itis.karakurik.androidLab2.models.CityWeather
import ru.itis.karakurik.androidLab2.models.convertors.WindDegConvertor

private const val BASE_URL = "https://api.openweathermap.org/data/2.5/"

class WeatherRepository {
private val okhttp: OkHttpClient by lazy {
OkHttpClient.Builder()
.addInterceptor(ApiKeyInterceptor())
.addInterceptor(UnitsInterceptor())
.addInterceptor(LangInterceptor())
.also {
if (BuildConfig.DEBUG) {
it.addInterceptor(
HttpLoggingInterceptor()
.setLevel(
HttpLoggingInterceptor.Level.BODY
)
)
}
}
.build()
}

private val api: Api by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okhttp)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(Api::class.java)
}

suspend fun getWeather(city: String): CityWeather {
return getCityWeather(api.getWeather(city))
}

private fun getCityWeather(weatherResponse: WeatherResponse) : CityWeather {
weatherResponse.run {
return CityWeather(
id,
name,
coord.lat,
coord.lon,
main.temp,
main.humidity,
WindDegConvertor.convertWindDeg(wind.deg)
)
}
}

private fun getCityWeather(city: City) : CityWeather {
city.run {
return CityWeather(
id,
name,
coord.lat,
coord.lon,
main.temp,
main.humidity,
WindDegConvertor.convertWindDeg(wind.deg)
)
}
}

suspend fun getCities(lat: Double, lon: Double, cnt: Int): MutableList<CityWeather> {
val citiesResponse = api.getWeatherCities(lat, lon, cnt)
val list = ArrayList<CityWeather>(cnt)
for(i in 0 until cnt) {
list.add(getCityWeather(citiesResponse.list[i]))
}
return list
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/ru/itis/karakurik/androidLab2/data/api/Api.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.itis.karakurik.androidLab2.data.api

import retrofit2.http.GET
import retrofit2.http.Query
import ru.itis.karakurik.androidLab2.data.api.response.citiesResponse.CitiesResponse
import ru.itis.karakurik.androidLab2.data.api.response.weatherResponse.WeatherResponse

interface Api {
@GET("weather")
suspend fun getWeather(@Query("q") city: String): WeatherResponse

@GET("find")
suspend fun getWeatherCities(
@Query("lat") lat: Double,
@Query("lon") lon: Double,
@Query("cnt") cnt: Int
) : CitiesResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.itis.karakurik.androidLab2.data.api.interceptors

import okhttp3.Interceptor
import okhttp3.Response

private const val API_KEY = "56fc6c6cb76c0864b4cd055080568268"
private const val QUERY_API_KEY = "appid"

class ApiKeyInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val newUrl = request.url.newBuilder()
.addQueryParameter(QUERY_API_KEY, API_KEY)
.build()

return chain.proceed(
request.newBuilder()
.url(newUrl)
.build()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.itis.karakurik.androidLab2.data.api.interceptors

import okhttp3.Interceptor
import okhttp3.Response

private const val QUERY_LANG = "lang"
private const val LANG = "RU"

class LangInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val newUrl = request.url.newBuilder()
.addQueryParameter(QUERY_LANG, LANG)
.build()

return chain.proceed(
request.newBuilder()
.url(newUrl)
.build()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.itis.karakurik.androidLab2.data.api.interceptors

import okhttp3.Interceptor
import okhttp3.Response

private const val QUERY_UNITS = "units"
private const val UNITS = "metric"

class UnitsInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val newUrl = request.url.newBuilder()
.addQueryParameter(QUERY_UNITS, UNITS)
.build()

return chain.proceed(
request.newBuilder()
.url(newUrl)
.build()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.itis.karakurik.androidLab2.data.api.response.citiesResponse


import com.google.gson.annotations.SerializedName

data class CitiesResponse(
@SerializedName("cod")
val cod: String,
@SerializedName("count")
val count: Int,
@SerializedName("list")
val list: List<City>,
@SerializedName("message")
val message: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.itis.karakurik.androidLab2.data.api.response.citiesResponse


import com.google.gson.annotations.SerializedName

data class City (
@SerializedName("clouds")
val clouds: Clouds,
@SerializedName("coord")
val coord: Coord,
@SerializedName("dt")
val dt: Int,
@SerializedName("id")
val id: Int,
@SerializedName("main")
val main: Main,
@SerializedName("name")
val name: String,
@SerializedName("rain")
val rain: Any,
@SerializedName("snow")
val snow: Any,
@SerializedName("sys")
val sys: Sys,
@SerializedName("weather")
val weather: List<Weather>,
@SerializedName("wind")
val wind: Wind
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.itis.karakurik.androidLab2.data.api.response.citiesResponse


import com.google.gson.annotations.SerializedName

data class Clouds(
@SerializedName("all")
val all: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.itis.karakurik.androidLab2.data.api.response.citiesResponse


import com.google.gson.annotations.SerializedName

data class Coord(
@SerializedName("lat")
val lat: Double,
@SerializedName("lon")
val lon: Double
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.itis.karakurik.androidLab2.data.api.response.citiesResponse


import com.google.gson.annotations.SerializedName

data class Main(
@SerializedName("feels_like")
val feelsLike: Double,
@SerializedName("grnd_level")
val grndLevel: Int,
@SerializedName("humidity")
val humidity: Int,
@SerializedName("pressure")
val pressure: Int,
@SerializedName("sea_level")
val seaLevel: Int,
@SerializedName("temp")
val temp: Double,
@SerializedName("temp_max")
val tempMax: Double,
@SerializedName("temp_min")
val tempMin: Double
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.itis.karakurik.androidLab2.data.api.response.citiesResponse


import com.google.gson.annotations.SerializedName

data class Sys(
@SerializedName("country")
val country: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.itis.karakurik.androidLab2.data.api.response.citiesResponse


import com.google.gson.annotations.SerializedName

data class Weather(
@SerializedName("description")
val description: String,
@SerializedName("icon")
val icon: String,
@SerializedName("id")
val id: Int,
@SerializedName("main")
val main: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.itis.karakurik.androidLab2.data.api.response.citiesResponse


import com.google.gson.annotations.SerializedName

data class Wind(
@SerializedName("deg")
val deg: Int,
@SerializedName("speed")
val speed: Double
)
Loading