diff --git a/app/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt b/app/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt index 091836f09..82f746fe2 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt @@ -1,24 +1,35 @@ package com.vitorpamplona.amethyst import android.app.Application +import android.content.Context import android.os.StrictMode import android.os.StrictMode.ThreadPolicy import android.os.StrictMode.VmPolicy import android.util.Log import coil.ImageLoader +import coil.disk.DiskCache import com.vitorpamplona.amethyst.service.playback.VideoCache import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch +import java.io.File import kotlin.time.measureTimedValue class Amethyst : Application() { val videoCache: VideoCache by lazy { val newCache = VideoCache() - newCache.initFileCache(instance) + newCache.initFileCache(this) newCache } + private val imageCache: DiskCache by lazy { + DiskCache.Builder() + .directory(applicationContext.safeCacheDir.resolve("image_cache")) + .maxSizePercent(0.2) + .maximumMaxSizeBytes(500L * 1024 * 1024) // 250MB + .build() + } + override fun onCreate() { super.onCreate() instance = this @@ -48,9 +59,7 @@ class Amethyst : Application() { } fun imageLoaderBuilder(): ImageLoader.Builder { - return ImageLoader.Builder(applicationContext).diskCache { - SingletonDiskCache.get(applicationContext) - } + return ImageLoader.Builder(applicationContext).diskCache { imageCache } } companion object { @@ -58,3 +67,9 @@ class Amethyst : Application() { private set } } + +internal val Context.safeCacheDir: File + get() { + val cacheDir = checkNotNull(cacheDir) { "cacheDir == null" } + return cacheDir.apply { mkdirs() } + } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ServiceManager.kt b/app/src/main/java/com/vitorpamplona/amethyst/ServiceManager.kt index 6a4a4edda..414ba56d3 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ServiceManager.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ServiceManager.kt @@ -189,28 +189,3 @@ object ServiceManager { forceRestart(null, false, true) } } - -object SingletonDiskCache { - - private const val DIRECTORY = "image_cache" - private var instance: DiskCache? = null - - @Synchronized - fun get(context: Context): DiskCache { - return instance ?: run { - // Create the singleton disk cache instance. - DiskCache.Builder() - .directory(context.safeCacheDir.resolve(DIRECTORY)) - .maxSizePercent(0.2) - .maximumMaxSizeBytes(500L * 1024 * 1024) // 250MB - .build() - .also { instance = it } - } - } -} - -internal val Context.safeCacheDir: File - get() { - val cacheDir = checkNotNull(cacheDir) { "cacheDir == null" } - return cacheDir.apply { mkdirs() } - } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index d04db033e..1677e37d6 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -5,7 +5,7 @@ import android.graphics.drawable.Drawable import android.util.Log import androidx.compose.runtime.Immutable import androidx.compose.runtime.Stable -import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.mutableIntStateOf import androidx.lifecycle.LiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider @@ -85,7 +85,7 @@ class ResourceToastMsg(val titleResId: Int, val resourceId: Int) : ToastMsg() class AccountViewModel(val account: Account, val settings: SettingsState) : ViewModel(), Dao { val accountLiveData: LiveData = account.live.map { it } val accountLanguagesLiveData: LiveData = account.liveLanguages.map { it } - val accountMarkAsReadUpdates = mutableStateOf(0) + val accountMarkAsReadUpdates = mutableIntStateOf(0) val userFollows: LiveData = account.userProfile().live().follows.map { it } val userRelays: LiveData = account.userProfile().live().relays.map { it }