Skip to content

Commit

Permalink
fixes #210 -- replace dagger component builder with dagger factory
Browse files Browse the repository at this point in the history
  • Loading branch information
sphrak committed Oct 18, 2020
1 parent 8d82017 commit 425b451
Show file tree
Hide file tree
Showing 56 changed files with 657 additions and 560 deletions.
26 changes: 6 additions & 20 deletions app/src/main/java/fi/kroon/vadret/BaseApplication.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
package fi.kroon.vadret

import android.app.Application
import android.content.Context
import com.jakewharton.threetenabp.AndroidThreeTen
import fi.kroon.vadret.di.component.CoreApplicationComponent
import fi.kroon.vadret.di.component.DaggerCoreApplicationComponent
import fi.kroon.vadret.di.modules.ContextModule
import fi.kroon.vadret.di.modules.DatabaseModule
import fi.kroon.vadret.core.CoreComponent
import fi.kroon.vadret.core.CoreComponentFactory
import fi.kroon.vadret.core.CoreComponentProvider
import timber.log.Timber

abstract class BaseApplication : Application() {
abstract class BaseApplication : Application(), CoreComponentProvider {

companion object {
@JvmStatic
fun appComponent(context: Context): CoreApplicationComponent =
(context.applicationContext as BaseApplication).cmp
}

val cmp: CoreApplicationComponent by lazy(LazyThreadSafetyMode.NONE) {
DaggerCoreApplicationComponent
.builder()
.contextModule(ContextModule(this))
.databaseModule(DatabaseModule(this))
.build()
}
override val coreComponent: CoreComponent
get() = CoreComponentFactory.getInstance(applicationContext)

override fun onCreate() {
super.onCreate()
cmp.inject(this)
plantTimber()
initThreeTenAbp()
cacheDir.delete()
Expand Down
44 changes: 44 additions & 0 deletions app/src/main/java/fi/kroon/vadret/core/CoreComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package fi.kroon.vadret.core

import android.content.Context
import com.afollestad.rxkprefs.RxkPrefs
import com.squareup.moshi.Moshi
import dagger.BindsInstance
import dagger.Component
import fi.kroon.vadret.core.module.ApiServiceModule
import fi.kroon.vadret.core.module.CacheModule
import fi.kroon.vadret.core.module.DatabaseModule
import fi.kroon.vadret.core.module.NetworkModule
import fi.kroon.vadret.core.module.RxkPrefsModule
import fi.kroon.vadret.data.exception.ErrorHandler
import fi.kroon.vadret.data.persistance.AppDatabase
import okhttp3.OkHttpClient
import okhttp3.internal.cache.DiskLruCache

@CoreScope
@Component(
modules = [
ApiServiceModule::class,
CacheModule::class,
DatabaseModule::class,
NetworkModule::class,
RxkPrefsModule::class
]
)
interface CoreComponent {

fun provideOkHttpClient(): OkHttpClient
fun provideAppDatabase(): AppDatabase
fun provideMoshi(): Moshi
fun provideDiskLruCache(): DiskLruCache
fun provideRxkPrefs(): RxkPrefs
fun provideErrorHandler(): ErrorHandler

@Component.Factory
interface Factory {
fun create(
@BindsInstance
context: Context
): CoreComponent
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/fi/kroon/vadret/core/CoreComponentFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fi.kroon.vadret.core

import android.content.Context

object CoreComponentFactory {

@Volatile
private var instance: CoreComponent? = null

fun getInstance(context: Context): CoreComponent =
instance ?: synchronized(this) {
instance ?: DaggerCoreComponent
.factory()
.create(
context = context
).also { instance = it }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fi.kroon.vadret.core

interface CoreComponentProvider {
val coreComponent: CoreComponent
}
7 changes: 7 additions & 0 deletions app/src/main/java/fi/kroon/vadret/core/CoreScope.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fi.kroon.vadret.core

import javax.inject.Scope

@Scope
@kotlin.annotation.Retention
annotation class CoreScope
18 changes: 18 additions & 0 deletions app/src/main/java/fi/kroon/vadret/core/module/ApiServiceModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fi.kroon.vadret.core.module

import com.squareup.moshi.Moshi
import dagger.Module
import dagger.Provides
import fi.kroon.vadret.core.CoreScope
import fi.kroon.vadret.data.common.SingleToArrayAdapter

@Module
object ApiServiceModule {

@Provides
@CoreScope
fun provideMoshi(): Moshi = Moshi
.Builder()
.add(SingleToArrayAdapter.INSTANCE)
.build()
}
24 changes: 24 additions & 0 deletions app/src/main/java/fi/kroon/vadret/core/module/CacheModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fi.kroon.vadret.core.module

import android.content.Context
import dagger.Module
import dagger.Provides
import fi.kroon.vadret.core.CoreScope
import fi.kroon.vadret.util.DISK_CACHE_SIZE
import okhttp3.internal.cache.DiskLruCache
import okhttp3.internal.io.FileSystem

@Module
object CacheModule {

@Provides
@CoreScope
fun provideDiskLruCache(context: Context): DiskLruCache =
DiskLruCache.create(
FileSystem.SYSTEM,
context.cacheDir,
1,
1,
DISK_CACHE_SIZE
)
}
23 changes: 23 additions & 0 deletions app/src/main/java/fi/kroon/vadret/core/module/DatabaseModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fi.kroon.vadret.core.module

import android.content.Context
import androidx.room.Room
import dagger.Module
import dagger.Provides
import fi.kroon.vadret.core.CoreScope
import fi.kroon.vadret.data.persistance.AppDatabase
import fi.kroon.vadret.util.DATABASE_NAME

@Module
object DatabaseModule {

@Provides
@CoreScope
fun provideAppDatabase(
context: Context
): AppDatabase = Room.databaseBuilder(
context,
AppDatabase::class.java,
DATABASE_NAME
).build()
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package fi.kroon.vadret.di.modules
package fi.kroon.vadret.core.module

import android.content.Context
import dagger.Module
import dagger.Provides
import fi.kroon.vadret.BuildConfig
import fi.kroon.vadret.di.scope.CoreApplicationScope
import fi.kroon.vadret.core.CoreScope
import fi.kroon.vadret.data.exception.ErrorHandler
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.io.File
import java.util.concurrent.TimeUnit
import javax.inject.Qualifier

@Module(
includes = [
ContextModule::class
]
)
@Module
object NetworkModule {

private const val DEFAULT_CONNECTION_TIMEOUT = 10000L
Expand All @@ -37,14 +34,18 @@ object NetworkModule {
}

@Provides
@CoreApplicationScope
@CoreScope
fun provideFailureHandler(): ErrorHandler = ErrorHandler()

@Provides
@CoreScope
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor =
HttpLoggingInterceptor()
.apply { level = getLogLevel }

@Provides
@InternalApi
@CoreApplicationScope
@CoreScope
fun provideCache(
context: Context
): Cache = Cache(
Expand All @@ -56,7 +57,7 @@ object NetworkModule {
)

@Provides
@CoreApplicationScope
@CoreScope
fun provideOkHttpClient(
httpLoggingInterceptor: HttpLoggingInterceptor,
@InternalApi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package fi.kroon.vadret.di.modules
package fi.kroon.vadret.core.module

import android.content.Context
import com.afollestad.rxkprefs.RxkPrefs
import com.afollestad.rxkprefs.rxkPrefs
import dagger.Module
import dagger.Provides
import fi.kroon.vadret.data.exception.ErrorHandler
import fi.kroon.vadret.di.scope.CoreApplicationScope
import fi.kroon.vadret.core.CoreScope
import fi.kroon.vadret.util.DEFAULT_SETTINGS

@Module
object RxkPrefsModule {

@Provides
@CoreApplicationScope
@CoreScope
fun provideRxkPrefs(context: Context): RxkPrefs = rxkPrefs(context, DEFAULT_SETTINGS)

@Provides
@CoreApplicationScope
fun provideFailureHandler(): ErrorHandler = ErrorHandler()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import fi.kroon.vadret.data.exception.ExceptionHandler
import fi.kroon.vadret.data.exception.IErrorHandler
import fi.kroon.vadret.data.exception.IExceptionHandler
import fi.kroon.vadret.data.failure.Failure
import fi.kroon.vadret.di.scope.CoreApplicationScope
import fi.kroon.vadret.util.NetworkHandler
import fi.kroon.vadret.util.extension.asLeft
import fi.kroon.vadret.util.extension.asRight
Expand All @@ -17,7 +16,6 @@ import io.reactivex.Single
import retrofit2.Response
import javax.inject.Inject

@CoreApplicationScope
class AggregatedFeedRepository @Inject constructor(
private val networkHandler: NetworkHandler,
private val netDataSource: Lazy<AggregatedFeedNetDataSource>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import fi.kroon.vadret.data.exception.ExceptionHandler
import fi.kroon.vadret.data.exception.IErrorHandler
import fi.kroon.vadret.data.exception.IExceptionHandler
import fi.kroon.vadret.data.failure.Failure
import fi.kroon.vadret.di.scope.CoreApplicationScope
import fi.kroon.vadret.util.NetworkHandler
import fi.kroon.vadret.util.extension.asLeft
import fi.kroon.vadret.util.extension.asRight
Expand All @@ -17,7 +16,6 @@ import io.reactivex.Single
import retrofit2.Response
import javax.inject.Inject

@CoreApplicationScope
class DistrictRepository @Inject constructor(
private val networkHandler: NetworkHandler,
private val networkDataSource: Lazy<DistrictNetDataSource>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package fi.kroon.vadret.data.exception

import fi.kroon.vadret.data.failure.Failure
import fi.kroon.vadret.di.scope.CoreApplicationScope
import fi.kroon.vadret.util.extension.asLeft
import fi.kroon.vadret.util.extension.asSingle
import io.github.sphrak.either.Either
import io.reactivex.Single

@CoreApplicationScope
class ErrorHandler : IErrorHandler {

override fun <T> getNetworkError(throwable: Throwable): Single<Either<Failure, T>> =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package fi.kroon.vadret.data.exception

import fi.kroon.vadret.data.failure.Failure
import fi.kroon.vadret.di.scope.CoreApplicationScope
import fi.kroon.vadret.util.extension.empty
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import javax.inject.Inject

@CoreApplicationScope
class ExceptionHandler @Inject constructor() : IExceptionHandler<Failure> {
override fun invoke(throwable: Throwable): Failure =
when (throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import fi.kroon.vadret.data.exception.IExceptionHandler
import fi.kroon.vadret.data.failure.Failure
import fi.kroon.vadret.data.feedsource.model.FeedSource
import fi.kroon.vadret.data.feedsource.net.FeedSourceNetDataSource
import fi.kroon.vadret.di.scope.CoreApplicationScope
import fi.kroon.vadret.util.NetworkHandler
import fi.kroon.vadret.util.extension.asLeft
import fi.kroon.vadret.util.extension.asRight
Expand All @@ -17,7 +16,6 @@ import io.reactivex.Single
import retrofit2.Response
import javax.inject.Inject

@CoreApplicationScope
class FeedSourceRepository @Inject constructor(
private val networkHandler: NetworkHandler,
private val feedSourceNetDataSource: Lazy<FeedSourceNetDataSource>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fi.kroon.vadret.data.failure.Failure
import fi.kroon.vadret.data.location.exception.LocationFailure
import fi.kroon.vadret.util.extension.asLeft
import fi.kroon.vadret.util.extension.asRight
import fi.kroon.vadret.util.extension.lazyAndroid
import io.github.sphrak.either.Either
import timber.log.Timber
import javax.inject.Inject
Expand All @@ -17,11 +18,11 @@ class LocationLocalDataSource @Inject constructor(
private val locationManager: LocationManager
) {

private val isGPSEnabled: Boolean by lazy(LazyThreadSafetyMode.NONE) {
private val isGPSEnabled: Boolean by lazyAndroid {
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
}

private val isNetworkLocationProviderEnabled: Boolean by lazy(LazyThreadSafetyMode.NONE) {
private val isNetworkLocationProviderEnabled: Boolean by lazyAndroid {
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import fi.kroon.vadret.data.nominatim.model.Nominatim
import fi.kroon.vadret.data.nominatim.model.NominatimOut
import fi.kroon.vadret.data.nominatim.model.NominatimReverseOut
import fi.kroon.vadret.data.nominatim.net.NominatimNetDataSource
import fi.kroon.vadret.di.scope.CoreApplicationScope
import fi.kroon.vadret.util.NetworkHandler
import fi.kroon.vadret.util.extension.asLeft
import fi.kroon.vadret.util.extension.asRight
Expand All @@ -20,7 +19,6 @@ import io.reactivex.Single
import retrofit2.Response
import javax.inject.Inject

@CoreApplicationScope
class NominatimRepository @Inject constructor(
private val nominatimNetDataSource: Lazy<NominatimNetDataSource>,
private val networkHandler: NetworkHandler,
Expand Down
Loading

0 comments on commit 425b451

Please sign in to comment.