Skip to content

Hw5 weather app hilt #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 2 commits into
base: hw4-weather-app-dagger2
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
12 changes: 10 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id 'kotlin-parcelize'
id 'kotlin-kapt'
id 'androidx.navigation.safeargs.kotlin'
id 'dagger.hilt.android.plugin'
}

android {
Expand Down Expand Up @@ -91,9 +92,16 @@ dependencies {

def dagger_version = "2.41"
implementation "com.google.dagger:dagger:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"

def hilt_version = "2.38.1"
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"

def hiltExt = "1.3.0-RC1"
implementation "it.czerwinski.android.hilt:hilt-extensions:${hiltExt}"
kapt "it.czerwinski.android.hilt:hilt-processor:${hiltExt}"


testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
14 changes: 2 additions & 12 deletions app/src/main/java/ru/itis/karakurik/androidLab2/WeatherApp.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
package ru.itis.karakurik.androidLab2

import android.app.Application
import ru.itis.karakurik.androidLab2.di.AppComponent
import ru.itis.karakurik.androidLab2.di.DaggerAppComponent
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class WeatherApp : Application() {

lateinit var appComponent: AppComponent

override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent
.builder()
// .context(context = this)
.application(this)
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WeatherMapper @Inject constructor(
private val weatherIconUrlMapper: WeatherIconUrlMapper
) {

fun map(weatherResponse: WeatherResponse) : Weather = Weather(
fun map(weatherResponse: WeatherResponse): Weather = Weather(
id = weatherResponse.id,
name = weatherResponse.name,
lat = weatherResponse.coord.lat,
Expand All @@ -27,7 +27,7 @@ class WeatherMapper @Inject constructor(
iconUrl = weatherIconUrlMapper.mapToLargeIcon(weatherResponse.weather[0].icon)
)

fun map(city: City) : Weather = Weather(
fun map(city: City): Weather = Weather(
id = city.id,
name = city.name,
lat = city.coord.lat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import javax.inject.Singleton

@Singleton
class WindDegMapper @Inject constructor() {
fun map(deg: Int) : WindDeg {
return when ((deg / 45 + 2* (deg%45) / 45) * 45 % 360) {
fun map(deg: Int): WindDeg {
return when ((deg / 45 + 2 * (deg % 45) / 45) * 45 % 360) {
0 -> WindDeg.N
45 -> WindDeg.NE
90 -> WindDeg.E
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package ru.itis.karakurik.androidLab2.data.api.repository

import dagger.hilt.components.SingletonComponent
import it.czerwinski.android.hilt.annotations.BoundTo
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
import ru.itis.karakurik.androidLab2.domain.repository.WeatherRepository
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
@BoundTo(supertype = WeatherRepository::class, component = SingletonComponent::class)
class WeatherRepositoryImpl @Inject constructor(
private val api: Api,
private val weatherMapper: WeatherMapper
Expand Down
37 changes: 0 additions & 37 deletions app/src/main/java/ru/itis/karakurik/androidLab2/di/AppComponent.kt

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.android.gms.location.LocationServices
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import ru.itis.karakurik.androidLab2.WeatherApp
import javax.inject.Qualifier
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
class AppModule {

@Provides
fun provideContext(weatherApp: WeatherApp): Context = weatherApp.applicationContext
// @Provides
// fun provideContext(weatherApp: WeatherApp): Context = weatherApp.applicationContext

@Provides
@DefaultDispatcher
Expand Down Expand Up @@ -43,7 +48,7 @@ class AppModule {

@Provides
fun provideSmoothScroller(
context: Context
@ApplicationContext context: Context
): RecyclerView.SmoothScroller = object : LinearSmoothScroller(context) {
override fun getVerticalSnapPreference(): Int {
return SNAP_TO_START
Expand All @@ -52,6 +57,6 @@ class AppModule {

@Provides
fun provideFusedLocationProviderClient(
context: Context
@ApplicationContext context: Context
) = LocationServices.getFusedLocationProviderClient(context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ru.itis.karakurik.androidLab2.di.module

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.Cache
import okhttp3.Interceptor
import okhttp3.OkHttpClient
Expand All @@ -10,10 +12,10 @@ 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.di.annotation.ApiKey
import ru.itis.karakurik.androidLab2.di.annotation.Lang
import ru.itis.karakurik.androidLab2.di.annotation.Logger
import ru.itis.karakurik.androidLab2.di.annotation.Units
import ru.itis.karakurik.androidLab2.di.qualifier.ApiKeyInterceptor
import ru.itis.karakurik.androidLab2.di.qualifier.LangInterceptor
import ru.itis.karakurik.androidLab2.di.qualifier.LoggingInterceptor
import ru.itis.karakurik.androidLab2.di.qualifier.UnitsInterceptor
import java.io.File
import javax.inject.Qualifier

Expand All @@ -25,11 +27,12 @@ private const val LANG = "RU"
private const val QUERY_UNITS = "units"
private const val UNITS = "metric"

@InstallIn(SingletonComponent::class)
@Module
class NetModule {

@Provides
@ApiKey
@ApiKeyInterceptor
fun apiKeyInterceptor(): Interceptor = Interceptor { chain ->
val request = chain.request()
val newUrl = request.url.newBuilder()
Expand All @@ -44,7 +47,7 @@ class NetModule {
}

@Provides
@Lang
@LangInterceptor
fun langInterceptor(): Interceptor = Interceptor { chain ->
val request = chain.request()
val newUrl = request.url.newBuilder()
Expand All @@ -59,7 +62,7 @@ class NetModule {
}

@Provides
@Units
@UnitsInterceptor
fun unitsInterceptor(): Interceptor = Interceptor { chain ->
val request = chain.request()
val newUrl = request.url.newBuilder()
Expand All @@ -74,7 +77,7 @@ class NetModule {
}

@Provides
@Logger
@LoggingInterceptor
fun provideLoggingInterceptor(): Interceptor {
return HttpLoggingInterceptor()
.setLevel(
Expand Down Expand Up @@ -107,10 +110,10 @@ class NetModule {
@Provides
fun provideOkhttpClient(
cache: Cache,
@ApiKey apiKeyInterceptor: Interceptor,
@Lang langInterceptor: Interceptor,
@Units unitsInterceptor: Interceptor,
@Logger loggingInterceptor: Interceptor,
@ApiKeyInterceptor apiKeyInterceptor: Interceptor,
@LangInterceptor langInterceptor: Interceptor,
@UnitsInterceptor unitsInterceptor: Interceptor,
@LoggingInterceptor loggingInterceptor: Interceptor,
): OkHttpClient =
OkHttpClient.Builder()
.cache(cache)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.itis.karakurik.androidLab2.di.qualifier

import javax.inject.Qualifier

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ApiKeyInterceptor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.itis.karakurik.androidLab2.di.qualifier

import javax.inject.Qualifier

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class LangInterceptor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.itis.karakurik.androidLab2.di.qualifier

import javax.inject.Qualifier

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class LoggingInterceptor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.itis.karakurik.androidLab2.di.qualifier

import javax.inject.Qualifier

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class UnitsInterceptor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package ru.itis.karakurik.androidLab2.domain.enum

enum class WindDeg (
val deg: Int
) {
Expand Down
Loading