Skip to content

add dagger #5

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 1 commit into
base: mvvm
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
8 changes: 7 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
compileSdk 31
buildFeatures {
viewBinding true
viewBinding true
}
defaultConfig {
applicationId "com.itis.templateitis"
Expand Down Expand Up @@ -65,10 +66,15 @@ dependencies {
debugImplementation "com.squareup.okhttp3:logging-interceptor:${okhttp}"
// endregion


testImplementation 'junit:junit:4.13.2'
implementation 'com.google.android.gms:play-services-location:17.0.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

def dagger = "2.41"
implementation "com.google.dagger:dagger:${dagger}"
kapt "com.google.dagger:dagger-compiler:${dagger}"
}

detekt {
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 @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/com/example/web_app/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.web_app

import android.app.Application
import com.example.web_app.di.AppComponent
import com.example.web_app.di.DaggerAppComponent
import com.example.web_app.di.module.AppModule
import com.example.web_app.di.module.NetModule

class App:Application() {
lateinit var appComponent: AppComponent

override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent
.builder()
.appModule(AppModule())
.netModule(NetModule())
.build()
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/web_app/AppViewModelFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.web_app

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

import javax.inject.Inject
import javax.inject.Provider

class AppViewModelFactory @Inject constructor(
private val viewModelMap: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
val result = viewModelMap[modelClass] ?: viewModelMap.entries.firstOrNull {
modelClass.isAssignableFrom(it.key)
}?.value ?: throw IllegalArgumentException("Unknown model class $modelClass")
@Suppress("UNCHECKED_CAST")
return result.get() as T
}
}
24 changes: 0 additions & 24 deletions app/src/main/java/com/example/web_app/ViewModelFactory.kt

This file was deleted.

35 changes: 13 additions & 22 deletions app/src/main/java/com/example/web_app/data/WeatherRepositoryImpl.kt
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
package com.example.web_app.data

import androidx.viewbinding.BuildConfig
import com.example.web_app.data.api.Api
import com.example.web_app.data.api.mapper.WeatherMapper
import com.example.web_app.data.response.WeatherList
import com.example.web_app.data.response.WeatherResponse
import com.example.web_app.domain.WeatherRepository
import com.example.web_app.domain.entity.Weather
import okhttp3.HttpUrl
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
private const val QUERY_UNITS = "units"
private const val UNITS = "metric"
private const val BASE_URL = "https://api.openweathermap.org/data/2.5/"
private const val API_KEY = "9e8331105a420290e0619d9769c5dc65"
private const val QUERY_API_KEY = "appid"

class
WeatherRepositoryImpl(
private val api:Api,
private val mapper: WeatherMapper
):WeatherRepository {
import javax.inject.Inject

class WeatherRepositoryImpl @Inject constructor(
private val api: Api,
private val mapper: WeatherMapper
) : WeatherRepository {


override suspend fun getWeatherByCity(city: String): Weather {
return mapper.mapToWeather(api.getWeatherByCity(city))
}

override suspend fun getWeatherById(id: Int): Weather {
return mapper.mapToWeather(api.getWeatherById(id))
}
override suspend fun getWeatherList(latitude: Double?, longitude: Double?, count:Int): List<Weather> {
return mapper.mapToWeatherList(api.getWeatherList(latitude,longitude,count))

override suspend fun getWeatherList(
latitude: Double?,
longitude: Double?,
count: Int
): List<Weather> {
return mapper.mapToWeatherList(api.getWeatherList(latitude, longitude, count))
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/com/example/web_app/di/AppComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.web_app.di

import com.example.web_app.di.module.AppModule
import com.example.web_app.di.module.NetModule
import com.example.web_app.di.module.RepositoryModule
import com.example.web_app.di.module.ViewModelModule
import com.example.web_app.presentation.ui.MainActivity
import com.example.web_app.presentation.ui.fragment.DetailFragment
import com.example.web_app.presentation.ui.fragment.SearchFragment
import dagger.Component

@Component(
modules = [
AppModule::class,
NetModule::class,
RepositoryModule::class,
ViewModelModule::class
]
)
interface AppComponent {

fun inject(mainActivity: MainActivity)
fun inject(searchFragment: SearchFragment)
fun inject(detailFragment: DetailFragment)
}
90 changes: 0 additions & 90 deletions app/src/main/java/com/example/web_app/di/DIContainer.kt

This file was deleted.

14 changes: 14 additions & 0 deletions app/src/main/java/com/example/web_app/di/ViewModelKey.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.web_app.di

import androidx.lifecycle.ViewModel
import dagger.MapKey
import kotlin.reflect.KClass

@Target(
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER
)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)
16 changes: 16 additions & 0 deletions app/src/main/java/com/example/web_app/di/module/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.web_app.di.module

import com.example.web_app.data.api.mapper.WeatherMapper
import dagger.Module
import dagger.Provides
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

@Module
class AppModule {
@Provides
fun provideWeatherMapper(): WeatherMapper = WeatherMapper()

@Provides
fun provideDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default
}
Loading