aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2020-02-18 01:37:32 -0800
committerAllan Wang <me@allanwang.ca>2020-02-18 01:37:32 -0800
commitce0ae639188dad9ca212667c59131c04de1ed575 (patch)
tree887d11c6f500226e7e8e23bd6c12db05d4d1cdc1
parentd66335b74b313c1d7a00059d7220303cb66534fc (diff)
downloadkau-ce0ae639188dad9ca212667c59131c04de1ed575.tar.gz
kau-ce0ae639188dad9ca212667c59131c04de1ed575.tar.bz2
kau-ce0ae639188dad9ca212667c59131c04de1ed575.zip
Add koin modules for pref injection
-rw-r--r--sample/build.gradle3
-rw-r--r--sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt40
-rw-r--r--sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt13
-rw-r--r--sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt5
-rw-r--r--sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt3
-rw-r--r--sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt53
-rw-r--r--sample/src/main/kotlin/ca/allanwang/kau/sample/SampleApp.kt30
7 files changed, 110 insertions, 37 deletions
diff --git a/sample/build.gradle b/sample/build.gradle
index 0ed7818..2bd5ba8 100644
--- a/sample/build.gradle
+++ b/sample/build.gradle
@@ -137,9 +137,11 @@ dependencies {
implementation project(':mediapicker')
implementation Dependencies.materialDialog("input")
+ implementation Dependencies.koin
testImplementation Dependencies.kotlinTest
testImplementation Dependencies.junit
+ testImplementation Dependencies.koinTest
androidTestImplementation Dependencies.kotlinTest
androidTestImplementation Dependencies.espresso
@@ -147,4 +149,5 @@ dependencies {
androidTestImplementation Dependencies.espresso("contrib")
androidTestImplementation Dependencies.testRules
androidTestImplementation Dependencies.testRunner
+ androidTestImplementation Dependencies.koinTest
}
diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt
index 72199cf..5847a94 100644
--- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt
+++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt
@@ -26,6 +26,7 @@ import androidx.test.espresso.matcher.ViewMatchers.withChild
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
+import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import org.hamcrest.BaseMatcher
import org.hamcrest.Description
@@ -35,6 +36,15 @@ import org.hamcrest.Matchers.instanceOf
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
+import org.koin.android.ext.koin.androidContext
+import org.koin.core.context.startKoin
+import org.koin.core.context.stopKoin
+import org.koin.test.KoinTest
+import org.koin.test.inject
+import kotlin.test.AfterTest
+import kotlin.test.BeforeTest
+import kotlin.test.assertFalse
+import kotlin.test.assertTrue
/**
* Created by Allan Wang on 21/12/2018.
@@ -43,11 +53,26 @@ import org.junit.runner.RunWith
*/
@RunWith(AndroidJUnit4::class)
@MediumTest
-class KPrefViewTest {
+class KPrefViewTest : KoinTest {
@get:Rule
val activity: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)
+ @BeforeTest
+ fun before() {
+ startKoin {
+ androidContext(InstrumentationRegistry.getInstrumentation().context)
+ modules(listOf(SampleApp.prefModule(), SampleTestApp.prefFactoryModule()))
+ }
+ }
+
+ @AfterTest
+ fun after() {
+ stopKoin()
+ }
+
+ private val pref: KPrefSample by inject()
+
fun verifyCheck(checked: Boolean): Matcher<View> {
return object : BoundedMatcher<View, View>(View::class.java) {
@@ -93,11 +118,11 @@ class KPrefViewTest {
fun basicCheckboxToggle() {
val checkbox1 = onCheckboxView(withChild(withText(R.string.checkbox_1)))
- val initiallyChecked = KPrefSample.check1
+ assertTrue(pref.check1, "check1 not normalized")
- checkbox1.verifyCheck("checkbox1 init", initiallyChecked)
+ checkbox1.verifyCheck("checkbox1 init", true)
checkbox1.perform(click())
- checkbox1.verifyCheck("checkbox1 after click", !initiallyChecked)
+ checkbox1.verifyCheck("checkbox1 after click", false)
}
/**
@@ -109,11 +134,8 @@ class KPrefViewTest {
val checkbox3 =
onCheckboxView(withChild(withText(R.string.checkbox_3)), withChild(withText(R.string.desc_dependent)))
- // normalize so that both are checked
- if (!KPrefSample.check2)
- checkbox2.perform(click())
- if (!KPrefSample.check3)
- checkbox3.perform(click())
+ assertFalse(pref.check2, "check2 not normalized")
+ assertFalse(pref.check3, "check3 not normalized")
checkbox3.verifyCheck("checkbox3 init", true, true)
checkbox3.perform(click())
diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt
new file mode 100644
index 0000000..4b0c4a7
--- /dev/null
+++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt
@@ -0,0 +1,13 @@
+package ca.allanwang.kau.sample
+
+import ca.allanwang.kau.kpref.KPrefFactory
+import ca.allanwang.kau.kpref.KPrefFactoryInMemory
+import org.koin.dsl.module
+
+object SampleTestApp {
+ fun prefFactoryModule() = module {
+ single<KPrefFactory> {
+ KPrefFactoryInMemory
+ }
+ }
+} \ No newline at end of file
diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt
index a472f09..bd1039b 100644
--- a/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt
+++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt
@@ -28,6 +28,7 @@ import ca.allanwang.kau.utils.fullLinearRecycler
import ca.allanwang.kau.utils.startActivity
import ca.allanwang.kau.utils.withAlpha
import ca.allanwang.kau.utils.withSlideOut
+import org.koin.android.ext.android.inject
/**
* Created by Allan Wang on 2017-06-12.
@@ -37,12 +38,14 @@ import ca.allanwang.kau.utils.withSlideOut
*/
class AnimActivity : KauBaseActivity() {
+ private val pref: KPrefSample by inject()
+
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val adapter = SingleFastAdapter()
setContentView(fullLinearRecycler(adapter).apply {
setBackgroundColor(
- KPrefSample.bgColor.withAlpha(255)
+ pref.bgColor.withAlpha(255)
)
})
diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt
index 80a75bf..97e8075 100644
--- a/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt
+++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt
@@ -17,11 +17,12 @@ package ca.allanwang.kau.sample
import android.graphics.Color
import ca.allanwang.kau.kpref.KPref
+import ca.allanwang.kau.kpref.KPrefFactory
/**
* Created by Allan Wang on 2017-06-07.
*/
-object KPrefSample : KPref() {
+class KPrefSample(factory: KPrefFactory) : KPref("pref_sample", factory = factory) {
var version: Int by kpref("version", -1)
var textColor: Int by kpref("TEXT_COLOR", Color.WHITE)
var accentColor: Int by kpref("ACCENT_COLOR", 0xffff8900.toInt())
diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
index 4fe941b..4cf632f 100644
--- a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
+++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
@@ -40,6 +40,7 @@ import ca.allanwang.kau.utils.withSceneTransitionAnimation
import ca.allanwang.kau.xml.showChangelog
import com.afollestad.materialdialogs.input.input
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
+import org.koin.android.ext.android.inject
class MainActivity : KPrefActivity() {
@@ -101,9 +102,11 @@ class MainActivity : KPrefActivity() {
const val REQUEST_MEDIA = 27
}
+ private val pref: KPrefSample by inject()
+
override fun kPrefCoreAttributes(): CoreAttributeContract.() -> Unit = {
- textColor = KPrefSample::textColor
- accentColor = KPrefSample::accentColor
+ textColor = pref::textColor
+ accentColor = pref::accentColor
}
override fun onCreateKPrefs(savedInstanceState: Bundle?): KPrefAdapterBuilder.() -> Unit = {
@@ -113,7 +116,7 @@ class MainActivity : KPrefActivity() {
/**
* This is how the setup looks like with all the proper tags
*/
- checkbox(title = R.string.checkbox_1, getter = KPrefSample::check1, setter = { KPrefSample.check1 = it },
+ checkbox(title = R.string.checkbox_1, getter = pref::check1, setter = { pref.check1 = it },
builder = {
descRes = R.string.desc
})
@@ -123,25 +126,25 @@ class MainActivity : KPrefActivity() {
*/
checkbox(
R.string.checkbox_2,
- KPrefSample::check2,
- { KPrefSample.check2 = it; reloadByTitle(R.string.checkbox_3) })
+ pref::check2,
+ { pref.check2 = it; reloadByTitle(R.string.checkbox_3) })
/**
* Since the builder is the last argument and is a lambda, we may write the setup cleanly like so:
*/
- checkbox(R.string.checkbox_3, KPrefSample::check3, { KPrefSample.check3 = it }) {
+ checkbox(R.string.checkbox_3, pref::check3, { pref.check3 = it }) {
descRes = R.string.desc_dependent
- enabler = { KPrefSample.check2 }
+ enabler = { pref.check2 }
onDisabledClick = { itemView.context.toast("I am still disabled") }
}
- colorPicker(R.string.text_color, KPrefSample::textColor, { KPrefSample.textColor = it; reload() }) {
+ colorPicker(R.string.text_color, pref::textColor, { pref.textColor = it; reload() }) {
descRes = R.string.color_custom
allowCustom = true
}
- colorPicker(R.string.accent_color, KPrefSample::accentColor, {
- KPrefSample.accentColor = it
+ colorPicker(R.string.accent_color, pref::accentColor, {
+ pref.accentColor = it
reload()
this@MainActivity.navigationBarColor = it
toolbarCanvas.ripple(it, RippleCanvas.MIDDLE, RippleCanvas.END, duration = 500L)
@@ -150,8 +153,8 @@ class MainActivity : KPrefActivity() {
allowCustom = false
}
- colorPicker(R.string.background_color, KPrefSample::bgColor, {
- KPrefSample.bgColor = it; bgCanvas.ripple(it, duration = 500L)
+ colorPicker(R.string.background_color, pref::bgColor, {
+ pref.bgColor = it; bgCanvas.ripple(it, duration = 500L)
}) {
iicon = GoogleMaterial.Icon.gmd_colorize
descRes = R.string.color_custom_alpha
@@ -159,7 +162,7 @@ class MainActivity : KPrefActivity() {
allowCustom = true
}
- text(R.string.text, KPrefSample::text, { KPrefSample.text = it }) {
+ text(R.string.text, pref::text, { pref.text = it }) {
descRes = R.string.text_desc
onClick = {
itemView.context.materialDialog {
@@ -171,7 +174,7 @@ class MainActivity : KPrefActivity() {
}
}
- seekbar(R.string.seekbar, KPrefSample::seekbar, { KPrefSample.seekbar = it }) {
+ seekbar(R.string.seekbar, pref::seekbar, { pref.seekbar = it }) {
descRes = R.string.kau_lorem_ipsum
increments = 5
textViewConfigs = {
@@ -215,30 +218,30 @@ class MainActivity : KPrefActivity() {
*/
checkbox(
R.string.checkbox_2,
- KPrefSample::check2,
- { KPrefSample.check2 = it; reloadByTitle(R.string.checkbox_3) }) {
+ pref::check2,
+ { pref.check2 = it; reloadByTitle(R.string.checkbox_3) }) {
titleFun = { R.string.checkbox_3 }
descRes = R.string.kau_lorem_ipsum
}
- text(R.string.text, KPrefSample::text, { KPrefSample.text = it }) {
+ text(R.string.text, pref::text, { pref.text = it }) {
descRes = R.string.kau_lorem_ipsum
textGetter = { string(R.string.kau_lorem_ipsum) }
}
- timePicker(R.string.time, KPrefSample::time12, { KPrefSample.time12 = it }) {
+ timePicker(R.string.time, pref::time12, { pref.time12 = it }) {
descRes = R.string.time_desc_12
use24HourFormat = false
}
- timePicker(R.string.time, KPrefSample::time24, { KPrefSample.time24 = it }) {
+ timePicker(R.string.time, pref::time24, { pref.time24 = it }) {
descRes = R.string.time_desc_24
use24HourFormat = true
}
}
fun subPrefs(): KPrefAdapterBuilder.() -> Unit = {
- text(R.string.text, { KPrefSample.text }, { KPrefSample.text = it }) {
+ text(R.string.text, { pref.text }, { pref.text = it }) {
descRes = R.string.text_desc
onClick = {
itemView.context.materialDialog {
@@ -254,11 +257,11 @@ class MainActivity : KPrefActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
- bgCanvas.set(KPrefSample.bgColor)
- toolbarCanvas.set(KPrefSample.accentColor)
- this.navigationBarColor = KPrefSample.accentColor
- if (KPrefSample.version < BuildConfig.VERSION_CODE) {
- KPrefSample.version = BuildConfig.VERSION_CODE
+ bgCanvas.set(pref.bgColor)
+ toolbarCanvas.set(pref.accentColor)
+ this.navigationBarColor = pref.accentColor
+ if (pref.version < BuildConfig.VERSION_CODE) {
+ pref.version = BuildConfig.VERSION_CODE
if (!BuildConfig.DEBUG)
showChangelog(R.xml.kau_changelog)
}
diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/SampleApp.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/SampleApp.kt
index 6e6d718..2537090 100644
--- a/sample/src/main/kotlin/ca/allanwang/kau/sample/SampleApp.kt
+++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/SampleApp.kt
@@ -16,6 +16,13 @@
package ca.allanwang.kau.sample
import android.app.Application
+import ca.allanwang.kau.kpref.KPrefFactory
+import ca.allanwang.kau.kpref.KPrefFactoryAndroid
+import org.koin.android.ext.koin.androidContext
+import org.koin.android.ext.koin.androidLogger
+import org.koin.core.context.startKoin
+import org.koin.core.module.Module
+import org.koin.dsl.module
/**
* Created by Allan Wang on 2017-06-08.
@@ -23,6 +30,27 @@ import android.app.Application
class SampleApp : Application() {
override fun onCreate() {
super.onCreate()
- KPrefSample.initialize(this, "pref_sample")
+
+ startKoin {
+ if (BuildConfig.DEBUG) {
+ androidLogger()
+ }
+ androidContext(this@SampleApp)
+ modules(listOf(prefFactoryModule(), prefModule()))
+ }
+ }
+
+ companion object {
+ fun prefFactoryModule(): Module = module {
+ single<KPrefFactory> {
+ KPrefFactoryAndroid(get())
+ }
+ }
+
+ fun prefModule(): Module = module {
+ single {
+ KPrefSample(get())
+ }
+ }
}
}