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

187 lines
5.4 KiB
Kotlin
Raw Normal View History

2022-03-01 18:40:53 +01:00
package me.ash.reader
import android.app.Application
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Build
2022-03-21 15:40:40 +01:00
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration
import androidx.work.WorkManager
import coil.ComponentRegistry
import coil.ImageLoader
import coil.decode.DataSource
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.decode.SvgDecoder
import coil.disk.DiskCache
import coil.memory.MemoryCache
import coil.request.*
2022-03-01 18:40:53 +01:00
import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.CompletableDeferred
2022-03-31 20:06:59 +02:00
import kotlinx.coroutines.CoroutineDispatcher
2022-03-21 15:40:40 +01:00
import kotlinx.coroutines.CoroutineScope
2022-03-01 18:40:53 +01:00
import kotlinx.coroutines.launch
import me.ash.reader.data.module.ApplicationScope
2022-03-31 20:06:59 +02:00
import me.ash.reader.data.module.DispatcherDefault
import me.ash.reader.data.repository.*
2022-04-23 21:39:32 +02:00
import me.ash.reader.data.source.AppNetworkDataSource
2022-03-01 18:40:53 +01:00
import me.ash.reader.data.source.OpmlLocalDataSource
import me.ash.reader.data.source.ReaderDatabase
2022-04-24 20:21:29 +02:00
import me.ash.reader.ui.ext.*
import org.conscrypt.Conscrypt
import java.security.Security
2022-03-01 18:40:53 +01:00
import javax.inject.Inject
@HiltAndroidApp
class App : Application(), Configuration.Provider, ImageLoader {
init {
// From: https://gitlab.com/spacecowboy/Feeder
// Install Conscrypt to handle TLSv1.3 pre Android10
Security.insertProviderAt(Conscrypt.newProvider(), 1)
}
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-04-23 21:39:32 +02:00
@Inject
lateinit var appNetworkDataSource: AppNetworkDataSource
2022-03-01 18:40:53 +01:00
@Inject
lateinit var opmlLocalDataSource: OpmlLocalDataSource
@Inject
lateinit var rssHelper: RssHelper
2022-04-23 21:39:32 +02:00
@Inject
lateinit var appRepository: AppRepository
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-04-03 12:15:28 +02:00
// @Inject
// lateinit var feverRssRepository: FeverRssRepository
2022-03-17 19:11:52 +01:00
2022-03-01 18:40:53 +01:00
@Inject
lateinit var opmlRepository: OpmlRepository
@Inject
lateinit var rssRepository: RssRepository
@Inject
@ApplicationScope
lateinit var applicationScope: CoroutineScope
2022-03-21 15:40:40 +01:00
2022-03-31 20:06:59 +02:00
@Inject
@DispatcherDefault
lateinit var dispatcherDefault: CoroutineDispatcher
2022-03-01 18:40:53 +01:00
override fun onCreate() {
super.onCreate()
2022-04-07 09:05:10 +02:00
CrashHandler(this)
2022-04-13 05:42:55 +02:00
dataStoreInit()
2022-03-31 20:06:59 +02:00
applicationScope.launch(dispatcherDefault) {
2022-03-21 15:40:40 +01:00
accountInit()
workerInit()
2022-05-10 16:38:57 +02:00
if (BuildConfig.FLAVOR != "fdroid") {
checkUpdate()
}
2022-03-21 15:40:40 +01:00
}
}
2022-05-07 12:40:27 +02:00
private fun dataStoreInit() {
}
2022-03-21 15:40:40 +01:00
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() {
rssRepository.get().doSync()
2022-03-21 15:40:40 +01:00
}
2022-04-23 21:39:32 +02:00
private suspend fun checkUpdate() {
2022-04-24 20:21:29 +02:00
applicationContext.getLatestApk().let {
if (it.exists()) {
it.del()
}
}
appRepository.checkUpdate(showToast = false)
2022-04-23 21:39:32 +02:00
}
2022-03-21 15:40:40 +01:00
override fun getWorkManagerConfiguration(): Configuration =
Configuration.Builder()
.setWorkerFactory(workerFactory)
.setMinimumLoggingLevel(android.util.Log.DEBUG)
.build()
override val components: ComponentRegistry
get() = ComponentRegistry.Builder()
.add(SvgDecoder.Factory())
.add(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoderDecoder.Factory()
} else {
GifDecoder.Factory()
}
)
.build()
override val defaults: DefaultRequestOptions
get() = DefaultRequestOptions()
override val diskCache: DiskCache
get() = DiskCache.Builder()
.directory(cacheDir.resolve("images"))
.maxSizePercent(0.02)
.build()
override val memoryCache: MemoryCache
get() = MemoryCache.Builder(this)
.maxSizePercent(0.25)
.build()
override fun enqueue(request: ImageRequest): Disposable {
// Always call onStart before onSuccess.
request.target?.onStart(request.placeholder)
val result = ColorDrawable(Color.BLACK)
request.target?.onSuccess(result)
return object : Disposable {
override val job = CompletableDeferred(newResult(request, result))
override val isDisposed get() = true
override fun dispose() {}
}
}
override suspend fun execute(request: ImageRequest): ImageResult {
return newResult(request, ColorDrawable(Color.BLACK))
}
override fun newBuilder(): ImageLoader.Builder {
throw UnsupportedOperationException()
}
override fun shutdown() {
}
private fun newResult(request: ImageRequest, drawable: Drawable): SuccessResult {
return SuccessResult(
drawable = drawable,
request = request,
dataSource = DataSource.MEMORY_CACHE
)
}
2022-03-01 18:40:53 +01:00
}