From 69b2d504934eab4e69b7ef9a574c6c7560cee54b Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 15 Jun 2020 20:11:12 -0700 Subject: Update AS and add hilt usage --- .../ca/allanwang/kau/sample/KPrefViewTest.kt | 20 +- .../ca/allanwang/kau/sample/SampleTestApp.kt | 56 +- .../kotlin/ca/allanwang/kau/sample/TestModules.kt | 15 + .../kotlin/ca/allanwang/kau/sample/AnimActivity.kt | 7 +- .../kotlin/ca/allanwang/kau/sample/KPrefSample.kt | 26 +- .../kotlin/ca/allanwang/kau/sample/MainActivity.kt | 573 +++++++++++++++++++-- .../kotlin/ca/allanwang/kau/sample/SampleApp.kt | 36 +- 7 files changed, 600 insertions(+), 133 deletions(-) create mode 100644 sample/src/androidTest/kotlin/ca/allanwang/kau/sample/TestModules.kt (limited to 'sample/src') 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 88a0402..91c98e7 100644 --- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt @@ -27,6 +27,10 @@ import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.MediumTest import androidx.test.rule.ActivityTestRule +import dagger.hilt.android.testing.HiltAndroidRule +import dagger.hilt.android.testing.HiltAndroidTest +import dagger.hilt.android.testing.HiltTestApplication +import dagger.hilt.android.testing.UninstallModules import kotlin.test.BeforeTest import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -40,8 +44,7 @@ import org.junit.Test import org.junit.rules.RuleChain import org.junit.rules.TestRule import org.junit.runner.RunWith -import org.koin.test.KoinTest -import org.koin.test.inject +import javax.inject.Inject /** * Created by Allan Wang on 21/12/2018. @@ -49,19 +52,22 @@ import org.koin.test.inject * Tests related to the :kpref-activity module */ @RunWith(AndroidJUnit4::class) -@MediumTest -class KPrefViewTest : KoinTest { +@HiltAndroidTest +@UninstallModules(PrefFactoryModule::class) +class KPrefViewTest { val activity: ActivityTestRule = ActivityTestRule(MainActivity::class.java) + val hiltRule = HiltAndroidRule(this) + @get:Rule - val rule: TestRule = RuleChain.outerRule(SampleTestRule()).around(activity) + val rule: TestRule = RuleChain.outerRule(hiltRule).around(SampleTestRule()).around(activity) - private val pref: KPrefSample by inject() + @Inject lateinit var pref: KPrefSample @BeforeTest fun before() { - pref.reset() + hiltRule.inject() } fun verifyCheck(checked: Boolean): Matcher { diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt index c6ab259..da53ce1 100644 --- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt @@ -17,18 +17,17 @@ package ca.allanwang.kau.sample import android.app.Application import android.content.Context +import androidx.test.core.app.ApplicationProvider import androidx.test.runner.AndroidJUnitRunner -import ca.allanwang.kau.kpref.KPrefFactory -import ca.allanwang.kau.kpref.KPrefFactoryInMemory +import dagger.hilt.EntryPoint +import dagger.hilt.InstallIn +import dagger.hilt.android.EntryPointAccessors +import dagger.hilt.android.components.ApplicationComponent +import dagger.hilt.android.testing.HiltTestApplication import org.junit.rules.TestRule import org.junit.runner.Description import org.junit.runners.model.Statement -import org.koin.android.ext.koin.androidContext -import org.koin.android.ext.koin.androidLogger -import org.koin.core.KoinComponent -import org.koin.core.context.startKoin -import org.koin.core.get -import org.koin.dsl.module +import java.lang.RuntimeException class SampleTestRunner : AndroidJUnitRunner() { override fun newApplication( @@ -36,43 +35,30 @@ class SampleTestRunner : AndroidJUnitRunner() { className: String?, context: Context? ): Application { - return super.newApplication(cl, SampleTestApp::class.java.name, context) + return super.newApplication(cl, HiltTestApplication::class.java.name, context) } } class SampleTestRule : TestRule { + + @EntryPoint + @InstallIn(ApplicationComponent::class) + interface SampleTestRuleEntryPoint { + fun pref(): KPrefSample + } + override fun apply(base: Statement, description: Description): Statement = - object : Statement(), KoinComponent { + object : Statement() { override fun evaluate() { + val entryPoint = EntryPointAccessors.fromApplication( + ApplicationProvider.getApplicationContext(), + SampleTestRuleEntryPoint::class.java + ) // Reset prefs - get().reset() + entryPoint.pref().reset() base.evaluate() } } } - -class SampleTestApp : Application() { - override fun onCreate() { - super.onCreate() - startKoin { - if (BuildConfig.DEBUG) { - androidLogger() - } - androidContext(this@SampleTestApp) - modules( - listOf( - prefFactoryModule(), - KPrefSample.module() - ) - ) - } - } - - fun prefFactoryModule() = module { - single { - KPrefFactoryInMemory - } - } -} diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/TestModules.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/TestModules.kt new file mode 100644 index 0000000..061af50 --- /dev/null +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/TestModules.kt @@ -0,0 +1,15 @@ +package ca.allanwang.kau.sample + +import ca.allanwang.kau.kpref.KPrefFactory +import ca.allanwang.kau.kpref.KPrefFactoryInMemory +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.components.ApplicationComponent + +@Module +@InstallIn(ApplicationComponent::class) +object PrefFactoryTestModule { + @Provides + fun factory(): 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 bd1039b..b3560c5 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt @@ -28,7 +28,8 @@ 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 +import dagger.hilt.android.AndroidEntryPoint +import javax.inject.Inject /** * Created by Allan Wang on 2017-06-12. @@ -36,9 +37,11 @@ import org.koin.android.ext.android.inject * Activity for animations * Now also showcases permissions */ +@AndroidEntryPoint class AnimActivity : KauBaseActivity() { - private val pref: KPrefSample by inject() + @Inject + lateinit var pref: KPrefSample override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) 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 4ac9928..a12c433 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt @@ -15,10 +15,16 @@ */ package ca.allanwang.kau.sample +import android.content.Context import android.graphics.Color import ca.allanwang.kau.kpref.KPref import ca.allanwang.kau.kpref.KPrefFactory -import org.koin.dsl.module +import ca.allanwang.kau.kpref.KPrefFactoryAndroid +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.components.ApplicationComponent +import dagger.hilt.android.qualifiers.ApplicationContext /** * Created by Allan Wang on 2017-06-07. @@ -35,10 +41,18 @@ class KPrefSample(factory: KPrefFactory) : KPref("pref_sample", factory = factor var seekbar: Int by kpref("seekbar", 20) var time12: Int by kpref("time_12", 315) var time24: Int by kpref("time_24", 2220) +} - companion object { - fun module() = module { - single { KPrefSample(get()) } - } - } +@Module +@InstallIn(ApplicationComponent::class) +object PrefModule { + @Provides + fun pref(factory: KPrefFactory): KPrefSample = KPrefSample(factory) } + +@Module +@InstallIn(ApplicationComponent::class) +object PrefFactoryModule { + @Provides + fun factory(@ApplicationContext context: Context): KPrefFactory = KPrefFactoryAndroid(context) +} \ No newline at end of file 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 4cf632f..e6841c8 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt @@ -40,8 +40,10 @@ 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 +import dagger.hilt.android.AndroidEntryPoint +import javax.inject.Inject +@AndroidEntryPoint class MainActivity : KPrefActivity() { var searchView: SearchView? = null @@ -51,58 +53,514 @@ class MainActivity : KPrefActivity() { // some of the most common english words for show val wordBank: List by lazy { listOf( - "the", "name", "of", "very", "to", "through", - "and", "just", "a", "form", "in", "much", "is", "great", "it", "think", "you", "say", - "that", "help", "he", "low", "was", "line", "for", "before", "on", "turn", "are", "cause", - "with", "same", "as", "mean", "I", "differ", "his", "move", "they", "right", "be", "boy", - "at", "old", "one", "too", "have", "does", "this", "tell", "from", "sentence", "or", "set", - "had", "three", "by", "want", "hot", "air", "but", "well", "some", "also", "what", "play", - "there", "small", "we", "end", "can", "put", "out", "home", "other", "read", "were", "hand", - "all", "port", "your", "large", "when", "spell", "up", "add", "use", "even", "word", "land", - "how", "here", "said", "must", "an", "big", "each", "high", "she", "such", "which", "follow", - "do", "act", "their", "why", "time", "ask", "if", "men", "will", "change", "way", "went", - "about", "light", "many", "kind", "then", "off", "them", "need", "would", "house", "write", - "picture", "like", "try", "so", "us", "these", "again", "her", "animal", "long", "point", - "make", "mother", "thing", "world", "see", "near", "him", "build", "two", "self", "has", - "earth", "look", "father", "more", "head", "day", "stand", "could", "own", "go", "page", - "come", "should", "did", "country", "my", "found", "sound", "answer", "no", "school", "most", - "grow", "number", "study", "who", "still", "over", "learn", "know", "plant", "water", "cover", - "than", "food", "call", "sun", "first", "four", "people", "thought", "may", "let", "down", "keep", - "side", "eye", "been", "never", "now", "last", "find", "door", "any", "between", "new", "city", - "work", "tree", "part", "cross", "take", "since", "get", "hard", "place", "start", "made", - "might", "live", "story", "where", "saw", "after", "far", "back", "sea", "little", "draw", - "only", "left", "round", "late", "man", "run", "year", "don't", "came", "while", "show", - "press", "every", "close", "good", "night", "me", "real", "give", "life", "our", "few", "under", - "stopRankWordRankWord", "open", "ten", "seem", "simple", "together", "several", "next", - "vowel", "white", "toward", "children", "war", "begin", "lay", "got", "against", "walk", "pattern", - "example", "slow", "ease", "center", "paper", "love", "often", "person", "always", "money", - "music", "serve", "those", "appear", "both", "road", "mark", "map", "book", "science", "letter", - "rule", "until", "govern", "mile", "pull", "river", "cold", "car", "notice", "feet", "voice", - "care", "fall", "second", "power", "group", "town", "carry", "fine", "took", "certain", "rain", - "fly", "eat", "unit", "room", "lead", "friend", "cry", "began", "dark", "idea", "machine", - "fish", "note", "mountain", "wait", "north", "plan", "once", "figure", "base", "star", "hear", - "box", "horse", "noun", "cut", "field", "sure", "rest", "watch", "correct", "color", "able", - "face", "pound", "wood", "done", "main", "beauty", "enough", "drive", "plain", "stood", "girl", - "contain", "usual", "front", "young", "teach", "ready", "week", "above", "final", "ever", "gave", - "red", "green", "list", "oh", "though", "quick", "feel", "develop", "talk", "sleep", "bird", - "warm", "soon", "free", "body", "minute", "dog", "strong", "family", "special", "direct", "mind", - "pose", "behind", "leave", "clear", "song", "tail", "measure", "produce", "state", "fact", "product", - "street", "black", "inch", "short", "lot", "numeral", "nothing", "class", "course", "wind", "stay", - "question", "wheel", "happen", "full", "complete", "force", "ship", "blue", "area", "object", "half", - "decide", "rock", "surface", "order", "deep", "fire", "moon", "south", "island", "problem", "foot", - "piece", "yet", "told", "busy", "knew", "test", "pass", "record", "farm", "boat", "top", "common", - "whole", "gold", "king", "possible", "size", "plane", "heard", "age", "best", "dry", "hour", "wonder", - "better", "laugh", "true.", "thousand", "during", "ago", "hundred", "ran", "am", "check", "remember", - "game", "step", "shape", "early", "yes", "hold", "hot", "west", "miss", "ground", "brought", "interest", - "heat", "reach", "snow", "fast", "bed", "five", "bring", "sing", "sit", "listen", "perhaps", "six", - "fill", "table", "east", "travel", "weight", "less", "language", "morning", "among" + "the", + "name", + "of", + "very", + "to", + "through", + "and", + "just", + "a", + "form", + "in", + "much", + "is", + "great", + "it", + "think", + "you", + "say", + "that", + "help", + "he", + "low", + "was", + "line", + "for", + "before", + "on", + "turn", + "are", + "cause", + "with", + "same", + "as", + "mean", + "I", + "differ", + "his", + "move", + "they", + "right", + "be", + "boy", + "at", + "old", + "one", + "too", + "have", + "does", + "this", + "tell", + "from", + "sentence", + "or", + "set", + "had", + "three", + "by", + "want", + "hot", + "air", + "but", + "well", + "some", + "also", + "what", + "play", + "there", + "small", + "we", + "end", + "can", + "put", + "out", + "home", + "other", + "read", + "were", + "hand", + "all", + "port", + "your", + "large", + "when", + "spell", + "up", + "add", + "use", + "even", + "word", + "land", + "how", + "here", + "said", + "must", + "an", + "big", + "each", + "high", + "she", + "such", + "which", + "follow", + "do", + "act", + "their", + "why", + "time", + "ask", + "if", + "men", + "will", + "change", + "way", + "went", + "about", + "light", + "many", + "kind", + "then", + "off", + "them", + "need", + "would", + "house", + "write", + "picture", + "like", + "try", + "so", + "us", + "these", + "again", + "her", + "animal", + "long", + "point", + "make", + "mother", + "thing", + "world", + "see", + "near", + "him", + "build", + "two", + "self", + "has", + "earth", + "look", + "father", + "more", + "head", + "day", + "stand", + "could", + "own", + "go", + "page", + "come", + "should", + "did", + "country", + "my", + "found", + "sound", + "answer", + "no", + "school", + "most", + "grow", + "number", + "study", + "who", + "still", + "over", + "learn", + "know", + "plant", + "water", + "cover", + "than", + "food", + "call", + "sun", + "first", + "four", + "people", + "thought", + "may", + "let", + "down", + "keep", + "side", + "eye", + "been", + "never", + "now", + "last", + "find", + "door", + "any", + "between", + "new", + "city", + "work", + "tree", + "part", + "cross", + "take", + "since", + "get", + "hard", + "place", + "start", + "made", + "might", + "live", + "story", + "where", + "saw", + "after", + "far", + "back", + "sea", + "little", + "draw", + "only", + "left", + "round", + "late", + "man", + "run", + "year", + "don't", + "came", + "while", + "show", + "press", + "every", + "close", + "good", + "night", + "me", + "real", + "give", + "life", + "our", + "few", + "under", + "stopRankWordRankWord", + "open", + "ten", + "seem", + "simple", + "together", + "several", + "next", + "vowel", + "white", + "toward", + "children", + "war", + "begin", + "lay", + "got", + "against", + "walk", + "pattern", + "example", + "slow", + "ease", + "center", + "paper", + "love", + "often", + "person", + "always", + "money", + "music", + "serve", + "those", + "appear", + "both", + "road", + "mark", + "map", + "book", + "science", + "letter", + "rule", + "until", + "govern", + "mile", + "pull", + "river", + "cold", + "car", + "notice", + "feet", + "voice", + "care", + "fall", + "second", + "power", + "group", + "town", + "carry", + "fine", + "took", + "certain", + "rain", + "fly", + "eat", + "unit", + "room", + "lead", + "friend", + "cry", + "began", + "dark", + "idea", + "machine", + "fish", + "note", + "mountain", + "wait", + "north", + "plan", + "once", + "figure", + "base", + "star", + "hear", + "box", + "horse", + "noun", + "cut", + "field", + "sure", + "rest", + "watch", + "correct", + "color", + "able", + "face", + "pound", + "wood", + "done", + "main", + "beauty", + "enough", + "drive", + "plain", + "stood", + "girl", + "contain", + "usual", + "front", + "young", + "teach", + "ready", + "week", + "above", + "final", + "ever", + "gave", + "red", + "green", + "list", + "oh", + "though", + "quick", + "feel", + "develop", + "talk", + "sleep", + "bird", + "warm", + "soon", + "free", + "body", + "minute", + "dog", + "strong", + "family", + "special", + "direct", + "mind", + "pose", + "behind", + "leave", + "clear", + "song", + "tail", + "measure", + "produce", + "state", + "fact", + "product", + "street", + "black", + "inch", + "short", + "lot", + "numeral", + "nothing", + "class", + "course", + "wind", + "stay", + "question", + "wheel", + "happen", + "full", + "complete", + "force", + "ship", + "blue", + "area", + "object", + "half", + "decide", + "rock", + "surface", + "order", + "deep", + "fire", + "moon", + "south", + "island", + "problem", + "foot", + "piece", + "yet", + "told", + "busy", + "knew", + "test", + "pass", + "record", + "farm", + "boat", + "top", + "common", + "whole", + "gold", + "king", + "possible", + "size", + "plane", + "heard", + "age", + "best", + "dry", + "hour", + "wonder", + "better", + "laugh", + "true.", + "thousand", + "during", + "ago", + "hundred", + "ran", + "am", + "check", + "remember", + "game", + "step", + "shape", + "early", + "yes", + "hold", + "hot", + "west", + "miss", + "ground", + "brought", + "interest", + "heat", + "reach", + "snow", + "fast", + "bed", + "five", + "bring", + "sing", + "sit", + "listen", + "perhaps", + "six", + "fill", + "table", + "east", + "travel", + "weight", + "less", + "language", + "morning", + "among" ) } const val REQUEST_MEDIA = 27 } - private val pref: KPrefSample by inject() + @Inject + lateinit var pref: KPrefSample override fun kPrefCoreAttributes(): CoreAttributeContract.() -> Unit = { textColor = pref::textColor @@ -167,7 +625,12 @@ class MainActivity : KPrefActivity() { onClick = { itemView.context.materialDialog { title(text = "Type Text") - input("Type here", prefill = item.pref, maxLength = 20, allowEmpty = true) { _, input -> + input( + "Type here", + prefill = item.pref, + maxLength = 20, + allowEmpty = true + ) { _, input -> item.pref = input.toString() } } @@ -246,7 +709,12 @@ class MainActivity : KPrefActivity() { onClick = { itemView.context.materialDialog { title(text = "Type Text") - input("Type here", prefill = item.pref, maxLength = 20, allowEmpty = true) { _, input -> + input( + "Type here", + prefill = item.pref, + maxLength = 20, + allowEmpty = true + ) { _, input -> item.pref = input.toString() reloadSelf() } @@ -311,7 +779,12 @@ class MainActivity : KPrefActivity() { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { - REQUEST_MEDIA -> toast("${kauOnMediaPickerResult(resultCode, data).size} items selected") + REQUEST_MEDIA -> toast( + "${kauOnMediaPickerResult( + resultCode, + data + ).size} items selected" + ) } } } 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 597894a..282f9ff 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/SampleApp.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/SampleApp.kt @@ -16,40 +16,10 @@ 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 +import dagger.hilt.android.HiltAndroidApp /** * Created by Allan Wang on 2017-06-08. */ -class SampleApp : Application() { - override fun onCreate() { - super.onCreate() - - startKoin { - if (BuildConfig.DEBUG) { - androidLogger() - } - androidContext(this@SampleApp) - modules( - listOf( - prefFactoryModule(), - KPrefSample.module() - ) - ) - } - } - - companion object { - fun prefFactoryModule(): Module = module { - single { - KPrefFactoryAndroid(get()) - } - } - } -} +@HiltAndroidApp +class SampleApp : Application() -- cgit v1.2.3