Skip to content

Commit

Permalink
add koin for dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Sep 28, 2021
1 parent eb730b5 commit 5b71dd2
Show file tree
Hide file tree
Showing 20 changed files with 446 additions and 376 deletions.
8 changes: 8 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,12 @@ dependencies {

// for dark mode
implementation 'androidx.webkit:webkit:1.4.0'

def koin_version = "3.1.2"
// Koin core features
implementation "io.insert-koin:koin-core:$koin_version"
// Koin test features
testImplementation "io.insert-koin:koin-test:$koin_version"
// Android
implementation "io.insert-koin:koin-android-compat:$koin_version"
}
22 changes: 22 additions & 0 deletions app/src/main/java/de/baumann/browser/EinkBroApplication.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
package de.baumann.browser

import android.app.Application
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager
import de.baumann.browser.database.BookmarkManager
import de.baumann.browser.epub.EpubManager
import de.baumann.browser.preference.ConfigManager
import de.baumann.browser.view.dialog.DialogManager
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.GlobalContext.startKoin
import org.koin.dsl.module

class EinkBroApplication : Application() {

override fun onCreate() {
super.onCreate()
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
startKoin {
androidContext(this@EinkBroApplication)
modules(myModule)
}
}
}

val myModule = module {
single { ConfigManager(androidContext()) }
single { PreferenceManager.getDefaultSharedPreferences(androidContext()) }
single { BookmarkManager(androidContext()) }

single { DialogManager(it[0]) }
single { EpubManager(it[0]) }
}
138 changes: 73 additions & 65 deletions app/src/main/java/de/baumann/browser/activity/BrowserActivity.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ import android.graphics.BitmapFactory
import android.util.Base64
import de.baumann.browser.util.DebugT
import de.baumann.browser.view.dTLoadUrl
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.io.ByteArrayInputStream


class NinjaWebViewClient(
private val ninjaWebView: NinjaWebView,
private val addHistoryAction: (String) -> Unit
) : WebViewClient() {
private val ninjaWebView: NinjaWebView,
private val addHistoryAction: (String) -> Unit
) : WebViewClient(), KoinComponent {
private val context: Context = ninjaWebView.context
private val sp: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
private val config: ConfigManager = ConfigManager(context)
private val sp: SharedPreferences by inject()
private val config: ConfigManager by inject()
private val adBlock: AdBlock = ninjaWebView.adBlock
private val cookie: Cookie = ninjaWebView.cookieHosts
private val white: Boolean = false
Expand Down
28 changes: 16 additions & 12 deletions app/src/main/java/de/baumann/browser/database/BookmarkDao.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package de.baumann.browser.database

import android.content.Context
import android.graphics.Bitmap
import androidx.room.*
import de.baumann.browser.preference.ConfigManager
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

@Database(entities = [Bookmark::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
Expand Down Expand Up @@ -39,16 +42,17 @@ interface BookmarkDao {
suspend fun deleteAll()
}

class BookmarkManager(private val context: Context) {
class BookmarkManager(private val context: Context) : KoinComponent {
val config: ConfigManager by inject()

val database = Room.databaseBuilder(
context,
AppDatabase::class.java, "einkbro_db"
context,
AppDatabase::class.java, "einkbro_db"
).build()

val bookmarkDao = database.bookmarkDao()

suspend fun migrateOldData() {
val config = ConfigManager(context)
if (config.dbVersion != 0) return

//add bookmarks
Expand All @@ -57,8 +61,8 @@ class BookmarkManager(private val context: Context) {
cursor.moveToFirst()
while (!cursor.isAfterLast) {
insert(
title = cursor.getString(1),
url = cursor.getString(2),
title = cursor.getString(1),
url = cursor.getString(2),
)
cursor.moveToNext()
}
Expand Down Expand Up @@ -92,12 +96,12 @@ class BookmarkManager(private val context: Context) {

suspend fun insertDirectory(title: String, parentId: Int = 0) {
bookmarkDao.insert(
Bookmark(
title = title,
url = "",
isDirectory = true,
parent = parentId,
)
Bookmark(
title = title,
url = "",
isDirectory = true,
parent = parentId,
)
)
}

Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/de/baumann/browser/database/RecordDb.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package de.baumann.browser.database

import android.app.Activity
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import androidx.core.database.sqlite.transaction
import de.baumann.browser.unit.RecordUnit
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.util.*

class RecordDb(context: Context?) {
class RecordDb(context: Context?): KoinComponent {
private lateinit var database: SQLiteDatabase
private val helper: RecordHelper = RecordHelper(context)
private val bookmarkManager: BookmarkManager by inject()

fun open(rw: Boolean) {
database = if (rw) helper.writableDatabase else helper.readableDatabase
Expand Down Expand Up @@ -157,8 +159,7 @@ class RecordDb(context: Context?) {
)
}

suspend fun listEntries(activity: Activity, listAll: Boolean, amount: Int = 0): List<Record> {
val bookmarkManager = BookmarkManager(activity)
suspend fun listEntries(listAll: Boolean, amount: Int = 0): List<Record> {
val list: MutableList<Record> = ArrayList()
if (listAll) {
//add bookmarks
Expand Down
Loading

0 comments on commit 5b71dd2

Please sign in to comment.