aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2021-04-17 18:35:14 -0700
committerAllan Wang <me@allanwang.ca>2021-04-17 18:35:14 -0700
commit8b233b2629039e8144fe6eef5438125cd48c8973 (patch)
treecd6eb898a9791b91e0879170d4832f304d3af848 /app
parent2c20f85be14abfd45017b2b0f3a0f5d7c7886b90 (diff)
downloadfrost-8b233b2629039e8144fe6eef5438125cd48c8973.tar.gz
frost-8b233b2629039e8144fe6eef5438125cd48c8973.tar.bz2
frost-8b233b2629039e8144fe6eef5438125cd48c8973.zip
Inject main activity contract
Diffstat (limited to 'app')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt16
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentBase.kt17
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/intro/IntroMainFragments.kt4
3 files changed, 27 insertions, 10 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt
index 8f440129..51f4732a 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt
@@ -128,7 +128,13 @@ import com.pitchedapps.frost.views.BadgedIcon
import com.pitchedapps.frost.views.FrostVideoViewer
import com.pitchedapps.frost.views.FrostViewPager
import com.pitchedapps.frost.widgets.NotificationWidget
+import dagger.Module
+import dagger.Provides
+import dagger.hilt.InstallIn
import dagger.hilt.android.AndroidEntryPoint
+import dagger.hilt.android.components.ActivityComponent
+import dagger.hilt.android.qualifiers.ActivityContext
+import dagger.hilt.android.scopes.ActivityScoped
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import javax.inject.Inject
@@ -936,3 +942,13 @@ abstract class BaseMainActivity :
const val UNSELECTED_TAB_ALPHA = 128f
}
}
+
+@Module
+@InstallIn(ActivityComponent::class)
+object MainActivityModule {
+ @Provides
+ @ActivityScoped
+ fun contract(@ActivityContext context: Context): MainActivityContract =
+ (context as? BaseMainActivity)
+ ?: throw IllegalArgumentException("${context::class.java.simpleName} does not implement MainActivityContract")
+}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentBase.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentBase.kt
index 825e0937..f7d6b0ad 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentBase.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/FragmentBase.kt
@@ -41,6 +41,7 @@ import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.REQUEST_REFRESH
import com.pitchedapps.frost.utils.REQUEST_TEXT_ZOOM
import com.pitchedapps.frost.utils.frostEvent
+import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
@@ -49,7 +50,7 @@ import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
-import org.koin.core.component.inject
+import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
/**
@@ -59,6 +60,7 @@ import kotlin.coroutines.CoroutineContext
* Must be attached to activities implementing [MainActivityContract]
*/
@UseExperimental(ExperimentalCoroutinesApi::class)
+@AndroidEntryPoint
abstract class BaseFragment :
Fragment(),
CoroutineScope,
@@ -87,6 +89,9 @@ abstract class BaseFragment :
}
}
+ @Inject
+ lateinit var mainContract: MainActivityContract
+
protected val fbCookie: FbCookie by inject()
protected val prefs: Prefs by inject()
protected val themeProvider: ThemeProvider by inject()
@@ -107,7 +112,7 @@ abstract class BaseFragment :
"Native Fallback",
"Item" to baseEnum.name
)
- (context as MainActivityContract).reloadFragment(this)
+ mainContract.reloadFragment(this)
}
override var firstLoad: Boolean = true
@@ -122,8 +127,6 @@ abstract class BaseFragment :
super.onCreate(savedInstanceState)
job = SupervisorJob()
firstLoad = true
- if (context !is MainActivityContract)
- throw IllegalArgumentException("${this::class.java.simpleName} is not attached to a context implementing MainActivityContract")
}
final override fun onCreateView(
@@ -145,9 +148,7 @@ abstract class BaseFragment :
onCreateRunnable = null
firstLoadRequest()
detachMainObservable()
- (context as? MainActivityContract)?.let {
- activityReceiver = attachMainObservable(it)
- }
+ activityReceiver = attachMainObservable(mainContract)
}
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
@@ -168,7 +169,7 @@ abstract class BaseFragment :
}
override fun setTitle(title: String) {
- (context as? MainActivityContract)?.setTitle(title)
+ mainContract.setTitle(title)
}
override fun attachMainObservable(contract: MainActivityContract): ReceiveChannel<Int> {
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroMainFragments.kt b/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroMainFragments.kt
index 7ff1df1d..040fa96f 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroMainFragments.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroMainFragments.kt
@@ -52,10 +52,10 @@ import kotlin.math.abs
abstract class BaseIntroFragment(val layoutRes: Int) : Fragment() {
@Inject
- lateinit var prefs: Prefs
+ protected lateinit var prefs: Prefs
@Inject
- lateinit var themeProvider: ThemeProvider
+ protected lateinit var themeProvider: ThemeProvider
val screenWidth
get() = resources.displayMetrics.widthPixels