From 5fb07e0508b49a2a051c471df112104b53720113 Mon Sep 17 00:00:00 2001 From: ligi Date: Sun, 1 Mar 2020 02:13:52 +0100 Subject: [PATCH] Migrate from kodein to koin --- android/build.gradle | 3 +- .../java/org/ligi/passandroid/TestApp.kt | 39 +++++++-------- .../org/ligi/passandroid/TheAddToCalendar.kt | 24 +++++----- .../org/ligi/passandroid/TheBarCodeEditing.kt | 3 +- .../passandroid/TheCondensedPassViewMode.kt | 6 +-- .../passandroid/TheFieldListEditFragment.kt | 2 +- .../TheFullscreenBarcodeActivity.kt | 4 +- .../ligi/passandroid/ThePassEditActivity.kt | 2 +- .../ligi/passandroid/ThePassListSwiping.kt | 7 ++- .../ligi/passandroid/ThePassViewActivity.kt | 2 +- .../org/ligi/passandroid/ThePassViewHolder.kt | 2 +- .../passandroid/TheUnzipPassController.java | 2 +- .../functions/PassFromAssetLoader.kt | 3 +- .../src/main/java/org/ligi/passandroid/App.kt | 48 ++++++++++--------- .../functions/InputStreamProvider.kt | 14 +++--- .../model/AndroidFileSystemPassStore.kt | 16 +++++-- .../model/AppleStylePassTranslation.java | 2 - .../ligi/passandroid/model/pass/BarCode.kt | 8 ++-- .../reader/AppleStylePassReader.kt | 28 +++++------ .../org/ligi/passandroid/ui/PassAdapter.kt | 19 ++++---- .../passandroid/ui/PassAndroidActivity.kt | 11 ++--- .../ligi/passandroid/ui/PassEditActivity.kt | 13 +++-- .../passandroid/ui/PassExportTaskAndShare.kt | 14 ++++-- .../org/ligi/passandroid/ui/PassExporter.kt | 12 +++-- .../ligi/passandroid/ui/PassImportActivity.kt | 9 ++-- .../ligi/passandroid/ui/PassListFragment.kt | 25 +++++----- .../ligi/passandroid/ui/PassMenuOptions.kt | 16 +++---- .../ligi/passandroid/ui/PassNavigationView.kt | 12 ++--- .../ligi/passandroid/ui/PassViewFragment.kt | 9 ++-- .../org/ligi/passandroid/ui/PrefsFragment.kt | 7 ++- .../ui/SearchPassesIntentService.kt | 11 ++--- .../ligi/passandroid/ui/TouchImageActivity.kt | 7 ++- .../passandroid/ui/UnzipControllerSpec.kt | 6 +-- .../passandroid/ui/UnzipPassController.kt | 15 ++++-- .../passandroid/ui/edit/FieldsEditFragment.kt | 9 ++-- .../ligi/passandroid/TrackerKodeinModule.kt | 7 +-- .../ligi/passandroid/TrackerKodeinModule.kt | 7 +-- .../ligi/passandroid/LocationsMapFragment.kt | 6 ++- 38 files changed, 223 insertions(+), 207 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 66ed1937..5f3b589b 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -112,8 +112,7 @@ dependencies { implementation 'org.permissionsdispatcher:permissionsdispatcher:4.6.0' kapt 'org.permissionsdispatcher:permissionsdispatcher-processor:4.6.0' - implementation 'com.github.salomonbrys.kodein:kodein:4.1.0' - compileOnly 'org.glassfish:javax.annotation:3.1.1' + implementation "org.koin:koin-android:2.1.1" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3" implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' diff --git a/android/src/androidTest/java/org/ligi/passandroid/TestApp.kt b/android/src/androidTest/java/org/ligi/passandroid/TestApp.kt index a5272116..e94236bf 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/TestApp.kt +++ b/android/src/androidTest/java/org/ligi/passandroid/TestApp.kt @@ -1,10 +1,8 @@ package org.ligi.passandroid -import com.github.salomonbrys.kodein.Kodein -import com.github.salomonbrys.kodein.bind -import com.github.salomonbrys.kodein.instance -import com.github.salomonbrys.kodein.singleton import org.greenrobot.eventbus.EventBus +import org.koin.core.module.Module +import org.koin.dsl.module import org.ligi.passandroid.injections.FixedPassListPassStore import org.ligi.passandroid.model.PassStore import org.ligi.passandroid.model.Settings @@ -20,26 +18,25 @@ import java.util.* class TestApp : App() { - override fun createKodein() = Kodein.Module { - bind() with singleton { - FixedPassListPassStore(emptyList()) + override fun createKoin(): Module { + + return module { + single { passStore as PassStore } + single { settings } + single { tracker } + single { mock(EventBus::class.java) } } - bind() with singleton { - mock(Settings::class.java).apply { - `when`(getSortOrder()).thenReturn(PassSortOrder.DATE_ASC) - `when`(getPassesDir()).thenReturn(File("")) - `when`(doTraceDroidEmailSend()).thenReturn(false) - } - } - bind(overrides = true) with singleton { mock(Tracker::class.java) } - bind() with singleton { mock(EventBus::class.java) } } companion object { - - fun passStore(): PassStore = kodein.instance() - fun settings(): Settings = kodein.instance() + val tracker = mock(Tracker::class.java) + val passStore = FixedPassListPassStore(emptyList()) + val settings = mock(Settings::class.java).apply { + `when`(getSortOrder()).thenReturn(PassSortOrder.DATE_ASC) + `when`(getPassesDir()).thenReturn(File("")) + `when`(doTraceDroidEmailSend()).thenReturn(false) + } fun populatePassStoreWithSinglePass() { @@ -51,13 +48,13 @@ class TestApp : App() { fixedPassListPassStore().setList(passList) - passStore().classifier.moveToTopic(pass, "test") + passStore.classifier.moveToTopic(pass, "test") } fun emptyPassStore() { fixedPassListPassStore().setList(emptyList()) } - private fun fixedPassListPassStore() = passStore() as FixedPassListPassStore + private fun fixedPassListPassStore() = passStore as FixedPassListPassStore } } diff --git a/android/src/androidTest/java/org/ligi/passandroid/TheAddToCalendar.kt b/android/src/androidTest/java/org/ligi/passandroid/TheAddToCalendar.kt index dfeb8f66..e340d405 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/TheAddToCalendar.kt +++ b/android/src/androidTest/java/org/ligi/passandroid/TheAddToCalendar.kt @@ -33,7 +33,7 @@ class TheAddToCalendar { fun testIfWeOnlyHaveCalendarStartDate() { TestApp.populatePassStoreWithSinglePass() - TestApp.passStore().currentPass!!.calendarTimespan = PassImpl.TimeSpan(time) + TestApp.passStore.currentPass!!.calendarTimespan = PassImpl.TimeSpan(time) rule.launchActivity() intending(hasType("vnd.android.cursor.item/event")).respondWith(Instrumentation.ActivityResult(RESULT_CANCELED, null)) @@ -44,7 +44,7 @@ class TheAddToCalendar { hasType("vnd.android.cursor.item/event"), hasExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, time.toEpochSecond() * 1000), hasExtra(CalendarContract.EXTRA_EVENT_END_TIME, time.plusHours(DEFAULT_EVENT_LENGTH_IN_HOURS).toEpochSecond() * 1000), - hasExtra("title", TestApp.passStore().currentPass!!.description) + hasExtra("title", TestApp.passStore.currentPass!!.description) )) } @@ -52,7 +52,7 @@ class TheAddToCalendar { fun testIfWeOnlyHaveCalendarEndDate() { TestApp.populatePassStoreWithSinglePass() - TestApp.passStore().currentPass!!.calendarTimespan = PassImpl.TimeSpan(to = time) + TestApp.passStore.currentPass!!.calendarTimespan = PassImpl.TimeSpan(to = time) rule.launchActivity() intending(hasType("vnd.android.cursor.item/event")).respondWith(Instrumentation.ActivityResult(RESULT_CANCELED, null)) @@ -63,7 +63,7 @@ class TheAddToCalendar { hasType("vnd.android.cursor.item/event"), hasExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, time.minusHours(DEFAULT_EVENT_LENGTH_IN_HOURS).toEpochSecond() * 1000), hasExtra(CalendarContract.EXTRA_EVENT_END_TIME, time.toEpochSecond() * 1000), - hasExtra("title", TestApp.passStore().currentPass!!.description) + hasExtra("title", TestApp.passStore.currentPass!!.description) )) } @@ -71,7 +71,7 @@ class TheAddToCalendar { fun testIfWeOnlyHaveCalendarStartAndEndDate() { TestApp.populatePassStoreWithSinglePass() - TestApp.passStore().currentPass!!.calendarTimespan = PassImpl.TimeSpan(time, time2) + TestApp.passStore.currentPass!!.calendarTimespan = PassImpl.TimeSpan(time, time2) rule.launchActivity() intending(hasType("vnd.android.cursor.item/event")).respondWith(Instrumentation.ActivityResult(RESULT_CANCELED, null)) @@ -82,7 +82,7 @@ class TheAddToCalendar { hasType("vnd.android.cursor.item/event"), hasExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, time.toEpochSecond() * 1000), hasExtra(CalendarContract.EXTRA_EVENT_END_TIME, time2.toEpochSecond() * 1000), - hasExtra("title", TestApp.passStore().currentPass!!.description) + hasExtra("title", TestApp.passStore.currentPass!!.description) )) } @@ -91,7 +91,7 @@ class TheAddToCalendar { fun testIfWeOnlyHaveExpirationDate() { TestApp.populatePassStoreWithSinglePass() - (TestApp.passStore().currentPass as PassImpl).validTimespans = listOf(PassImpl.TimeSpan(time)) + (TestApp.passStore.currentPass as PassImpl).validTimespans = listOf(PassImpl.TimeSpan(time)) rule.launchActivity() intending(hasType("vnd.android.cursor.item/event")).respondWith(Instrumentation.ActivityResult(RESULT_CANCELED, null)) @@ -105,7 +105,7 @@ class TheAddToCalendar { hasType("vnd.android.cursor.item/event"), hasExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, time.toEpochSecond() * 1000), hasExtra(CalendarContract.EXTRA_EVENT_END_TIME, time.plusHours(DEFAULT_EVENT_LENGTH_IN_HOURS).toEpochSecond() * 1000), - hasExtra("title", TestApp.passStore().currentPass!!.description) + hasExtra("title", TestApp.passStore.currentPass!!.description) )) } @@ -113,7 +113,7 @@ class TheAddToCalendar { fun testIfWeOnlyHaveExpirationEndDate() { TestApp.populatePassStoreWithSinglePass() - (TestApp.passStore().currentPass as PassImpl).validTimespans = listOf(PassImpl.TimeSpan(to = time)) + (TestApp.passStore.currentPass as PassImpl).validTimespans = listOf(PassImpl.TimeSpan(to = time)) rule.launchActivity() intending(hasType("vnd.android.cursor.item/event")).respondWith(Instrumentation.ActivityResult(RESULT_CANCELED, null)) @@ -127,7 +127,7 @@ class TheAddToCalendar { hasType("vnd.android.cursor.item/event"), hasExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, time.minusHours(DEFAULT_EVENT_LENGTH_IN_HOURS).toEpochSecond() * 1000), hasExtra(CalendarContract.EXTRA_EVENT_END_TIME, time.toEpochSecond() * 1000), - hasExtra("title", TestApp.passStore().currentPass!!.description) + hasExtra("title", TestApp.passStore.currentPass!!.description) )) } @@ -135,7 +135,7 @@ class TheAddToCalendar { fun testIfWeOnlyHaveExpirationStartAndEndDate() { TestApp.populatePassStoreWithSinglePass() - (TestApp.passStore().currentPass as PassImpl).validTimespans = listOf(PassImpl.TimeSpan(time, time2)) + (TestApp.passStore.currentPass as PassImpl).validTimespans = listOf(PassImpl.TimeSpan(time, time2)) rule.launchActivity() intending(hasType("vnd.android.cursor.item/event")).respondWith(Instrumentation.ActivityResult(RESULT_CANCELED, null)) @@ -149,7 +149,7 @@ class TheAddToCalendar { hasType("vnd.android.cursor.item/event"), hasExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, time.toEpochSecond() * 1000), hasExtra(CalendarContract.EXTRA_EVENT_END_TIME, time2.toEpochSecond() * 1000), - hasExtra("title", TestApp.passStore().currentPass!!.description) + hasExtra("title", TestApp.passStore.currentPass!!.description) )) } diff --git a/android/src/androidTest/java/org/ligi/passandroid/TheBarCodeEditing.kt b/android/src/androidTest/java/org/ligi/passandroid/TheBarCodeEditing.kt index 7955b545..946af673 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/TheBarCodeEditing.kt +++ b/android/src/androidTest/java/org/ligi/passandroid/TheBarCodeEditing.kt @@ -10,7 +10,6 @@ import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.matcher.ViewMatchers.* import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.SdkSuppress -import com.github.salomonbrys.kodein.instance import com.linkedin.android.testbutler.TestButler import org.assertj.core.api.Assertions.assertThat import org.junit.Rule @@ -28,7 +27,7 @@ class TheBarCodeEditing { @get:Rule val rule = TruleskActivityRule(PassEditActivity::class.java, false) - val passStore: PassStore = App.kodein.instance() + val passStore: PassStore = TestApp.passStore private lateinit var currentPass: PassImpl diff --git a/android/src/androidTest/java/org/ligi/passandroid/TheCondensedPassViewMode.kt b/android/src/androidTest/java/org/ligi/passandroid/TheCondensedPassViewMode.kt index 927e3e07..86b1aa9e 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/TheCondensedPassViewMode.kt +++ b/android/src/androidTest/java/org/ligi/passandroid/TheCondensedPassViewMode.kt @@ -20,14 +20,14 @@ class TheCondensedPassViewMode { @get:Rule var rule = TruleskActivityRule(PassListActivity::class.java, false) { TestApp.populatePassStoreWithSinglePass() - val currentPass = TestApp.passStore().currentPass as PassImpl + val currentPass = TestApp.passStore.currentPass as PassImpl currentPass.calendarTimespan = PassImpl.TimeSpan(ZonedDateTime.of(2016, 11, 23, 20, 42, 42, 5, ZoneId.systemDefault())) currentPass.fields = mutableListOf(PassField("textprobe", "bar", "yo", false)) } @Test fun testDateShowsForCondensedOff() { - `when`(TestApp.settings().isCondensedModeEnabled()).thenReturn(false) + `when`(TestApp.settings.isCondensedModeEnabled()).thenReturn(false) rule.launchActivity() @@ -41,7 +41,7 @@ class TheCondensedPassViewMode { @Test fun testFieldShowsForCondensedOn() { - `when`(TestApp.settings().isCondensedModeEnabled()).thenReturn(true) + `when`(TestApp.settings.isCondensedModeEnabled()).thenReturn(true) rule.launchActivity() diff --git a/android/src/androidTest/java/org/ligi/passandroid/TheFieldListEditFragment.kt b/android/src/androidTest/java/org/ligi/passandroid/TheFieldListEditFragment.kt index bdfa1e2c..42445aee 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/TheFieldListEditFragment.kt +++ b/android/src/androidTest/java/org/ligi/passandroid/TheFieldListEditFragment.kt @@ -18,7 +18,7 @@ class TheFieldListEditFragment { @get:Rule val rule = TruleskIntentRule(PassEditActivity::class.java) { - TestApp.passStore().currentPass = PassImpl(UUID.randomUUID().toString()).apply { + TestApp.passStore.currentPass = PassImpl(UUID.randomUUID().toString()).apply { fields = arrayListOf(field) } } diff --git a/android/src/androidTest/java/org/ligi/passandroid/TheFullscreenBarcodeActivity.kt b/android/src/androidTest/java/org/ligi/passandroid/TheFullscreenBarcodeActivity.kt index 213484be..7c0d7d28 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/TheFullscreenBarcodeActivity.kt +++ b/android/src/androidTest/java/org/ligi/passandroid/TheFullscreenBarcodeActivity.kt @@ -2,11 +2,11 @@ package org.ligi.passandroid import android.graphics.Bitmap import android.graphics.drawable.BitmapDrawable +import android.widget.ImageView import androidx.test.espresso.Espresso.onView import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.matcher.ViewMatchers.isDisplayed import androidx.test.espresso.matcher.ViewMatchers.withId -import android.widget.ImageView import org.assertj.core.api.Assertions.assertThat import org.junit.Rule import org.junit.Test @@ -68,7 +68,7 @@ class TheFullscreenBarcodeActivity { val pass = PassImpl(UUID.randomUUID().toString()) pass.barCode = BarCode(format, BARCODE_MESSAGE) - TestApp.passStore().currentPass = pass + TestApp.passStore.currentPass = pass rule.launchActivity(null) onView(withId(R.id.fullscreen_barcode)).check(matches(isDisplayed())) diff --git a/android/src/androidTest/java/org/ligi/passandroid/ThePassEditActivity.kt b/android/src/androidTest/java/org/ligi/passandroid/ThePassEditActivity.kt index e5bcc285..524d070a 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/ThePassEditActivity.kt +++ b/android/src/androidTest/java/org/ligi/passandroid/ThePassEditActivity.kt @@ -25,7 +25,7 @@ import org.ligi.trulesk.TruleskIntentRule @TargetApi(14) class ThePassEditActivity { - val passStore = TestApp.passStore() + val passStore = TestApp.passStore @get:Rule var rule = TruleskIntentRule(PassEditActivity::class.java) { diff --git a/android/src/androidTest/java/org/ligi/passandroid/ThePassListSwiping.kt b/android/src/androidTest/java/org/ligi/passandroid/ThePassListSwiping.kt index ad88e3aa..19b6f0f8 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/ThePassListSwiping.kt +++ b/android/src/androidTest/java/org/ligi/passandroid/ThePassListSwiping.kt @@ -9,7 +9,6 @@ import androidx.test.espresso.matcher.ViewMatchers.* import org.assertj.core.api.Assertions.assertThat import org.junit.Rule import org.junit.Test -import org.ligi.passandroid.App.Companion.passStore import org.ligi.passandroid.ui.PassListActivity import org.ligi.passandroid.ui.PassListFragment import org.ligi.trulesk.TruleskIntentRule @@ -29,7 +28,7 @@ class ThePassListSwiping { onView(withText(R.string.topic_trash)).perform(click()) - assertThat(passStore.classifier.getTopics()).containsExactly(rule.activity.getString(R.string.topic_trash)) + assertThat(TestApp.passStore.classifier.getTopics()).containsExactly(rule.activity.getString(R.string.topic_trash)) } @@ -39,7 +38,7 @@ class ThePassListSwiping { onView(withText(R.string.topic_archive)).perform(click()) - assertThat(passStore.classifier.getTopics()).containsExactly(rule.activity.getString(R.string.topic_archive)) + assertThat(TestApp.passStore.classifier.getTopics()).containsExactly(rule.activity.getString(R.string.topic_archive)) } @Test @@ -51,7 +50,7 @@ class ThePassListSwiping { onView(withText(android.R.string.ok)).perform(click()) - assertThat(passStore.classifier.getTopics()).containsExactly(CUSTOM_PROBE) + assertThat(TestApp.passStore.classifier.getTopics()).containsExactly(CUSTOM_PROBE) } diff --git a/android/src/androidTest/java/org/ligi/passandroid/ThePassViewActivity.kt b/android/src/androidTest/java/org/ligi/passandroid/ThePassViewActivity.kt index 6f88b463..9844b49f 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/ThePassViewActivity.kt +++ b/android/src/androidTest/java/org/ligi/passandroid/ThePassViewActivity.kt @@ -22,7 +22,7 @@ import java.util.* @TargetApi(14) class ThePassViewActivity { - private fun getActPass() = TestApp.passStore().currentPass as PassImpl + private fun getActPass() = TestApp.passStore.currentPass as PassImpl @get:Rule var rule = TruleskActivityRule(PassViewActivity::class.java, false) diff --git a/android/src/androidTest/java/org/ligi/passandroid/ThePassViewHolder.kt b/android/src/androidTest/java/org/ligi/passandroid/ThePassViewHolder.kt index 12ff648f..04fb479e 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/ThePassViewHolder.kt +++ b/android/src/androidTest/java/org/ligi/passandroid/ThePassViewHolder.kt @@ -18,7 +18,7 @@ class ThePassViewHolder { private val currentPass by lazy { TestApp.populatePassStoreWithSinglePass() - App.passStore.currentPass as PassImpl + TestApp.passStore.currentPass as PassImpl } @get:Rule diff --git a/android/src/androidTest/java/org/ligi/passandroid/TheUnzipPassController.java b/android/src/androidTest/java/org/ligi/passandroid/TheUnzipPassController.java index f0a70095..91c1195d 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/TheUnzipPassController.java +++ b/android/src/androidTest/java/org/ligi/passandroid/TheUnzipPassController.java @@ -49,7 +49,7 @@ public class TheUnzipPassController { verify(failCallback).fail(any(String.class)); } catch (Exception e) { - fail("should be able to load file"); + fail("should be able to load file " + e); } } diff --git a/android/src/androidTest/java/org/ligi/passandroid/functions/PassFromAssetLoader.kt b/android/src/androidTest/java/org/ligi/passandroid/functions/PassFromAssetLoader.kt index 44a8b0ae..a0804157 100644 --- a/android/src/androidTest/java/org/ligi/passandroid/functions/PassFromAssetLoader.kt +++ b/android/src/androidTest/java/org/ligi/passandroid/functions/PassFromAssetLoader.kt @@ -3,6 +3,7 @@ package org.ligi.passandroid.functions import android.content.Context import androidx.test.platform.app.InstrumentationRegistry import org.assertj.core.api.Fail.fail +import org.ligi.passandroid.TestApp import org.ligi.passandroid.model.InputStreamWithSource import org.ligi.passandroid.model.PassStore import org.ligi.passandroid.model.pass.Pass @@ -30,7 +31,7 @@ fun loadPassFromAsset(asset: String, callback: (pass: Pass?) -> Unit) { object : UnzipPassController.SuccessCallback { override fun call(uuid: String) { callback.invoke(AppleStylePassReader.read(File(getTestTargetPath(instrumentation.targetContext), uuid), "en", - instrumentation.targetContext)) + instrumentation.targetContext,TestApp.tracker)) } }, mock diff --git a/android/src/main/java/org/ligi/passandroid/App.kt b/android/src/main/java/org/ligi/passandroid/App.kt index 21a5f83d..883ce6b9 100644 --- a/android/src/main/java/org/ligi/passandroid/App.kt +++ b/android/src/main/java/org/ligi/passandroid/App.kt @@ -2,10 +2,14 @@ package org.ligi.passandroid import android.app.Application import androidx.appcompat.app.AppCompatDelegate -import com.github.salomonbrys.kodein.* import com.jakewharton.threetenabp.AndroidThreeTen import com.squareup.moshi.Moshi import org.greenrobot.eventbus.EventBus +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 org.ligi.passandroid.json_adapter.ColorAdapter import org.ligi.passandroid.json_adapter.ZonedTimeAdapter import org.ligi.passandroid.model.AndroidFileSystemPassStore @@ -17,43 +21,43 @@ import org.ligi.tracedroid.logging.Log open class App : Application() { + private val moshi = Moshi.Builder() + .add(ZonedTimeAdapter()) + .add(ColorAdapter()) + .build() + + private val settings by lazy { AndroidSettings(this) } + + open fun createKoin(): Module { + + return module { + single { AndroidFileSystemPassStore(this@App, get(), moshi, get()) as PassStore } + single { settings as Settings } + single { EventBus.getDefault() } + single { createTracker(this@App) } + } + } + override fun onCreate() { super.onCreate() - kodein = Kodein { - import(createTrackerKodeinModule(this@App)) - import(createKodein(), allowOverride = true) + startKoin { + if (BuildConfig.DEBUG) androidLogger() + androidContext(this@App) + modules(createKoin()) } AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) AndroidThreeTen.init(this) initTraceDroid() - val settings: Settings = kodein.instance() AppCompatDelegate.setDefaultNightMode(settings.getNightMode()) } - open fun createKodein() = Kodein.Module { - val moshi = Moshi.Builder() - .add(ZonedTimeAdapter()) - .add(ColorAdapter()) - .build() - - - bind() with singleton { AndroidFileSystemPassStore(this@App, instance(), moshi, instance()) } - bind() with singleton { AndroidSettings(this@App) } - bind() with singleton { EventBus.getDefault() } - } private fun initTraceDroid() { TraceDroid.init(this) Log.setTAG("PassAndroid") } - companion object { - lateinit var kodein: Kodein - val tracker by lazy { kodein.Instance(TT(Tracker::class.java)) } - val passStore by lazy { kodein.Instance(TT(PassStore::class.java)) } - val settings by lazy { kodein.Instance(TT(Settings::class.java)) } - } } diff --git a/android/src/main/java/org/ligi/passandroid/functions/InputStreamProvider.kt b/android/src/main/java/org/ligi/passandroid/functions/InputStreamProvider.kt index 965ff70b..fd875bec 100644 --- a/android/src/main/java/org/ligi/passandroid/functions/InputStreamProvider.kt +++ b/android/src/main/java/org/ligi/passandroid/functions/InputStreamProvider.kt @@ -4,31 +4,31 @@ import android.content.Context import android.net.Uri import okhttp3.OkHttpClient import okhttp3.Request -import org.ligi.passandroid.App +import org.ligi.passandroid.Tracker import org.ligi.passandroid.model.InputStreamWithSource import java.io.BufferedInputStream import java.net.URL const val IPHONE_USER_AGENT = "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53" -fun fromURI(context: Context, uri: Uri): InputStreamWithSource? { - App.tracker.trackEvent("protocol", "to_inputstream", uri.scheme, null) +fun fromURI(context: Context, uri: Uri, tracker: Tracker): InputStreamWithSource? { + tracker.trackEvent("protocol", "to_inputstream", uri.scheme, null) return when (uri.scheme) { "content" -> fromContent(context, uri) "http", "https" -> // TODO check if SPDY should be here - return fromOKHttp(uri) + return fromOKHttp(uri, tracker) "file" -> getDefaultInputStreamForUri(uri) else -> { - App.tracker.trackException("unknown scheme in ImportAsyncTask" + uri.scheme, false) + tracker.trackException("unknown scheme in ImportAsyncTask" + uri.scheme, false) getDefaultInputStreamForUri(uri) } } } -private fun fromOKHttp(uri: Uri): InputStreamWithSource? { +private fun fromOKHttp(uri: Uri, tracker: Tracker): InputStreamWithSource? { val client = OkHttpClient() val url = URL(uri.toString()) val requestBuilder = Request.Builder().url(url) @@ -47,7 +47,7 @@ private fun fromOKHttp(uri: Uri): InputStreamWithSource? { for ((key, value) in iPhoneFakeMap) { if (uri.toString().contains(value)) { - App.tracker.trackEvent("quirk_fix", "ua_fake", key, null) + tracker.trackEvent("quirk_fix", "ua_fake", key, null) requestBuilder.header("User-Agent", IPHONE_USER_AGENT) } } diff --git a/android/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.kt b/android/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.kt index 76262825..d13a114e 100644 --- a/android/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.kt +++ b/android/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.kt @@ -7,8 +7,10 @@ import okio.buffer import okio.sink import okio.source import org.greenrobot.eventbus.EventBus -import org.ligi.passandroid.App +import org.koin.core.KoinComponent +import org.koin.core.inject import org.ligi.passandroid.BuildConfig +import org.ligi.passandroid.Tracker import org.ligi.passandroid.events.PassStoreChangeEvent import org.ligi.passandroid.model.pass.Pass import org.ligi.passandroid.model.pass.PassImpl @@ -17,13 +19,19 @@ import org.ligi.passandroid.reader.PassReader import java.io.File import java.util.* -class AndroidFileSystemPassStore(private val context: Context, settings: Settings, private val moshi: Moshi, private val bus: EventBus) : PassStore { +class AndroidFileSystemPassStore( + private val context: Context, settings: Settings, + private val moshi: Moshi, + private val bus: EventBus +) : PassStore, KoinComponent { private val path: File = settings.getPassesDir() override val passMap = HashMap() override var currentPass: Pass? = null + private val tracker: Tracker by inject () + override val classifier: PassClassifier by lazy { val classificationFile = File(settings.getStateDir(), "classifier_state.json") FileBackedPassClassifier(classificationFile, this, moshi) @@ -71,7 +79,7 @@ class AndroidFileSystemPassStore(private val context: Context, settings: Setting try { result = jsonAdapter.fromJson(file.source().buffer()) } catch (ignored: JsonDataException) { - App.tracker.trackException("invalid main.json", false) + tracker.trackException("invalid main.json", false) } } @@ -81,7 +89,7 @@ class AndroidFileSystemPassStore(private val context: Context, settings: Setting } if (result == null && File(pathForID, "pass.json").exists()) { - result = AppleStylePassReader.read(pathForID, language, context) + result = AppleStylePassReader.read(pathForID, language, context, tracker) } if (result != null) { diff --git a/android/src/main/java/org/ligi/passandroid/model/AppleStylePassTranslation.java b/android/src/main/java/org/ligi/passandroid/model/AppleStylePassTranslation.java index 5fc2a4f5..f47ab73c 100644 --- a/android/src/main/java/org/ligi/passandroid/model/AppleStylePassTranslation.java +++ b/android/src/main/java/org/ligi/passandroid/model/AppleStylePassTranslation.java @@ -10,7 +10,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.UnsupportedEncodingException; import java.util.HashMap; -import org.ligi.passandroid.App; public class AppleStylePassTranslation extends HashMap { @@ -74,7 +73,6 @@ public class AppleStylePassTranslation extends HashMap { } return new String(fileData); } catch (Throwable e) { - App.Companion.getTracker().trackException("problem_reading_translation", e, false); e.printStackTrace(); return null; } diff --git a/android/src/main/java/org/ligi/passandroid/model/pass/BarCode.kt b/android/src/main/java/org/ligi/passandroid/model/pass/BarCode.kt index 7435d91a..06b4ce5e 100644 --- a/android/src/main/java/org/ligi/passandroid/model/pass/BarCode.kt +++ b/android/src/main/java/org/ligi/passandroid/model/pass/BarCode.kt @@ -2,21 +2,21 @@ package org.ligi.passandroid.model.pass import android.content.res.Resources import android.graphics.drawable.BitmapDrawable -import com.github.salomonbrys.kodein.instance import com.squareup.moshi.JsonClass -import org.ligi.passandroid.App +import org.koin.core.KoinComponent +import org.koin.core.inject import org.ligi.passandroid.Tracker import org.ligi.passandroid.functions.generateBitmapDrawable import org.ligi.tracedroid.logging.Log import java.util.* @JsonClass(generateAdapter = true) -class BarCode(val format: PassBarCodeFormat?, val message: String? = UUID.randomUUID().toString().toUpperCase()) { +class BarCode(val format: PassBarCodeFormat?, val message: String? = UUID.randomUUID().toString().toUpperCase()) : KoinComponent { + val tracker: Tracker by inject () var alternativeText: String? = null fun getBitmap(resources: Resources): BitmapDrawable? { - val tracker: Tracker = App.kodein.instance() if (message == null) { // no message -> no barcode tracker.trackException("No Barcode in pass - strange", false) diff --git a/android/src/main/java/org/ligi/passandroid/reader/AppleStylePassReader.kt b/android/src/main/java/org/ligi/passandroid/reader/AppleStylePassReader.kt index d1f9f4fc..4d33194e 100644 --- a/android/src/main/java/org/ligi/passandroid/reader/AppleStylePassReader.kt +++ b/android/src/main/java/org/ligi/passandroid/reader/AppleStylePassReader.kt @@ -7,8 +7,8 @@ import android.graphics.Color import org.json.JSONException import org.json.JSONObject import org.ligi.kaxt.parseColor -import org.ligi.passandroid.App import org.ligi.passandroid.R +import org.ligi.passandroid.Tracker import org.ligi.passandroid.functions.getHumanCategoryString import org.ligi.passandroid.functions.readJSONSafely import org.ligi.passandroid.model.ApplePassbookQuirkCorrector @@ -26,7 +26,7 @@ import java.util.* object AppleStylePassReader { - fun read(passFile: File, language: String, context: Context): Pass? { + fun read(passFile: File, language: String, context: Context, tracker: Tracker): Pass? { val translation = AppleStylePassTranslation() @@ -34,7 +34,7 @@ object AppleStylePassReader { var passJSON: JSONObject? = null - val localizedPath = findLocalizedPath(passFile, language) + val localizedPath = findLocalizedPath(passFile, language, tracker) if (localizedPath != null) { val file = File(localizedPath, "pass.strings") @@ -77,7 +77,7 @@ object AppleStylePassReader { if (passJSON == null) { Log.w("could not load pass.json from passcode ") - App.tracker.trackEvent("problem_event", "pass", "without_pass_json", null) + tracker.trackEvent("problem_event", "pass", "without_pass_json", null) return null } @@ -86,7 +86,7 @@ object AppleStylePassReader { if (barcodeJSON != null) { val barcodeFormatString = barcodeJSON.getString("format") - App.tracker.trackEvent("measure_event", "barcode_format", barcodeFormatString, 0L) + tracker.trackEvent("measure_event", "barcode_format", barcodeFormatString, 0L) val barcodeFormat = BarCode.getFormatFromString(barcodeFormatString) val barCode = BarCode(barcodeFormat, barcodeJSON.getString("message")) pass.barCode = barCode @@ -105,9 +105,9 @@ object AppleStylePassReader { } catch (e: JSONException) { // be robust when it comes to bad dates - had a RL crash with "2013-12-25T00:00-57:00" here // OK then we just have no date here - App.tracker.trackException("problem parsing relevant date", e, false) + tracker.trackException("problem parsing relevant date", e, false) } catch (e: DateTimeException) { - App.tracker.trackException("problem parsing relevant date", e, false) + tracker.trackException("problem parsing relevant date", e, false) } } @@ -118,9 +118,9 @@ object AppleStylePassReader { } catch (e: JSONException) { // be robust when it comes to bad dates - had a RL crash with "2013-12-25T00:00-57:00" here // OK then we just have no date here - App.tracker.trackException("problem parsing expiration date", e, false) + tracker.trackException("problem parsing expiration date", e, false) } catch (e: DateTimeException) { - App.tracker.trackException("problem parsing expiration date", e, false) + tracker.trackException("problem parsing expiration date", e, false) } } @@ -196,12 +196,12 @@ object AppleStylePassReader { try { pass.creator = passJSON.getString("organizationName") - App.tracker.trackEvent("measure_event", "organisation_parse", pass.creator, 1L) + tracker.trackEvent("measure_event", "organisation_parse", pass.creator, 1L) } catch (ignored: JSONException) { // ok - we have no organisation - big deal ..-) } - ApplePassbookQuirkCorrector(App.tracker).correctQuirks(pass) + ApplePassbookQuirkCorrector(tracker).correctQuirks(pass) return pass } @@ -240,18 +240,18 @@ object AppleStylePassReader { } - private fun findLocalizedPath(path: File, language: String): String? { + private fun findLocalizedPath(path: File, language: String, tracker: Tracker): String? { val localized = File(path, "$language.lproj") if (localized.exists() && localized.isDirectory) { - App.tracker.trackEvent("measure_event", "pass", language + "_native_lproj", null) + tracker.trackEvent("measure_event", "pass", language + "_native_lproj", null) return localized.path } val fallback = File(path, "en.lproj") if (fallback.exists() && fallback.isDirectory) { - App.tracker.trackEvent("measure_event", "pass", "en_lproj", null) + tracker.trackEvent("measure_event", "pass", "en_lproj", null) return fallback.path } diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassAdapter.kt b/android/src/main/java/org/ligi/passandroid/ui/PassAdapter.kt index 843a02fe..4f7c0f14 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassAdapter.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassAdapter.kt @@ -1,14 +1,14 @@ package org.ligi.passandroid.ui -import com.google.android.material.snackbar.Snackbar +import android.view.LayoutInflater +import android.view.ViewGroup import androidx.appcompat.app.AppCompatActivity import androidx.cardview.widget.CardView import androidx.recyclerview.widget.RecyclerView -import android.view.LayoutInflater -import android.view.ViewGroup -import com.github.salomonbrys.kodein.instance +import com.google.android.material.snackbar.Snackbar +import org.koin.core.KoinComponent +import org.koin.core.inject import org.ligi.kaxt.startActivityFromClass -import org.ligi.passandroid.App import org.ligi.passandroid.R import org.ligi.passandroid.model.PassStore import org.ligi.passandroid.model.PassStoreProjection @@ -17,10 +17,13 @@ import org.ligi.passandroid.ui.pass_view_holder.CondensedPassViewHolder import org.ligi.passandroid.ui.pass_view_holder.PassViewHolder import org.ligi.passandroid.ui.pass_view_holder.VerbosePassViewHolder -class PassAdapter(private val passListActivity: AppCompatActivity, private val passStoreProjection: PassStoreProjection) : RecyclerView.Adapter() { +class PassAdapter( + private val passListActivity: AppCompatActivity, + private val passStoreProjection: PassStoreProjection +) : RecyclerView.Adapter(), KoinComponent { - val passStore: PassStore = App.kodein.instance() - val settings: Settings = App.kodein.instance() + private val passStore: PassStore by inject () + private val settings: Settings by inject () override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): PassViewHolder { val inflater = LayoutInflater.from(viewGroup.context) diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassAndroidActivity.kt b/android/src/main/java/org/ligi/passandroid/ui/PassAndroidActivity.kt index 7616aa9c..b7d3960b 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassAndroidActivity.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassAndroidActivity.kt @@ -1,19 +1,18 @@ package org.ligi.passandroid.ui import androidx.appcompat.app.AppCompatActivity -import com.github.salomonbrys.kodein.instance import org.greenrobot.eventbus.EventBus -import org.ligi.passandroid.App +import org.koin.android.ext.android.inject import org.ligi.passandroid.Tracker import org.ligi.passandroid.model.PassStore import org.ligi.passandroid.model.Settings open class PassAndroidActivity : AppCompatActivity() { - val passStore: PassStore = App.kodein.instance() - val settings: Settings = App.kodein.instance() - val bus: EventBus = App.kodein.instance() - val tracker: Tracker = App.kodein.instance() + val passStore: PassStore by inject() + val settings: Settings by inject() + val bus: EventBus by inject() + val tracker: Tracker by inject() private var lastSetNightMode: Int? = null diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassEditActivity.kt b/android/src/main/java/org/ligi/passandroid/ui/PassEditActivity.kt index 1713b9ca..35dd304a 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassEditActivity.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassEditActivity.kt @@ -3,19 +3,18 @@ package org.ligi.passandroid.ui import android.Manifest import android.content.Intent import android.os.Bundle -import androidx.annotation.IdRes -import androidx.appcompat.app.AlertDialog -import androidx.appcompat.app.AppCompatActivity import android.view.MenuItem import android.view.View import android.widget.Button import android.widget.ImageView -import com.github.salomonbrys.kodein.instance +import androidx.annotation.IdRes +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.edit.* import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe +import org.koin.android.ext.android.inject import org.ligi.kaxt.doAfterEdit -import org.ligi.passandroid.App import org.ligi.passandroid.R import org.ligi.passandroid.events.PassRefreshEvent import org.ligi.passandroid.model.PassStore @@ -39,8 +38,8 @@ class PassEditActivity : AppCompatActivity() { private lateinit var currentPass: PassImpl private val imageEditHelper by lazy { ImageEditHelper(this, passStore) } - internal val passStore: PassStore = App.kodein.instance() - internal val bus: EventBus = App.kodein.instance() + internal val passStore: PassStore by inject() + internal val bus: EventBus by inject() private val passViewHelper: PassViewHelper by lazy { PassViewHelper(this) } diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassExportTaskAndShare.kt b/android/src/main/java/org/ligi/passandroid/ui/PassExportTaskAndShare.kt index 6a8122cf..359de497 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassExportTaskAndShare.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassExportTaskAndShare.kt @@ -4,15 +4,21 @@ import android.app.Activity import android.app.ProgressDialog import android.content.Intent import android.os.Handler +import android.widget.Toast import androidx.annotation.UiThread import androidx.core.content.FileProvider -import android.widget.Toast -import org.ligi.passandroid.App +import org.koin.core.KoinComponent +import org.koin.core.inject import org.ligi.passandroid.R +import org.ligi.passandroid.Tracker import java.io.File -internal open class PassExportTaskAndShare(protected val activity: Activity, private val inputPath: File) { +internal open class PassExportTaskAndShare( + protected val activity: Activity, + private val inputPath: File +) : KoinComponent { + val tracker: Tracker by inject() @UiThread fun execute() { val file = File(activity.filesDir, "share/share.espass") // important - the FileProvider must be configured for this path @@ -31,7 +37,7 @@ internal open class PassExportTaskAndShare(protected val activity: Activity, pri } if (passExporter.exception != null) { - App.tracker.trackException("passExporterException", passExporter.exception!!, false) + tracker.trackException("passExporterException", passExporter.exception!!, false) Toast.makeText(activity, "could not export pass: " + passExporter.exception, Toast.LENGTH_LONG).show() } else { val uriForFile = FileProvider.getUriForFile(activity, activity.getString(R.string.authority_fileprovider), file) diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassExporter.kt b/android/src/main/java/org/ligi/passandroid/ui/PassExporter.kt index e5c9c983..f97792d0 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassExporter.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassExporter.kt @@ -4,14 +4,16 @@ import net.lingala.zip4j.ZipFile import net.lingala.zip4j.model.ZipParameters import net.lingala.zip4j.model.enums.CompressionLevel import net.lingala.zip4j.model.enums.CompressionMethod - -import org.ligi.passandroid.App - +import org.koin.core.KoinComponent +import org.koin.core.inject +import org.ligi.passandroid.Tracker import java.io.File -class PassExporter(private val inputPath: File, val file: File) { +class PassExporter(private val inputPath: File, val file: File) : KoinComponent { var exception: Exception? = null + val tracker: Tracker by inject() + fun export() { try { file.delete() @@ -28,7 +30,7 @@ class PassExporter(private val inputPath: File, val file: File) { } catch (exception: Exception) { exception.printStackTrace() - App.tracker.trackException("when exporting pass to zip", exception, false) + tracker.trackException("when exporting pass to zip", exception, false) this.exception = exception // we need to take action on the main thread later file.delete() // prevent zombies from taking over } diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassImportActivity.kt b/android/src/main/java/org/ligi/passandroid/ui/PassImportActivity.kt index b0cb05d4..9df3f49e 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassImportActivity.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassImportActivity.kt @@ -5,14 +5,13 @@ import android.os.Bundle import android.view.View.GONE import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.lifecycleScope -import com.github.salomonbrys.kodein.instance import kotlinx.android.synthetic.main.activity_import.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import org.koin.android.ext.android.inject import org.ligi.kaxt.startActivityFromClass import org.ligi.kaxtui.alert -import org.ligi.passandroid.App import org.ligi.passandroid.R import org.ligi.passandroid.Tracker import org.ligi.passandroid.functions.fromURI @@ -25,8 +24,8 @@ import permissions.dispatcher.RuntimePermissions @RuntimePermissions class PassImportActivity : AppCompatActivity() { - val tracker: Tracker = App.kodein.instance() - val passStore: PassStore = App.kodein.instance() + val tracker: Tracker by inject() + val passStore: PassStore by inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -51,7 +50,7 @@ class PassImportActivity : AppCompatActivity() { fun doImport(withPermission: Boolean) { lifecycleScope.launch(Dispatchers.IO) { try { - val fromURI = fromURI(this@PassImportActivity, intent!!.data!!) + val fromURI = fromURI(this@PassImportActivity, intent!!.data!!, tracker) withContext(Dispatchers.Main) { diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassListFragment.kt b/android/src/main/java/org/ligi/passandroid/ui/PassListFragment.kt index 64b20b7a..5e31b62b 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassListFragment.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassListFragment.kt @@ -1,23 +1,22 @@ package org.ligi.passandroid.ui import android.os.Bundle -import androidx.annotation.VisibleForTesting -import androidx.fragment.app.Fragment -import androidx.appcompat.app.AppCompatActivity -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView -import androidx.recyclerview.widget.RecyclerView.ViewHolder -import androidx.recyclerview.widget.ItemTouchHelper -import androidx.recyclerview.widget.ItemTouchHelper.* import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import com.github.salomonbrys.kodein.instance +import androidx.annotation.VisibleForTesting +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.Fragment +import androidx.recyclerview.widget.ItemTouchHelper +import androidx.recyclerview.widget.ItemTouchHelper.* +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.ViewHolder import kotlinx.android.synthetic.main.pass_recycler.view.* import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode -import org.ligi.passandroid.App +import org.koin.android.ext.android.inject import org.ligi.passandroid.R import org.ligi.passandroid.events.PassStoreChangeEvent import org.ligi.passandroid.events.ScanFinishedEvent @@ -32,9 +31,9 @@ class PassListFragment : Fragment() { private lateinit var passStoreProjection: PassStoreProjection private lateinit var adapter: PassAdapter - val passStore: PassStore = App.kodein.instance() - val settings: Settings = App.kodein.instance() - val bus: EventBus = App.kodein.instance() + val passStore: PassStore by inject() + val settings: Settings by inject() + val bus: EventBus by inject() override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val inflate = inflater.inflate(R.layout.pass_recycler, container, false) diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassMenuOptions.kt b/android/src/main/java/org/ligi/passandroid/ui/PassMenuOptions.kt index f200ef39..c49344be 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassMenuOptions.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassMenuOptions.kt @@ -2,14 +2,14 @@ package org.ligi.passandroid.ui import android.app.Activity import android.content.Intent -import androidx.core.app.NavUtils -import androidx.appcompat.app.AlertDialog import android.view.LayoutInflater import android.view.MenuItem -import com.github.salomonbrys.kodein.instance +import androidx.appcompat.app.AlertDialog +import androidx.core.app.NavUtils import kotlinx.android.synthetic.main.delete_dialog_layout.view.* +import org.koin.core.KoinComponent +import org.koin.core.inject import org.ligi.kaxt.startActivityFromClass -import org.ligi.passandroid.App import org.ligi.passandroid.R import org.ligi.passandroid.Tracker import org.ligi.passandroid.maps.PassbookMapsFacade @@ -19,11 +19,11 @@ import org.ligi.passandroid.model.pass.Pass import org.ligi.passandroid.printing.doPrint import java.io.File -class PassMenuOptions(val activity: Activity, val pass: Pass) { +class PassMenuOptions(val activity: Activity, val pass: Pass) : KoinComponent { - var passStore: PassStore = App.kodein.instance() - var tracker: Tracker = App.kodein.instance() - var settings: Settings = App.kodein.instance() + val passStore: PassStore by inject() + val tracker: Tracker by inject() + val settings: Settings by inject() fun process(item: MenuItem): Boolean { when (item.itemId) { diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassNavigationView.kt b/android/src/main/java/org/ligi/passandroid/ui/PassNavigationView.kt index b6f615a6..537d3ce4 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassNavigationView.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassNavigationView.kt @@ -4,22 +4,22 @@ import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.net.Uri -import com.google.android.material.navigation.NavigationView import android.util.AttributeSet -import com.github.salomonbrys.kodein.instance +import com.google.android.material.navigation.NavigationView import kotlinx.android.synthetic.main.navigation_drawer_header.view.* import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode -import org.ligi.passandroid.App +import org.koin.core.KoinComponent +import org.koin.core.inject import org.ligi.passandroid.R import org.ligi.passandroid.events.PassStoreChangeEvent import org.ligi.passandroid.model.PassStore -class PassNavigationView(context: Context, attrs: AttributeSet) : NavigationView(context, attrs) { +class PassNavigationView(context: Context, attrs: AttributeSet) : NavigationView(context, attrs), KoinComponent { - val passStore: PassStore = App.kodein.instance() - val bus: EventBus = App.kodein.instance() + val passStore: PassStore by inject() + val bus: EventBus by inject() private fun getIntent(id: Int) = when (id) { R.id.menu_settings -> Intent(context, PreferenceActivity::class.java) diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassViewFragment.kt b/android/src/main/java/org/ligi/passandroid/ui/PassViewFragment.kt index dbe56a50..a99cf61a 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassViewFragment.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassViewFragment.kt @@ -2,23 +2,22 @@ package org.ligi.passandroid.ui import android.content.Intent import android.os.Bundle -import androidx.fragment.app.Fragment -import androidx.core.text.util.LinkifyCompat import android.text.util.Linkify import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView +import androidx.core.text.util.LinkifyCompat +import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity -import com.github.salomonbrys.kodein.instance import kotlinx.android.synthetic.main.activity_pass_view.* import kotlinx.android.synthetic.main.barcode.* import kotlinx.android.synthetic.main.pass_list_item.* import kotlinx.android.synthetic.main.pass_view_extra_data.* +import org.koin.android.ext.android.inject import org.ligi.compat.HtmlCompat import org.ligi.kaxt.startActivityFromClass -import org.ligi.passandroid.App import org.ligi.passandroid.R import org.ligi.passandroid.maps.PassbookMapsFacade import org.ligi.passandroid.model.PassBitmapDefinitions @@ -29,7 +28,7 @@ import org.ligi.passandroid.ui.pass_view_holder.VerbosePassViewHolder class PassViewFragment : Fragment() { private val passViewHelper by lazy { PassViewHelper(requireActivity()) } - private var passStore : PassStore = App.kodein.instance() + internal val passStore : PassStore by inject() lateinit var pass : Pass private fun processImage(view: ImageView, name: String, pass: Pass) { diff --git a/android/src/main/java/org/ligi/passandroid/ui/PrefsFragment.kt b/android/src/main/java/org/ligi/passandroid/ui/PrefsFragment.kt index bf1aca66..bd6d8d7f 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PrefsFragment.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PrefsFragment.kt @@ -6,14 +6,17 @@ import android.os.Bundle import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO import androidx.preference.PreferenceFragmentCompat -import org.ligi.passandroid.App +import org.koin.android.ext.android.inject import org.ligi.passandroid.R +import org.ligi.passandroid.model.Settings import permissions.dispatcher.NeedsPermission import permissions.dispatcher.RuntimePermissions @RuntimePermissions class PrefsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener { + val settings : Settings by inject() + override fun onResume() { super.onResume() preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this) @@ -26,7 +29,7 @@ class PrefsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPref override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) { if (key == getString(R.string.preference_key_nightmode)) { - @AppCompatDelegate.NightMode val nightMode = App.settings.getNightMode() + @AppCompatDelegate.NightMode val nightMode = settings.getNightMode() if (nightMode == MODE_NIGHT_AUTO) { ensureDayNightWithPermissionCheck() diff --git a/android/src/main/java/org/ligi/passandroid/ui/SearchPassesIntentService.kt b/android/src/main/java/org/ligi/passandroid/ui/SearchPassesIntentService.kt index cca8eae2..070a568f 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/SearchPassesIntentService.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/SearchPassesIntentService.kt @@ -11,9 +11,8 @@ import android.os.Build import android.os.Environment import androidx.core.app.NotificationCompat import androidx.preference.PreferenceManager -import com.github.salomonbrys.kodein.instance import org.greenrobot.eventbus.EventBus -import org.ligi.passandroid.App +import org.koin.android.ext.android.inject import org.ligi.passandroid.R import org.ligi.passandroid.Tracker import org.ligi.passandroid.events.ScanFinishedEvent @@ -43,9 +42,9 @@ class SearchPassesIntentService : IntentService("SearchPassesIntentService") { private var lastProgressUpdate: Long = 0 - val passStore: PassStore = App.kodein.instance() - val bus: EventBus = App.kodein.instance() - val tracker: Tracker = App.kodein.instance() + val passStore: PassStore by inject() + val bus: EventBus by inject() + val tracker: Tracker by inject() override fun onHandleIntent(intent: Intent?) { @@ -130,7 +129,7 @@ class SearchPassesIntentService : IntentService("SearchPassesIntentService") { Log.i("found" + file.absolutePath) try { - val ins = fromURI(baseContext, Uri.parse("file://" + file.absolutePath)) + val ins = fromURI(baseContext, Uri.parse("file://" + file.absolutePath), tracker) val onSuccessCallback = SearchSuccessCallback(baseContext, passStore, foundList, diff --git a/android/src/main/java/org/ligi/passandroid/ui/TouchImageActivity.kt b/android/src/main/java/org/ligi/passandroid/ui/TouchImageActivity.kt index e856a843..fb0a83e6 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/TouchImageActivity.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/TouchImageActivity.kt @@ -1,16 +1,15 @@ package org.ligi.passandroid.ui import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity import android.view.MenuItem -import com.github.salomonbrys.kodein.instance +import androidx.appcompat.app.AppCompatActivity import com.ortiz.touch.TouchImageView -import org.ligi.passandroid.App +import org.koin.android.ext.android.inject import org.ligi.passandroid.model.PassStore class TouchImageActivity : AppCompatActivity() { - val passStore: PassStore = App.kodein.instance() + val passStore: PassStore by inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/android/src/main/java/org/ligi/passandroid/ui/UnzipControllerSpec.kt b/android/src/main/java/org/ligi/passandroid/ui/UnzipControllerSpec.kt index 90cfe5e8..5120431b 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/UnzipControllerSpec.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/UnzipControllerSpec.kt @@ -1,8 +1,8 @@ package org.ligi.passandroid.ui import android.content.Context -import org.ligi.passandroid.App import org.ligi.passandroid.model.PassStore +import org.ligi.passandroid.model.Settings import org.ligi.passandroid.ui.UnzipPassController.FailCallback import org.ligi.passandroid.ui.UnzipPassController.SuccessCallback import java.io.File @@ -14,7 +14,7 @@ open class UnzipControllerSpec(var targetPath: File, val failCallback: FailCallback?) { var overwrite = false - constructor(context: Context, passStore: PassStore, onSuccessCallback: SuccessCallback?, failCallback: FailCallback?) - : this(App.settings.getPassesDir(), context, passStore, onSuccessCallback, failCallback) + constructor(context: Context, passStore: PassStore, onSuccessCallback: SuccessCallback?, failCallback: FailCallback?, settings: Settings) + : this(settings.getPassesDir(), context, passStore, onSuccessCallback, failCallback) } diff --git a/android/src/main/java/org/ligi/passandroid/ui/UnzipPassController.kt b/android/src/main/java/org/ligi/passandroid/ui/UnzipPassController.kt index 6488ff58..6bcd4f6d 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/UnzipPassController.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/UnzipPassController.kt @@ -11,18 +11,25 @@ import net.lingala.zip4j.exception.ZipException import okio.buffer import okio.source import org.json.JSONObject -import org.ligi.passandroid.App +import org.koin.core.KoinComponent +import org.koin.core.inject +import org.ligi.passandroid.Tracker import org.ligi.passandroid.functions.createPassForImageImport import org.ligi.passandroid.functions.createPassForPDFImport import org.ligi.passandroid.functions.readJSONSafely +import org.ligi.passandroid.model.AndroidSettings import org.ligi.passandroid.model.InputStreamWithSource import org.ligi.passandroid.model.PassStore +import org.ligi.passandroid.model.Settings import org.ligi.tracedroid.logging.Log import java.io.File import java.io.FileOutputStream import java.util.* -object UnzipPassController { +object UnzipPassController : KoinComponent { + + val tracker :Tracker by inject() + val settings : Settings by inject() interface SuccessCallback { fun call(uuid: String) @@ -39,7 +46,7 @@ object UnzipPassController { processFile(FileUnzipControllerSpec(tempFile.absolutePath, spec)) tempFile.delete() } catch (e: Exception) { - App.tracker.trackException("problem processing InputStream", e, false) + tracker.trackException("problem processing InputStream", e, false) spec.failCallback?.fail("problem with temp file: $e") } @@ -158,6 +165,6 @@ object UnzipPassController { } class InputStreamUnzipControllerSpec(internal val inputStreamWithSource: InputStreamWithSource, context: Context, passStore: PassStore, - onSuccessCallback: SuccessCallback?, failCallback: FailCallback?) : UnzipControllerSpec(context, passStore, onSuccessCallback, failCallback) + onSuccessCallback: SuccessCallback?, failCallback: FailCallback?) : UnzipControllerSpec(context, passStore, onSuccessCallback, failCallback, settings) } diff --git a/android/src/main/java/org/ligi/passandroid/ui/edit/FieldsEditFragment.kt b/android/src/main/java/org/ligi/passandroid/ui/edit/FieldsEditFragment.kt index 9864925f..01a9848c 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/edit/FieldsEditFragment.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/edit/FieldsEditFragment.kt @@ -1,22 +1,25 @@ package org.ligi.passandroid.ui.edit import android.os.Bundle -import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.fragment.app.Fragment import kotlinx.android.synthetic.main.edit_field.view.* import kotlinx.android.synthetic.main.edit_fields.view.* +import org.koin.android.ext.android.inject import org.ligi.kaxt.doAfterEdit -import org.ligi.passandroid.App import org.ligi.passandroid.R +import org.ligi.passandroid.model.PassStore import org.ligi.passandroid.model.pass.PassField import org.ligi.passandroid.model.pass.PassImpl class FieldsEditFragment : Fragment() { - private fun getPass(): PassImpl = App.passStore.currentPass as PassImpl + val passStore by inject() + + private fun getPass(): PassImpl = passStore.currentPass as PassImpl private var isEditingHiddenFields: Boolean = false diff --git a/android/src/noAnalytics/java/org/ligi/passandroid/TrackerKodeinModule.kt b/android/src/noAnalytics/java/org/ligi/passandroid/TrackerKodeinModule.kt index af370f49..858dc85c 100644 --- a/android/src/noAnalytics/java/org/ligi/passandroid/TrackerKodeinModule.kt +++ b/android/src/noAnalytics/java/org/ligi/passandroid/TrackerKodeinModule.kt @@ -1,10 +1,5 @@ package org.ligi.passandroid import android.content.Context -import com.github.salomonbrys.kodein.Kodein -import com.github.salomonbrys.kodein.bind -import com.github.salomonbrys.kodein.singleton -fun createTrackerKodeinModule(context: Context) = Kodein.Module { - bind() with singleton { NotTracker() } -} +fun createTracker(context: Context) = NotTracker() as Tracker diff --git a/android/src/withAnalytics/java/org/ligi/passandroid/TrackerKodeinModule.kt b/android/src/withAnalytics/java/org/ligi/passandroid/TrackerKodeinModule.kt index 31ec944f..c516cdba 100644 --- a/android/src/withAnalytics/java/org/ligi/passandroid/TrackerKodeinModule.kt +++ b/android/src/withAnalytics/java/org/ligi/passandroid/TrackerKodeinModule.kt @@ -1,10 +1,5 @@ package org.ligi.passandroid import android.content.Context -import com.github.salomonbrys.kodein.Kodein -import com.github.salomonbrys.kodein.bind -import com.github.salomonbrys.kodein.singleton -fun createTrackerKodeinModule(context: Context) = Kodein.Module { - bind() with singleton { AnalyticsTracker(context) } -} +fun createTracker(context: Context) = AnalyticsTracker(context) as Tracker diff --git a/android/src/withMaps/java/org/ligi/passandroid/LocationsMapFragment.kt b/android/src/withMaps/java/org/ligi/passandroid/LocationsMapFragment.kt index dc52b626..611338e2 100644 --- a/android/src/withMaps/java/org/ligi/passandroid/LocationsMapFragment.kt +++ b/android/src/withMaps/java/org/ligi/passandroid/LocationsMapFragment.kt @@ -11,12 +11,16 @@ import com.google.android.gms.maps.SupportMapFragment import com.google.android.gms.maps.model.LatLng import com.google.android.gms.maps.model.LatLngBounds import com.google.android.gms.maps.model.MarkerOptions +import org.koin.android.ext.android.inject import org.ligi.kaxt.startActivityFromClass +import org.ligi.passandroid.model.PassStore import org.ligi.passandroid.ui.PassViewActivityBase class LocationsMapFragment : SupportMapFragment() { var clickToFullscreen = false + val passStore : PassStore by inject() + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val root = super.onCreateView(inflater, container, savedInstanceState) val baseActivity = activity as PassViewActivityBase @@ -25,7 +29,7 @@ class LocationsMapFragment : SupportMapFragment() { map.setOnMapLoadedCallback { if (clickToFullscreen) map.setOnMapClickListener { - App.passStore.currentPass = baseActivity.currentPass + passStore.currentPass = baseActivity.currentPass baseActivity.startActivityFromClass(FullscreenMapActivity::class.java) }