refactor(manager): shared preferences

This commit is contained in:
rhunk
2023-10-21 21:46:36 +02:00
parent a82c9d1738
commit 688051fd63
2 changed files with 5 additions and 6 deletions

View File

@@ -111,21 +111,18 @@ class LogManager(
var lineAddListener = { _: LogLine -> }
private val logFolder = File(remoteSideContext.androidContext.cacheDir, "logs")
private val preferences: SharedPreferences
private var logFile: File
init {
if (!logFolder.exists()) {
logFolder.mkdirs()
}
preferences = remoteSideContext.androidContext.getSharedPreferences("logger", 0)
logFile = preferences.getString("log_file", null)?.let { File(it) }?.takeIf { it.exists() } ?: run {
logFile = remoteSideContext.sharedPreferences.getString("log_file", null)?.let { File(it) }?.takeIf { it.exists() } ?: run {
newLogFile()
logFile
}
if (System.currentTimeMillis() - preferences.getLong("last_created", 0) > LOG_LIFETIME.inWholeMilliseconds) {
if (System.currentTimeMillis() - remoteSideContext.sharedPreferences.getLong("last_created", 0) > LOG_LIFETIME.inWholeMilliseconds) {
newLogFile()
}
}
@@ -141,7 +138,7 @@ class LogManager(
logFile = File(logFolder, "snapenhance_${getCurrentDateTime(pathSafe = true)}.log").also {
it.createNewFile()
}
preferences.edit().putString("log_file", logFile.absolutePath).putLong("last_created", currentTime).apply()
remoteSideContext.sharedPreferences.edit().putString("log_file", logFile.absolutePath).putLong("last_created", currentTime).apply()
}
fun clearLogs() {

View File

@@ -3,6 +3,7 @@ package me.rhunk.snapenhance
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
@@ -54,6 +55,7 @@ class RemoteSideContext(
get() = _activity?.get()
set(value) { _activity?.clear(); _activity = WeakReference(value) }
val sharedPreferences: SharedPreferences get() = androidContext.getSharedPreferences("prefs", 0)
val config = ModConfig()
val translation = LocaleWrapper()
val mappings = MappingsWrapper()