Created
October 19, 2021 08:24
-
-
Save FunkyMuse/62543b2655a2de3131644f21fc395eaf to your computer and use it in GitHub Desktop.
AssistedInject prefs sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@FragmentScoped | |
class WalkThroughPrefsProvider @Inject constructor( | |
private val oneTimePrefFactory: OneTimePref.OneTimePrefFactory | |
) : OneTimePrefContract by oneTimePrefFactory.create( | |
WALK_THROUGH_PREFS, | |
WALK_THROUGH_PREFS_SHOWN_KEY | |
) { | |
private companion object { | |
private const val WALK_THROUGH_PREFS = "walkThrough" | |
private const val WALK_THROUGH_PREFS_SHOWN_KEY = "walkThroughKey" | |
} | |
} | |
class OneTimePref @AssistedInject constructor( | |
@ApplicationContext private val context: Context, | |
@Assisted(PREFS_TAG_KEY) private val prefsTag: String, | |
@Assisted(PREFS_BOOLEAN_KEY) private val prefsBooleanKey: String | |
) : OneTimePrefContract { | |
private companion object { | |
private const val PREFS_TAG_KEY = "prefsTag" | |
private const val PREFS_BOOLEAN_KEY = "prefsBoolean" | |
} | |
@AssistedFactory | |
interface OneTimePrefFactory { | |
fun create( | |
@Assisted(PREFS_TAG_KEY) prefsTag: String, | |
@Assisted(PREFS_BOOLEAN_KEY) prefsBooleanKey: String | |
): OneTimePref | |
} | |
override val oneTimePrefs: SharedPreferences | |
get() = context.getSharedPreferences( | |
prefsTag, | |
Context.MODE_PRIVATE | |
) | |
override val isOneTimeShown get() = oneTimePrefs.getBoolean(prefsBooleanKey, false) | |
override fun setOneTimeShown() = oneTimePrefs.edit { putBoolean(prefsBooleanKey, true) } | |
} | |
interface OneTimePrefContract { | |
val isOneTimeShown: Boolean | |
fun setOneTimeShown() | |
val oneTimePrefs: SharedPreferences | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We're currently in the process of developing an Android kiosk platform, and as part of our system, we've designed several applications to operate as system applications. One of these applications functions without a user interface, instead relying on an Android Service to carry out various tasks. Among these tasks, it periodically checks which application is currently running in the foreground. If the foreground application isn't one scheduled by the user, our system intervenes by terminating it and initiating the expected application, ensuring smooth operation.
Here's a snippet of the code structure:
In the
ApplicationMonitor
, we observe changes in the foreground app through theForegroundApp
, requiring a coroutine scope. However, rather than creating a new scope, we aim to leverage the Service lifecycle scope for better integration.