Skip to content

Hw3 weather app mvvm #3

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 3 commits into
base: hw2-weather-app
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
2 changes: 1 addition & 1 deletion .idea/misc.xml

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

1 change: 0 additions & 1 deletion .idea/vcs.xml

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

30 changes: 21 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ android {
}

buildFeatures {
viewBinding = true
viewBinding true
dataBinding true
}


buildTypes {
release {
minifyEnabled false
Expand All @@ -40,6 +42,7 @@ android {

dependencies {
implementation 'com.google.android.gms:play-services-location:19.0.1'

def room_version = "2.4.2"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
Expand All @@ -48,7 +51,11 @@ 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"

def lifecycle = "2.4.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle"

//SwipeRefreshLayout
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
Expand All @@ -59,23 +66,28 @@ dependencies {
//Coil
implementation "io.coil-kt:coil:1.1.1"

def viewBinding = "1.5.3"
implementation "com.github.kirich1409:viewbindingpropertydelegate:$viewBinding"
// To use only without reflection variants of viewBinding
implementation "com.github.kirich1409:viewbindingpropertydelegate-noreflection:$viewBinding"

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


implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0"
def coroutines = "1.6.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"

// region Network
def retrofit = "2.9.0"
implementation "com.squareup.retrofit2:retrofit:${retrofit}"
implementation "com.squareup.retrofit2:converter-gson:${retrofit}"
implementation "com.squareup.retrofit2:retrofit:$retrofit"
implementation "com.squareup.retrofit2:converter-gson:$retrofit"

def okhttp = "4.9.3"
implementation "com.squareup.okhttp3:okhttp:${okhttp}"
debugImplementation "com.squareup.okhttp3:logging-interceptor:${okhttp}"
implementation "com.squareup.okhttp3:okhttp:$okhttp"
debugImplementation "com.squareup.okhttp3:logging-interceptor:$okhttp"
// endregion

testImplementation 'junit:junit:4.13.2'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.INTERNET" />

<application
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package ru.itis.karakurik.androidLab2.data.api.mapper

class WeatherIconUrlMapper {
fun map(iconId: String): String {
fun mapToLargeIcon(iconId: String): String {
return "http://openweathermap.org/img/wn/${iconId}@2x.png"
}

fun mapToSmallIcon(iconId: String): String {
return "http://openweathermap.org/img/wn/${iconId}.png"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class WeatherMapper(
windDeg = windDegMapper.map(weatherResponse.wind.deg),
windSpeed = weatherResponse.wind.speed,
pressure = weatherResponse.main.pressure,
iconUrl = weatherIconUrlMapper.map(weatherResponse.weather[0].icon)
iconUrl = weatherIconUrlMapper.mapToLargeIcon(weatherResponse.weather[0].icon)
)

fun map(city: City) : Weather = Weather(
Expand All @@ -35,6 +35,6 @@ class WeatherMapper(
windDeg = windDegMapper.map(city.wind.deg),
windSpeed = city.wind.speed,
pressure = city.main.pressure,
iconUrl = weatherIconUrlMapper.map(city.weather[0].icon)
iconUrl = weatherIconUrlMapper.mapToLargeIcon(city.weather[0].icon)
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.itis.karakurik.androidLab2.di

import kotlinx.coroutines.Dispatchers
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
Expand All @@ -14,6 +15,8 @@ import ru.itis.karakurik.androidLab2.di.interceptors.LangInterceptor
import ru.itis.karakurik.androidLab2.di.interceptors.UnitsInterceptor
import ru.itis.karakurik.androidLab2.domain.repository.WeatherRepository
import ru.itis.karakurik.androidLab2.domain.repository.WeatherRepositoryImpl
import ru.itis.karakurik.androidLab2.domain.usecase.GetWeatherListUseCase
import ru.itis.karakurik.androidLab2.domain.usecase.GetWeatherUseCase

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

Expand Down Expand Up @@ -53,7 +56,7 @@ object DiContainer {
WeatherIconUrlMapper()
}

val weatherMapper: WeatherMapper by lazy {
private val weatherMapper: WeatherMapper by lazy {
WeatherMapper(
windDegMapper,
weatherIconUrlMapper
Expand All @@ -66,4 +69,12 @@ object DiContainer {
weatherMapper
)
}

val getWeatherUseCase: GetWeatherUseCase by lazy {
GetWeatherUseCase(weatherRepository, Dispatchers.Default)
}

val getWeatherListUseCase: GetWeatherListUseCase by lazy {
GetWeatherListUseCase(weatherRepository, Dispatchers.Default)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.itis.karakurik.androidLab2.domain.repository

import ru.itis.karakurik.androidLab2.BuildConfig
import ru.itis.karakurik.androidLab2.data.api.Api
import ru.itis.karakurik.androidLab2.data.api.mapper.WeatherMapper
import ru.itis.karakurik.androidLab2.domain.entity.Weather
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.coroutines.withContext
import ru.itis.karakurik.androidLab2.domain.entity.Weather
import ru.itis.karakurik.androidLab2.domain.repository.WeatherRepository

class GetWeathersUseCase(
class GetWeatherListUseCase(
private val weatherRepository: WeatherRepository,
private val dispatcher: CoroutineDispatcher = Dispatchers.Main
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package ru.itis.karakurik.androidLab2.extentions
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.recyclerview.widget.LinearSmoothScroller

import androidx.recyclerview.widget.RecyclerView

fun AppCompatActivity.findController (id: Int) : NavController {
return (supportFragmentManager.findFragmentById(id) as NavHostFragment).navController
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ru.itis.karakurik.androidLab2.presentation

import Event
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import ru.itis.karakurik.androidLab2.domain.entity.Weather
import ru.itis.karakurik.androidLab2.domain.usecase.GetWeatherListUseCase
import ru.itis.karakurik.androidLab2.domain.usecase.GetWeatherUseCase

class MainViewModel(
private val getWeatherUseCase: GetWeatherUseCase,
private val getWeatherListUseCase: GetWeatherListUseCase
) : ViewModel() {

private val _weather: MutableLiveData<Result<Weather>> = MutableLiveData()
val weather: LiveData<Result<Weather>> = _weather

private val _weatherList: MutableLiveData<Result<List<Weather>>> = MutableLiveData()
val weatherList: LiveData<Result<List<Weather>>> = _weatherList

private val _cityId: MutableLiveData<Event<Result<Int>>> = MutableLiveData()
val cityId: LiveData<Event<Result<Int>>> = _cityId

fun onGetWeather(cityId : Int) {
viewModelScope.launch {
kotlin.runCatching {
getWeatherUseCase(cityId)
}.onSuccess {
_weather.value = Result.success(it)
}.onFailure {
_weather.value = Result.failure(it)
}
}
}

fun onGetWeatherList(lat: Double, lon: Double, cnt: Int) {
viewModelScope.launch {
kotlin.runCatching {
getWeatherListUseCase(lat, lon, cnt)
}.onSuccess {
_weatherList.value = Result.success(it)
}.onFailure {
_weatherList.value = Result.failure(it)
}
}
}

fun onGetCityId(cityName: String) {
viewModelScope.launch {
kotlin.runCatching {
getWeatherUseCase(cityName).id
}.onSuccess {
_cityId.value = Event(Result.success(it))
}.onFailure {
_cityId.value = Event(Result.failure(it))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ package ru.itis.karakurik.androidLab2.presentation.activities
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import by.kirich1409.viewbindingdelegate.viewBinding
import ru.itis.karakurik.androidLab2.R
import ru.itis.karakurik.androidLab2.databinding.ActivityMainBinding
import ru.itis.karakurik.androidLab2.extentions.findController

class MainActivity : AppCompatActivity() {
private var binding: ActivityMainBinding? = null
class MainActivity : AppCompatActivity(R.layout.activity_main) {
private val binding by viewBinding(ActivityMainBinding::bind)
private var controller: NavController? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater).also {
setContentView(it.root)
}
controller = binding?.navHostFragmentMain?.id?.let { findController(it) }
controller = binding.navHostFragmentMain.id.let { findController(it) }
}

override fun onDestroy() {
super.onDestroy()
binding = null
controller = null
}
}
Loading