Skip to content

Commit

Permalink
Changed preferenceState model; fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lhwdev committed Nov 10, 2020
1 parent d20abbe commit f0616ba
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/com/lhwdev/selfTestMacro/AlarmReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/lhwdev/selfTestMacro/FirstActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/lhwdev/selfTestMacro/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
25 changes: 23 additions & 2 deletions app/src/main/java/com/lhwdev/selfTestMacro/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<Context, PreferenceState>()

val Context.preferenceState: PreferenceState
get() = preferenceStateMap.getOrPut(applicationContext) {
PreferenceState(prefMain())
}



fun SharedPreferences.preferenceInt(key: String, defaultValue: Int) =
Expand Down
36 changes: 18 additions & 18 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f0616ba

Please sign in to comment.