Skip to content

Instantly share code, notes, and snippets.

@FunkyMuse
Created October 19, 2021 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FunkyMuse/62543b2655a2de3131644f21fc395eaf to your computer and use it in GitHub Desktop.
Save FunkyMuse/62543b2655a2de3131644f21fc395eaf to your computer and use it in GitHub Desktop.
AssistedInject prefs sample
@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
}
@RimonGazi
Copy link

The above code pass static data at creation time, but how can we passed not static data at creation time ?
Here how can i pass "CoroutineScope " ?

@AndroidEntryPoint
class DashboardFragment : Fragment() {

    val scope: CoroutineScope =lifecycleScope

    @Inject
    lateinit var walkThroughPrefsProvider: WalkThroughPrefsProvider

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (walkThroughPrefsProvider.isOneTimeNotShown){
            showWalkThrough()
        }
    }
}

@FunkyMuse
Copy link
Author

The above code pass static data at creation time, but how can we passed not static data at creation time ? Here how can i pass "CoroutineScope " ?

@AndroidEntryPoint
class DashboardFragment : Fragment() {

    val scope: CoroutineScope =lifecycleScope

    @Inject
    lateinit var walkThroughPrefsProvider: WalkThroughPrefsProvider

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (walkThroughPrefsProvider.isOneTimeNotShown){
            showWalkThrough()
        }
    }
}

Why would you pass a CoroutineScope?

You're doing something in a layer you shouldn't be, explore a bit about architecture and how one layer communicates with another one.

@RimonGazi
Copy link

RimonGazi commented Mar 4, 2024

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:

class ApplicationMonitor {

    val serviceScope: CoroutineScope = ...

    val foregroundApp: ForegroundApp = ...

    init {
        serviceScope.launch {
            foregroundApp.collect {
                // Handle foreground app changes
            }
        }
    }

    suspend fun monitor() {
        // Implement monitoring logic
    }
}

class ForegroundApp {
    val currentApp: Flow<String> = ...
    // Other relevant properties and methods
}

class WatchdogService : LifecycleService  {
     @Inject lateinit var applicationMonitor : ApplicationMonitor

}

In the ApplicationMonitor, we observe changes in the foreground app through the ForegroundApp, requiring a coroutine scope. However, rather than creating a new scope, we aim to leverage the Service lifecycle scope for better integration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment