ReadYou/app/src/main/java/me/ash/reader/App.kt

96 lines
2.7 KiB
Kotlin
Raw Normal View History

2022-03-01 18:40:53 +01:00
package me.ash.reader
import android.app.Application
2022-03-21 15:40:40 +01:00
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.*
2022-03-01 18:40:53 +01:00
import dagger.hilt.android.HiltAndroidApp
2022-03-21 15:40:40 +01:00
import kotlinx.coroutines.CoroutineScope
2022-03-01 18:40:53 +01:00
import kotlinx.coroutines.DelicateCoroutinesApi
2022-03-21 15:40:40 +01:00
import kotlinx.coroutines.Dispatchers
2022-03-01 18:40:53 +01:00
import kotlinx.coroutines.launch
import me.ash.reader.data.repository.*
2022-03-01 18:40:53 +01:00
import me.ash.reader.data.source.OpmlLocalDataSource
import me.ash.reader.data.source.ReaderDatabase
import me.ash.reader.data.source.RssNetworkDataSource
2022-03-21 15:40:40 +01:00
import java.util.concurrent.TimeUnit
2022-03-01 18:40:53 +01:00
import javax.inject.Inject
@DelicateCoroutinesApi
@HiltAndroidApp
2022-03-21 15:40:40 +01:00
class App : Application(), Configuration.Provider {
2022-03-01 18:40:53 +01:00
@Inject
lateinit var readerDatabase: ReaderDatabase
2022-03-21 15:40:40 +01:00
@Inject
lateinit var workerFactory: HiltWorkerFactory
@Inject
lateinit var workManager: WorkManager
2022-03-01 18:40:53 +01:00
@Inject
lateinit var opmlLocalDataSource: OpmlLocalDataSource
@Inject
lateinit var rssNetworkDataSource: RssNetworkDataSource
@Inject
lateinit var rssHelper: RssHelper
2022-03-20 17:07:45 +01:00
@Inject
lateinit var stringsRepository: StringsRepository
2022-03-01 18:40:53 +01:00
@Inject
lateinit var accountRepository: AccountRepository
@Inject
lateinit var localRssRepository: LocalRssRepository
2022-03-01 18:40:53 +01:00
2022-03-17 19:11:52 +01:00
@Inject
lateinit var feverRssRepository: FeverRssRepository
2022-03-01 18:40:53 +01:00
@Inject
lateinit var opmlRepository: OpmlRepository
@Inject
lateinit var rssRepository: RssRepository
2022-03-21 15:40:40 +01:00
private val applicationScope = CoroutineScope(Dispatchers.IO)
2022-03-01 18:40:53 +01:00
override fun onCreate() {
super.onCreate()
2022-03-21 15:40:40 +01:00
applicationScope.launch {
accountInit()
workerInit()
}
}
private suspend fun accountInit() {
if (accountRepository.isNoAccount()) {
val account = accountRepository.addDefaultAccount()
applicationContext.dataStore.put(DataStoreKeys.CurrentAccountId, account.id!!)
applicationContext.dataStore.put(DataStoreKeys.CurrentAccountType, account.type)
2022-03-01 18:40:53 +01:00
}
}
2022-03-21 15:40:40 +01:00
private fun workerInit() {
val repeatingRequest = PeriodicWorkRequestBuilder<SyncWorker>(
15, TimeUnit.MINUTES
).setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
).addTag(SyncWorker.WORK_NAME).build()
workManager.enqueueUniquePeriodicWork(
SyncWorker.WORK_NAME,
ExistingPeriodicWorkPolicy.REPLACE,
repeatingRequest
)
}
override fun getWorkManagerConfiguration(): Configuration =
Configuration.Builder()
.setWorkerFactory(workerFactory)
.setMinimumLoggingLevel(android.util.Log.DEBUG)
.build()
2022-03-01 18:40:53 +01:00
}