Skip to content

Commit be309e1

Browse files
ArnyminerZrfc2822
andauthored
Rewrite BatteryOptimizationsFragment to Compose (#580)
* Migrated to Jetpack Compose Signed-off-by: Arnau Mora <[email protected]> * Added `observeBoolean` Signed-off-by: Arnau Mora <[email protected]> * Simplified settings interaction Signed-off-by: Arnau Mora Gras <[email protected]> * Migrated to Jetpack Compose Signed-off-by: Arnau Mora <[email protected]> * Added `observeBoolean` Signed-off-by: Arnau Mora <[email protected]> * Simplified settings interaction Signed-off-by: Arnau Mora Gras <[email protected]> * Use SafeAndroidUriHandler instead of UiUtils.launchUri * Removed animation for manufacturerWarning Signed-off-by: Arnau Mora Gras <[email protected]> * Removed animation for manufacturerWarning Signed-off-by: Arnau Mora Gras <[email protected]> * Added `getBooleanLive` Signed-off-by: Arnau Mora <[email protected]> * Using `getBooleanLive` Signed-off-by: Arnau Mora <[email protected]> * Moved UI definitions to file scope Signed-off-by: Arnau Mora Gras <[email protected]> * Don't use specific times for waiting in tests * Renamed function Signed-off-by: Arnau Mora Gras <[email protected]> * More exact naming --------- Signed-off-by: Arnau Mora <[email protected]> Signed-off-by: Arnau Mora Gras <[email protected]> Co-authored-by: Ricki Hirner <[email protected]>
1 parent 6b1367d commit be309e1

File tree

4 files changed

+329
-218
lines changed

4 files changed

+329
-218
lines changed

app/src/androidTest/kotlin/at/bitfire/davdroid/settings/SettingsManagerTest.kt

+57-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@
44

55
package at.bitfire.davdroid.settings
66

7+
import at.bitfire.davdroid.TestUtils.getOrAwaitValue
78
import dagger.hilt.android.testing.HiltAndroidRule
89
import dagger.hilt.android.testing.HiltAndroidTest
10+
import kotlinx.coroutines.CompletableDeferred
11+
import kotlinx.coroutines.Dispatchers
12+
import kotlinx.coroutines.runBlocking
13+
import org.junit.After
914
import org.junit.Assert.assertEquals
1015
import org.junit.Assert.assertFalse
16+
import org.junit.Assert.assertNull
17+
import org.junit.Assert.assertTrue
1118
import org.junit.Before
1219
import org.junit.Rule
1320
import org.junit.Test
@@ -16,6 +23,12 @@ import javax.inject.Inject
1623
@HiltAndroidTest
1724
class SettingsManagerTest {
1825

26+
companion object {
27+
/** Use this setting to test SettingsManager methods. Will be removed after every test run. */
28+
const val SETTING_TEST = "test"
29+
}
30+
31+
1932
@get:Rule
2033
val hiltRule = HiltAndroidRule(this)
2134

@@ -26,16 +39,58 @@ class SettingsManagerTest {
2639
hiltRule.inject()
2740
}
2841

42+
@After
43+
fun removeTestSetting() {
44+
settingsManager.remove(SETTING_TEST)
45+
}
46+
2947

3048
@Test
31-
fun testContainsKey_NotExisting() {
49+
fun test_containsKey_NotExisting() {
3250
assertFalse(settingsManager.containsKey("notExisting"))
3351
}
3452

3553
@Test
36-
fun testContainsKey_Existing() {
54+
fun test_containsKey_Existing() {
3755
// provided by DefaultsProvider
3856
assertEquals(Settings.PROXY_TYPE_SYSTEM, settingsManager.getInt(Settings.PROXY_TYPE))
3957
}
4058

59+
60+
@Test
61+
fun test_getBooleanLive_getValue() = runBlocking(Dispatchers.Main) { // observeForever can't be run in background thread
62+
val live = settingsManager.getBooleanLive(SETTING_TEST)
63+
assertNull(live.value)
64+
65+
// set value
66+
settingsManager.putBoolean(SETTING_TEST, true)
67+
assertTrue(live.getOrAwaitValue()!!)
68+
69+
// set another value
70+
live.value = false
71+
assertFalse(live.getOrAwaitValue()!!)
72+
}
73+
74+
75+
@Test
76+
fun test_ObserverCalledWhenValueChanges() {
77+
val value = CompletableDeferred<Int>()
78+
val observer = SettingsManager.OnChangeListener {
79+
value.complete(settingsManager.getInt(SETTING_TEST))
80+
}
81+
82+
try {
83+
settingsManager.addOnChangeListener(observer)
84+
settingsManager.putInt(SETTING_TEST, 123)
85+
86+
runBlocking {
87+
// wait until observer is called
88+
assertEquals(123, value.await())
89+
}
90+
91+
} finally {
92+
settingsManager.removeOnChangeListener(observer)
93+
}
94+
}
95+
4196
}

app/src/main/kotlin/at/bitfire/davdroid/settings/SettingsManager.kt

+35-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ package at.bitfire.davdroid.settings
77
import android.content.Context
88
import android.util.NoSuchPropertyException
99
import androidx.annotation.AnyThread
10+
import androidx.lifecycle.MutableLiveData
1011
import at.bitfire.davdroid.log.Logger
12+
import at.bitfire.davdroid.settings.SettingsManager.OnChangeListener
1113
import dagger.Module
1214
import dagger.Provides
1315
import dagger.hilt.EntryPoint
@@ -170,6 +172,38 @@ class SettingsManager internal constructor(
170172
fun remove(key: String) = putString(key, null)
171173

172174

175+
/*** LIVE DATA ***/
176+
177+
/**
178+
* Returns a [MutableLiveData] which is backed by the settings with the given key.
179+
* An observer must be added to the returned [MutableLiveData] to make it active.
180+
*/
181+
fun getBooleanLive(key: String) = object : MutableLiveData<Boolean?>() {
182+
private val preferenceChangeListener = OnChangeListener { updateValue() }
183+
184+
private fun updateValue() {
185+
value = getBooleanOrNull(key)
186+
}
187+
188+
// setValue is also called from postValue, so no need to override
189+
override fun setValue(value: Boolean?) {
190+
super.setValue(value)
191+
putBoolean(key, value)
192+
}
193+
194+
override fun onActive() {
195+
super.onActive()
196+
updateValue()
197+
addOnChangeListener(preferenceChangeListener)
198+
}
199+
200+
override fun onInactive() {
201+
super.onInactive()
202+
removeOnChangeListener(preferenceChangeListener)
203+
}
204+
}
205+
206+
173207
/*** HELPERS ***/
174208

175209
fun dump(writer: Writer) {
@@ -180,7 +214,7 @@ class SettingsManager internal constructor(
180214
}
181215

182216

183-
interface OnChangeListener {
217+
fun interface OnChangeListener {
184218
/**
185219
* Will be called when something has changed in a [SettingsProvider].
186220
* May run in worker thread!

0 commit comments

Comments
 (0)