-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replacing Dagger Hilt with Koin for Dependency injection.
- Loading branch information
Debanshu Datta
committed
Jun 24, 2022
1 parent
8824ff9
commit 9bad062
Showing
14 changed files
with
114 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 30 additions & 61 deletions
91
app/src/main/java/com/debanshu777/compose_github/di/AppModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,53 @@ | ||
package com.debanshu777.compose_github.di | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import com.debanshu777.compose_github.GithubDatatbase | ||
import com.debanshu777.compose_github.network.dataSource.GitHubViewModel | ||
import com.debanshu777.compose_github.network.dataSource.MainRepository | ||
import com.debanshu777.compose_github.network.dataSource.local.DeveloperDataSource | ||
import com.debanshu777.compose_github.network.dataSource.local.DeveloperDataSourceImpl | ||
import com.debanshu777.compose_github.network.dataSource.local.LocalRepository | ||
import com.debanshu777.compose_github.network.dataSource.local.RepositoryDataSource | ||
import com.debanshu777.compose_github.network.dataSource.local.RepositoryDataSourceImpl | ||
import com.debanshu777.compose_github.network.dataSource.remote.RemoteRepository | ||
import com.squareup.sqldelight.android.AndroidSqliteDriver | ||
import com.squareup.sqldelight.db.SqlDriver | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.android.* | ||
import io.ktor.client.plugins.* | ||
import io.ktor.client.plugins.contentnegotiation.* | ||
import io.ktor.client.plugins.logging.* | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import io.ktor.serialization.kotlinx.json.* | ||
import kotlinx.serialization.json.Json | ||
import javax.inject.Singleton | ||
import org.koin.android.ext.koin.androidContext | ||
import org.koin.androidx.viewmodel.dsl.viewModel | ||
import org.koin.dsl.module | ||
|
||
@InstallIn(SingletonComponent::class) | ||
@Module | ||
object AppModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun providesSqlDriver(@ApplicationContext context: Context): SqlDriver { | ||
return AndroidSqliteDriver( | ||
val appModule = module { | ||
single<SqlDriver> { | ||
AndroidSqliteDriver( | ||
schema = GithubDatatbase.Schema, | ||
context = context, | ||
context = androidContext(), | ||
name = "github.db" | ||
) | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideRepositoryDataSource(driver: SqlDriver): RepositoryDataSource { | ||
return RepositoryDataSourceImpl(GithubDatatbase(driver)) | ||
single<RepositoryDataSource> { | ||
RepositoryDataSourceImpl(GithubDatatbase(get())) | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideDeveloperDataSource(driver: SqlDriver): DeveloperDataSource { | ||
return DeveloperDataSourceImpl(GithubDatatbase(driver)) | ||
single<DeveloperDataSource> { | ||
DeveloperDataSourceImpl(GithubDatatbase(get())) | ||
} | ||
|
||
@Provides | ||
fun provideKtorAPIClient(): HttpClient { | ||
val jsonProps = Json { | ||
single { | ||
Json { | ||
encodeDefaults = true | ||
ignoreUnknownKeys = true | ||
isLenient = true | ||
} | ||
return HttpClient(Android) { | ||
install(ContentNegotiation) { | ||
json(jsonProps) | ||
} | ||
install(Logging) { | ||
logger = object : Logger { | ||
override fun log(message: String) { | ||
Log.d("Network Message", "log: $message") | ||
} | ||
} | ||
level = LogLevel.ALL | ||
} | ||
install(HttpTimeout) { | ||
socketTimeoutMillis = 30_0000 | ||
requestTimeoutMillis = 30_0000 | ||
connectTimeoutMillis = 30_0000 | ||
} | ||
defaultRequest { | ||
contentType(ContentType.Application.Json) | ||
accept(ContentType.Application.Json) | ||
} | ||
} | ||
} | ||
} | ||
single { | ||
RemoteRepository() | ||
} | ||
single { | ||
LocalRepository(get(), get()) | ||
} | ||
single { | ||
MainRepository(get(), get()) | ||
} | ||
viewModel { | ||
GitHubViewModel(get()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 17 additions & 3 deletions
20
app/src/main/java/com/debanshu777/compose_github/ui/BaseApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
package com.debanshu777.compose_github.ui | ||
|
||
import android.app.Application | ||
import dagger.hilt.android.HiltAndroidApp | ||
import com.debanshu777.compose_github.di.appModule | ||
import org.koin.android.ext.koin.androidContext | ||
import org.koin.android.ext.koin.androidLogger | ||
import org.koin.core.context.startKoin | ||
|
||
@HiltAndroidApp | ||
class BaseApplication : Application() | ||
|
||
class BaseApplication : Application() { | ||
override fun onCreate() { | ||
super.onCreate() | ||
startKoin { | ||
androidLogger() | ||
androidContext(this@BaseApplication) | ||
modules( | ||
appModule | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.