From f0616bae1ef042b05225022aacea2ff4d33ac183 Mon Sep 17 00:00:00 2001 From: lhwdev Date: Tue, 10 Nov 2020 21:55:38 +0900 Subject: [PATCH] Changed preferenceState model; fixed bug --- app/build.gradle | 6 ++-- .../com/lhwdev/selfTestMacro/AlarmReceiver.kt | 3 -- .../com/lhwdev/selfTestMacro/FirstActivity.kt | 3 +- .../com/lhwdev/selfTestMacro/MainActivity.kt | 3 +- .../java/com/lhwdev/selfTestMacro/utils.kt | 25 +++++++++++-- build.gradle | 36 +++++++++---------- gradle/wrapper/gradle-wrapper.properties | 2 +- 7 files changed, 47 insertions(+), 31 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 827258a2..4a0b9251 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,14 +8,14 @@ plugins { android { compileSdkVersion 30 - buildToolsVersion "29.0.3" + buildToolsVersion "30.0.2" defaultConfig { applicationId "com.lhwdev.selfTestMacro" minSdkVersion 19 targetSdkVersion 30 - versionCode 1003 - versionName "2.3" + versionCode 1004 + versionName "2.4" multiDexEnabled true diff --git a/app/src/main/java/com/lhwdev/selfTestMacro/AlarmReceiver.kt b/app/src/main/java/com/lhwdev/selfTestMacro/AlarmReceiver.kt index 04d05085..2656657e 100644 --- a/app/src/main/java/com/lhwdev/selfTestMacro/AlarmReceiver.kt +++ b/app/src/main/java/com/lhwdev/selfTestMacro/AlarmReceiver.kt @@ -13,9 +13,6 @@ class AlarmReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val result = goAsync() - if(!isPreferenceInitialized) - preferenceState = PreferenceState(context.prefMain()) - runBlocking { // TODO: is this okay? context.submitSuspend() result.finish() diff --git a/app/src/main/java/com/lhwdev/selfTestMacro/FirstActivity.kt b/app/src/main/java/com/lhwdev/selfTestMacro/FirstActivity.kt index 92501955..0c9e851f 100644 --- a/app/src/main/java/com/lhwdev/selfTestMacro/FirstActivity.kt +++ b/app/src/main/java/com/lhwdev/selfTestMacro/FirstActivity.kt @@ -29,9 +29,8 @@ class FirstActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_first) window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) - val pref = PreferenceState(prefMain()) + val pref = preferenceState val first = intent.hasExtra("first") - preferenceState = pref setSupportActionBar(toolbar) diff --git a/app/src/main/java/com/lhwdev/selfTestMacro/MainActivity.kt b/app/src/main/java/com/lhwdev/selfTestMacro/MainActivity.kt index bff40263..5231edfa 100644 --- a/app/src/main/java/com/lhwdev/selfTestMacro/MainActivity.kt +++ b/app/src/main/java/com/lhwdev/selfTestMacro/MainActivity.kt @@ -40,8 +40,7 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val pref = PreferenceState(prefMain()) - preferenceState = pref + val pref = preferenceState val isFirst = pref.firstState == 0 if(isFirst && !intent.getBooleanExtra("doneFirst", false)) { diff --git a/app/src/main/java/com/lhwdev/selfTestMacro/utils.kt b/app/src/main/java/com/lhwdev/selfTestMacro/utils.kt index 1f109d29..f755b7e9 100644 --- a/app/src/main/java/com/lhwdev/selfTestMacro/utils.kt +++ b/app/src/main/java/com/lhwdev/selfTestMacro/utils.kt @@ -26,6 +26,7 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.StringFormat import kotlinx.serialization.json.Json import org.json.JSONObject +import java.util.WeakHashMap import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty @@ -90,9 +91,29 @@ class PreferenceState(val pref: SharedPreferences) { } } -lateinit var preferenceState: PreferenceState -val isPreferenceInitialized get() = ::preferenceState.isInitialized + +// I knew that global things are bad in android(i waz lazy), but didn't know would be by far worst. +// Only one line below caused TWO bugs; I WON'T do like this in the future +// +// but, some decent ways to do this? +// 1. always passing it through argument: so complicated +// 2. keeping global with the initialization of Application; here [MainApplication] +// 3. passing via argument, but through extension receiver +// 4. like Ambient? : unsafe(though safer in Jetpack Compose) +// 5. ThreadLocal: what else from the original one + +//lateinit var preferenceState: PreferenceState +//val isPreferenceInitialized get() = ::preferenceState.isInitialized + +// ok, maybe decent way; pooling from cache +private val preferenceStateMap = WeakHashMap() + +val Context.preferenceState: PreferenceState + get() = preferenceStateMap.getOrPut(applicationContext) { + PreferenceState(prefMain()) + } + fun SharedPreferences.preferenceInt(key: String, defaultValue: Int) = diff --git a/build.gradle b/build.gradle index 7f208f5c..52bf853a 100644 --- a/build.gradle +++ b/build.gradle @@ -1,30 +1,30 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.4.10' - repositories { - google() - jcenter() - maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' } - } - dependencies { - classpath 'com.android.tools.build:gradle:4.2.0-alpha09' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + ext.kotlin_version = '1.4.10' + repositories { + google() + jcenter() + maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' } + } + dependencies { + classpath 'com.android.tools.build:gradle:4.2.0-alpha16' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.3.1' - classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" - + classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" + // NOTE: Do not place your application dependencies here; they belong - // in the individual module build.gradle files - } + // in the individual module build.gradle files + } } allprojects { - repositories { - google() - jcenter() - } + repositories { + google() + jcenter() + } } task clean(type: Delete) { - delete rootProject.buildDir + delete rootProject.buildDir } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a4d9c70f..91294729 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Thu Aug 06 12:23:16 KST 2020 distributionBase=GRADLE_USER_HOME -distributionUrl=https://services.gradle.org/distributions/gradle-6.6-rc-6-bin.zip +distributionUrl=https://services.gradle.org/distributions/gradle-6.7-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME