diff --git a/README.md b/README.md index 049f29f..c7f82d5 100644 --- a/README.md +++ b/README.md @@ -249,20 +249,16 @@ Reader.clearCache() ### Global Config -To let KtRssReader works with the database, you need to set the application context in your application. - ```kotlin class MyApplication : Application() { override fun onCreate() { readerGlobalConfig { - setApplicationContext(this@MyApplication) enableLog = true } } } ``` -* `setApplicationContext()`: The application context. * `enableLog`: If this is enabled, the debug log will be shown on the console. ### Reader Config diff --git a/app/src/main/java/tw/ktrssreader/SampleApplication.kt b/app/src/main/java/tw/ktrssreader/SampleApplication.kt index d95f8ad..89f2b96 100644 --- a/app/src/main/java/tw/ktrssreader/SampleApplication.kt +++ b/app/src/main/java/tw/ktrssreader/SampleApplication.kt @@ -8,7 +8,6 @@ class SampleApplication : Application() { override fun onCreate() { super.onCreate() readerGlobalConfig { - setApplicationContext(this@SampleApplication) enableLog = true } } diff --git a/build.gradle b/build.gradle index ea06b85..f560e51 100644 --- a/build.gradle +++ b/build.gradle @@ -5,6 +5,7 @@ buildscript { def coroutines_version = '1.3.9' def okhttp_version = '4.9.0' def room_version = '2.2.5' + def startup_version = '1.0.0' def mockk_version = '1.10.0' def turbine_version = '0.2.1' @@ -16,6 +17,7 @@ buildscript { 'roomRuntime' : "androidx.room:room-runtime:$room_version", 'roomKtx' : "androidx.room:room-ktx:$room_version", 'roomCompiler' : "androidx.room:room-compiler:$room_version", + 'startup' : "androidx.startup:startup-runtime:$startup_version" ] testDeps = [ 'mockk' : "io.mockk:mockk:$mockk_version", diff --git a/ktRssReader/build.gradle b/ktRssReader/build.gradle index 9f8fcff..f84232f 100644 --- a/ktRssReader/build.gradle +++ b/ktRssReader/build.gradle @@ -53,6 +53,8 @@ dependencies { implementation deps.roomKtx kapt deps.roomCompiler + implementation deps.startup + testImplementation testDeps.mockk testImplementation testDeps.turbine diff --git a/ktRssReader/src/main/AndroidManifest.xml b/ktRssReader/src/main/AndroidManifest.xml index 1c8feb4..bf28237 100644 --- a/ktRssReader/src/main/AndroidManifest.xml +++ b/ktRssReader/src/main/AndroidManifest.xml @@ -1,5 +1,18 @@ - / + + + + + + + \ No newline at end of file diff --git a/ktRssReader/src/main/java/tw/ktrssreader/KtRssReader.kt b/ktRssReader/src/main/java/tw/ktrssreader/KtRssReader.kt index 8940067..67c7ee2 100644 --- a/ktRssReader/src/main/java/tw/ktrssreader/KtRssReader.kt +++ b/ktRssReader/src/main/java/tw/ktrssreader/KtRssReader.kt @@ -18,7 +18,6 @@ package tw.ktrssreader import kotlinx.coroutines.flow.flow import tw.ktrssreader.config.KtRssReaderConfig -import tw.ktrssreader.config.KtRssReaderGlobalConfig import tw.ktrssreader.constant.Const import tw.ktrssreader.provider.KtRssProvider import tw.ktrssreader.utils.ThreadUtils @@ -98,7 +97,9 @@ object Reader { url: String, crossinline customParser: ((String) -> T?) = { null }, crossinline config: Config = {} - ) = suspendCoroutine { it.resume(read(url = url, customParser = customParser, config = config)) } + ) = suspendCoroutine { + it.resume(read(url = url, customParser = customParser, config = config)) + } inline fun flowRead( url: String, @@ -108,7 +109,7 @@ object Reader { fun clearCache() { ThreadUtils.runOnNewThread("[clear cache]") { - val db = KtRssProvider.provideDatabase(KtRssReaderGlobalConfig.getApplicationContext()) + val db = KtRssProvider.provideDatabase(KtRssReaderInitializer.applicationContext) db.channelDao().clearAll() } } diff --git a/ktRssReader/src/main/java/tw/ktrssreader/KtRssReaderInitializer.kt b/ktRssReader/src/main/java/tw/ktrssreader/KtRssReaderInitializer.kt new file mode 100644 index 0000000..8e050eb --- /dev/null +++ b/ktRssReader/src/main/java/tw/ktrssreader/KtRssReaderInitializer.kt @@ -0,0 +1,19 @@ +package tw.ktrssreader + +import android.content.Context +import androidx.startup.Initializer + +class KtRssReaderInitializer : Initializer { + + companion object { + lateinit var applicationContext: Context + } + + override fun create(context: Context) { + applicationContext = context + } + + override fun dependencies(): List>> { + return emptyList() + } +} \ No newline at end of file diff --git a/ktRssReader/src/main/java/tw/ktrssreader/cache/DatabaseRssCache.kt b/ktRssReader/src/main/java/tw/ktrssreader/cache/DatabaseRssCache.kt index 54e2547..2693945 100644 --- a/ktRssReader/src/main/java/tw/ktrssreader/cache/DatabaseRssCache.kt +++ b/ktRssReader/src/main/java/tw/ktrssreader/cache/DatabaseRssCache.kt @@ -16,7 +16,7 @@ package tw.ktrssreader.cache -import tw.ktrssreader.config.KtRssReaderGlobalConfig +import tw.ktrssreader.KtRssReaderInitializer import tw.ktrssreader.constant.Const import tw.ktrssreader.persistence.db.entity.ChannelEntity import tw.ktrssreader.provider.KtRssProvider @@ -28,7 +28,7 @@ import java.util.* class DatabaseRssCache : RssCache { private val logTag = this::class.java.simpleName - private val db = KtRssProvider.provideDatabase(KtRssReaderGlobalConfig.getApplicationContext()) + private val db = KtRssProvider.provideDatabase(KtRssReaderInitializer.applicationContext) private val dao = db.channelDao() override fun readCache(url: String, type: @Const.ChannelType Int, expiredTimeMillis: Long): T? { diff --git a/ktRssReader/src/main/java/tw/ktrssreader/config/KtRssReaderGlobalConfig.kt b/ktRssReader/src/main/java/tw/ktrssreader/config/KtRssReaderGlobalConfig.kt index 449b8b4..2b344a4 100644 --- a/ktRssReader/src/main/java/tw/ktrssreader/config/KtRssReaderGlobalConfig.kt +++ b/ktRssReader/src/main/java/tw/ktrssreader/config/KtRssReaderGlobalConfig.kt @@ -16,20 +16,8 @@ package tw.ktrssreader.config -import android.content.Context - object KtRssReaderGlobalConfig { - private var applicationContext: Context? = null var enableLog: Boolean = false - - fun setApplicationContext(applicationContext: Context) { - this.applicationContext = applicationContext - } - - fun getApplicationContext(): Context { - return applicationContext - ?: error("No Application Context configured. Please use readerGlobalConfig() DSL.") - } } fun readerGlobalConfig(builder: KtRssReaderGlobalConfig.() -> Unit) {