Skip to content

add hilt #6

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: dagger
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
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}

android {
Expand Down Expand Up @@ -75,6 +76,8 @@ dependencies {
def dagger = "2.41"
implementation "com.google.dagger:dagger:${dagger}"
kapt "com.google.dagger:dagger-compiler:${dagger}"
implementation("com.google.dagger:hilt-android:${dagger}")
kapt("com.google.dagger:hilt-android-compiler:${dagger}")
}

detekt {
Expand Down
19 changes: 3 additions & 16 deletions app/src/main/java/com/example/web_app/App.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
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
import dagger.hilt.android.HiltAndroidApp

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

override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent
.builder()
.appModule(AppModule())
.netModule(NetModule())
.build()
}
}
@HiltAndroidApp
class App : Application()
19 changes: 0 additions & 19 deletions app/src/main/java/com/example/web_app/AppViewModelFactory.kt

This file was deleted.

25 changes: 0 additions & 25 deletions app/src/main/java/com/example/web_app/di/AppComponent.kt

This file was deleted.

14 changes: 0 additions & 14 deletions app/src/main/java/com/example/web_app/di/ViewModelKey.kt

This file was deleted.

3 changes: 3 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
Expand Up @@ -3,10 +3,13 @@ package com.example.web_app.di.module
import com.example.web_app.data.api.mapper.WeatherMapper
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

@Module
@InstallIn(SingletonComponent::class)
class AppModule {
@Provides
fun provideWeatherMapper(): WeatherMapper = WeatherMapper()
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/example/web_app/di/module/NetModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.example.web_app.BuildConfig
import com.example.web_app.data.api.Api
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand All @@ -18,6 +20,7 @@ private const val API_KEY = "9e8331105a420290e0619d9769c5dc65"
private const val QUERY_API_KEY = "appid"

@Module
@InstallIn(SingletonComponent::class)
class NetModule{
@Provides
@Named("apiKey")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import com.example.web_app.data.WeatherRepositoryImpl
import com.example.web_app.domain.WeatherRepository
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

@Module
@InstallIn(SingletonComponent::class)
interface RepositoryModule {

@Binds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import com.example.web_app.domain.usecase.GetWeatherByIdUseCase
import com.example.web_app.domain.usecase.GetWeatherListUseCase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class UseCaseModule {
@Provides
@Singleton
Expand Down
34 changes: 0 additions & 34 deletions app/src/main/java/com/example/web_app/di/module/ViewModelModule.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.example.web_app.App
import com.example.web_app.R
import com.example.web_app.di.AppComponent
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
lateinit var appComponent: AppComponent

private lateinit var controller: NavController
override fun onCreate(savedInstanceState: Bundle?) {
appComponent = (application as App).appComponent
appComponent.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
controller =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.example.web_app.R
import com.example.web_app.AppViewModelFactory
import com.example.web_app.databinding.FragmentDetailBinding
import com.example.web_app.domain.entity.Weather
import com.example.web_app.presentation.ui.MainActivity
import com.example.web_app.presentation.viewModel.DetailFragmentViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import javax.inject.Inject

@AndroidEntryPoint
class DetailFragment : Fragment(R.layout.fragment_detail) {
@Inject
lateinit var factoryApp: AppViewModelFactory


var binding: FragmentDetailBinding? = null
private val viewModel: DetailFragmentViewModel by viewModels { factoryApp }
private val viewModel: DetailFragmentViewModel by viewModels()

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -41,10 +40,6 @@ class DetailFragment : Fragment(R.layout.fragment_detail) {
id?.let { getWeather(it) }
}

override fun onCreate(savedInstanceState: Bundle?) {
(activity as MainActivity).appComponent.inject(this)
super.onCreate(savedInstanceState)
}
private fun getWeather(id: Int) {
lifecycleScope.launch {
viewModel.getWeatherById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.web_app.R
import com.example.web_app.AppViewModelFactory
import com.example.web_app.databinding.FragmentSearchBinding
import com.example.web_app.presentation.ui.MainActivity
import com.example.web_app.presentation.ui.adapter.WeatherAdapter
import com.example.web_app.presentation.viewModel.SearchFragmentViewModel
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class SearchFragment : Fragment(R.layout.fragment_search) {
private val CONST_LONGITUDE = 10.34
private val CONST_LATITUDE = 12.35
Expand All @@ -36,17 +36,11 @@ class SearchFragment : Fragment(R.layout.fragment_search) {
private var changingLatitude: Double = CONST_LATITUDE
private lateinit var locationClient: FusedLocationProviderClient

@Inject
lateinit var factoryApp: AppViewModelFactory
private val viewModel: SearchFragmentViewModel by viewModels {
factoryApp
}
private val viewModel: SearchFragmentViewModel by viewModels()

private var mRecyclerView: RecyclerView? = null
private var weatherAdapter: WeatherAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
(activity as MainActivity).appComponent.inject(this)
super.onCreate(savedInstanceState)
}

var binding: FragmentSearchBinding? = null;
override fun onCreateView(
inflater: LayoutInflater,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.web_app.domain.entity.Weather
import com.example.web_app.domain.usecase.GetWeatherByIdUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DetailFragmentViewModel @Inject constructor(
private val getWeatherByIdUseCase: GetWeatherByIdUseCase
):ViewModel() {
) : ViewModel() {
private var _weather: MutableLiveData<Result<Weather>> = MutableLiveData()
val weather: LiveData<Result<Weather>> = _weather
fun getWeatherById(id:Int) {
fun getWeatherById(id: Int) {
viewModelScope.launch {
try {
val weather = getWeatherByIdUseCase(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import androidx.lifecycle.viewModelScope
import com.example.web_app.domain.entity.Weather
import com.example.web_app.domain.usecase.GetWeatherByCityUseCase
import com.example.web_app.domain.usecase.GetWeatherListUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SearchFragmentViewModel @Inject constructor(
private val getWeatherListUseCase: GetWeatherListUseCase,
private val getWeatherByCityUseCase: GetWeatherByCityUseCase
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"

classpath "com.google.dagger:hilt-android-gradle-plugin:2.41"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down