From 21f39d64b3bed283586ccbdb70176d67c751adf3 Mon Sep 17 00:00:00 2001 From: davide Date: Mon, 18 Jul 2022 14:05:47 +0200 Subject: [PATCH] Inject the Api Details Service --- androidApp/build.gradle.kts | 1 + .../readerforselfossv2/android/AddSourceActivity.kt | 11 +++++++---- .../apps/readerforselfossv2/android/HomeActivity.kt | 11 +++++++---- .../apps/readerforselfossv2/android/LoginActivity.kt | 4 ++-- .../apps/readerforselfossv2/android/ReaderActivity.kt | 11 ++++++++--- .../readerforselfossv2/android/SourcesActivity.kt | 11 ++++++++--- .../android/background/background.kt | 11 ++++++++--- .../android/fragments/ArticleFragment.kt | 11 ++++++----- shared/build.gradle.kts | 1 + 9 files changed, 48 insertions(+), 24 deletions(-) diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index c2c5c6a..cabb1c2 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -172,6 +172,7 @@ dependencies { //Dependency Injection implementation("org.kodein.di:kodein-di:7.12.0") + implementation("org.kodein.di:kodein-di-framework-android-x:7.12.0") //PhotoView implementation("com.github.chrisbanes:PhotoView:2.3.0") diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/AddSourceActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/AddSourceActivity.kt index eeedf5f..d8908f6 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/AddSourceActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/AddSourceActivity.kt @@ -20,7 +20,6 @@ import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.isBaseUrlValid import com.ftinc.scoop.Scoop import bou.amine.apps.readerforselfossv2.android.databinding.ActivityAddSourceBinding -import bou.amine.apps.readerforselfossv2.android.service.AndroidApiDetailsService import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl import bou.amine.apps.readerforselfossv2.rest.SelfossModel @@ -28,17 +27,22 @@ import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import org.kodein.di.DIAware +import org.kodein.di.android.closestDI +import org.kodein.di.instance -class AddSourceActivity : AppCompatActivity() { +class AddSourceActivity : AppCompatActivity(), DIAware { - private lateinit var apiDetailsService: ApiDetailsService private var mSpoutsValue: String? = null private lateinit var api: SelfossApiImpl private lateinit var appColors: AppColors private lateinit var binding: ActivityAddSourceBinding + override val di by closestDI() + private val apiDetailsService : ApiDetailsService by instance() + override fun onCreate(savedInstanceState: Bundle?) { appColors = AppColors(this@AddSourceActivity) @@ -77,7 +81,6 @@ class AddSourceActivity : AppCompatActivity() { val prefs = PreferenceManager.getDefaultSharedPreferences(this) val settings = getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) - apiDetailsService = AndroidApiDetailsService(this@AddSourceActivity) api = SelfossApiImpl( // this, // this@AddSourceActivity, diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index ad657ab..24e79f7 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -41,7 +41,6 @@ import bou.amine.apps.readerforselfossv2.android.persistence.entities.AndroidIte import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_1_2 import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_2_3 import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_3_4 -import bou.amine.apps.readerforselfossv2.android.service.AndroidApiDetailsService import bou.amine.apps.readerforselfossv2.android.settings.SettingsActivity import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.Toppings @@ -83,15 +82,17 @@ import com.mikepenz.materialdrawer.widget.AccountHeaderView import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import org.kodein.di.DIAware +import org.kodein.di.android.closestDI +import org.kodein.di.instance import java.util.concurrent.TimeUnit import kotlin.concurrent.thread -class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { +class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAware { private lateinit var dataBase: AndroidDeviceDatabase private lateinit var dbService: AndroidDeviceDatabaseService private lateinit var searchService: SearchService - private lateinit var apiDetailsService: ApiDetailsService private lateinit var service: SelfossService private val MENU_PREFERENCES = 12302 private val DRAWER_ID_TAGS = 100101L @@ -151,6 +152,9 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { private lateinit var config: Config + override val di by closestDI() + private val apiDetailsService : ApiDetailsService by instance() + data class DrawerData(val tags: List?, val sources: List?) override fun onStart() { @@ -195,7 +199,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { sharedPref = PreferenceManager.getDefaultSharedPreferences(this) settings = getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) - apiDetailsService = AndroidApiDetailsService(applicationContext) api = SelfossApiImpl( // this, // this@HomeActivity, diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt index 1b3e222..b4d7b22 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt @@ -26,6 +26,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import org.kodein.di.* +import org.kodein.di.android.closestDI class LoginActivity() : AppCompatActivity(), DIAware { @@ -40,8 +41,7 @@ class LoginActivity() : AppCompatActivity(), DIAware { private lateinit var appColors: AppColors private lateinit var binding: ActivityLoginBinding - override val diContext: DIContext<*> = diContext(this) - override val di by lazy { (application as MyApp).di } + override val di by closestDI() private val apiDetailsService : ApiDetailsService by instance() override fun onCreate(savedInstanceState: Bundle?) { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt index dea9ffe..2b490d5 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt @@ -20,7 +20,6 @@ import bou.amine.apps.readerforselfossv2.android.persistence.database.AppDatabas import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_1_2 import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_2_3 import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_3_4 -import bou.amine.apps.readerforselfossv2.android.service.AndroidApiDetailsService import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.Toppings import bou.amine.apps.readerforselfossv2.android.utils.Config @@ -32,8 +31,11 @@ import com.ftinc.scoop.Scoop import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import org.kodein.di.DIAware +import org.kodein.di.android.closestDI +import org.kodein.di.instance -class ReaderActivity : AppCompatActivity() { +class ReaderActivity : AppCompatActivity(), DIAware { private var markOnScroll: Boolean = false private var currentItem: Int = 0 @@ -52,6 +54,9 @@ class ReaderActivity : AppCompatActivity() { private val JUSTIFY = 1 private val ALIGN_LEFT = 2 + override val di by closestDI() + private val apiDetailsService : ApiDetailsService by instance() + private fun showMenuItem(willAddToFavorite: Boolean) { if (willAddToFavorite) { toolbarMenu.findItem(R.id.star).icon.setTint(Color.WHITE) @@ -106,7 +111,7 @@ class ReaderActivity : AppCompatActivity() { // this@ReaderActivity, // settings.getBoolean("isSelfSignedCert", false), // prefs.getString("api_timeout", "-1")!!.toLong() - AndroidApiDetailsService(this@ReaderActivity) + apiDetailsService ) if (allItems.isEmpty()) { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt index b2580c5..e2217ed 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt @@ -10,24 +10,30 @@ import androidx.recyclerview.widget.LinearLayoutManager import android.widget.Toast import bou.amine.apps.readerforselfossv2.android.adapters.SourcesListAdapter import bou.amine.apps.readerforselfossv2.android.databinding.ActivitySourcesBinding -import bou.amine.apps.readerforselfossv2.android.service.AndroidApiDetailsService import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.Toppings import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import com.ftinc.scoop.Scoop import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import org.kodein.di.DIAware +import org.kodein.di.android.closestDI +import org.kodein.di.instance import java.util.ArrayList -class SourcesActivity : AppCompatActivity() { +class SourcesActivity : AppCompatActivity(), DIAware { private lateinit var appColors: AppColors private lateinit var binding: ActivitySourcesBinding + override val di by closestDI() + private val apiDetailsService : ApiDetailsService by instance() + override fun onCreate(savedInstanceState: Bundle?) { appColors = AppColors(this@SourcesActivity) binding = ActivitySourcesBinding.inflate(layoutInflater) @@ -62,7 +68,6 @@ class SourcesActivity : AppCompatActivity() { getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) val prefs = PreferenceManager.getDefaultSharedPreferences(this) - val apiDetailsService = AndroidApiDetailsService(this@SourcesActivity) val api = SelfossApiImpl( // this, // this@SourcesActivity, diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt index 4cc9028..0b97ffb 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt @@ -13,6 +13,7 @@ import androidx.room.Room import androidx.work.Worker import androidx.work.WorkerParameters import bou.amine.apps.readerforselfossv2.android.MainActivity +import bou.amine.apps.readerforselfossv2.android.MyApp import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.model.preloadImages import bou.amine.apps.readerforselfossv2.android.persistence.AndroidDeviceDatabase @@ -22,32 +23,36 @@ import bou.amine.apps.readerforselfossv2.android.persistence.entities.ActionEnti import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_1_2 import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_2_3 import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_3_4 -import bou.amine.apps.readerforselfossv2.android.service.AndroidApiDetailsService import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable import bou.amine.apps.readerforselfossv2.rest.SelfossApiImpl import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import bou.amine.apps.readerforselfossv2.service.SearchService import bou.amine.apps.readerforselfossv2.service.SelfossService import bou.amine.apps.readerforselfossv2.utils.DateUtils import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import org.kodein.di.DIAware +import org.kodein.di.instance import java.util.* import kotlin.concurrent.schedule import kotlin.concurrent.thread -class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(context, params) { +class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(context, params), DIAware { lateinit var db: AppDatabase + override val di by lazy { (applicationContext as MyApp).di } + private val apiDetailsService : ApiDetailsService by instance() + override fun doWork(): Result { val settings = this.context.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) val sharedPref = PreferenceManager.getDefaultSharedPreferences(this.context) val periodicRefresh = sharedPref.getBoolean("periodic_refresh", false) if (periodicRefresh) { - val apiDetailsService = AndroidApiDetailsService(this.context) val api = SelfossApiImpl( // this.context, // null, diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt index b1297b7..66f987c 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt @@ -33,7 +33,6 @@ import bou.amine.apps.readerforselfossv2.android.persistence.entities.AndroidIte import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_1_2 import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_2_3 import bou.amine.apps.readerforselfossv2.android.persistence.migrations.MIGRATION_3_4 -import bou.amine.apps.readerforselfossv2.android.service.AndroidApiDetailsService import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.utils.* import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper @@ -55,6 +54,8 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import org.kodein.di.* +import org.kodein.di.android.x.closestDI import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -63,9 +64,8 @@ import java.net.URL import java.util.* import java.util.concurrent.ExecutionException -class ArticleFragment : Fragment() { +class ArticleFragment : Fragment(), DIAware { private lateinit var dbService: AndroidDeviceDatabaseService - private lateinit var apiDetailsService: ApiDetailsService private lateinit var service: SelfossService private var fontSize: Int = 16 private lateinit var item: SelfossModel.Item @@ -85,6 +85,9 @@ class ArticleFragment : Fragment() { private var _binding: FragmentArticleBinding? = null private val binding get() = _binding!! + override val di : DI by closestDI() + private val apiDetailsService : ApiDetailsService by instance() + private lateinit var prefs: SharedPreferences private var typeface: Typeface? = null @@ -105,8 +108,6 @@ class ArticleFragment : Fragment() { super.onCreate(savedInstanceState) - apiDetailsService = AndroidApiDetailsService(requireContext()) - dbService = AndroidDeviceDatabaseService(AndroidDeviceDatabase(requireContext()), SearchService(DateUtils(apiDetailsService))) service = SelfossService(SelfossApiImpl(apiDetailsService), dbService, SearchService(DateUtils(apiDetailsService))) diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index ee7aa5b..a66811d 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -44,6 +44,7 @@ kotlin { //Dependency Injection implementation("org.kodein.di:kodein-di:7.12.0") + implementation("org.kodein.di:kodein-di-framework-android-x:7.12.0") } } val androidTest by getting {