Skip to content

Instantly share code, notes, and snippets.

@Dzendo
Dzendo / SkyApplication.kt
Created March 15, 2023 10:19
Declaralion and find SharedPreference from device
// Declaralion and find SharedPreference from device
shPr = PreferenceManager.getDefaultSharedPreferences(this /* Activity context */)
// If you save values between sessions - then false (if there are none, then it will create)
// Если сохранять значения между сеансами, то false (если их нет, то создаст)
if (shPr.getBoolean("preference_remember", true))
preferencesReset(false) // if not reset at startup to Default
else
preferencesReset(true) // if reset at startup to Default
@Dzendo
Dzendo / SkyApplication.kt
Created March 15, 2023 09:46
Declaralion and find SharedPreference from device
// Declaralion and find SharedPreference from device
shPr = PreferenceManager.getDefaultSharedPreferences(this /* Activity context */)
// If you save values between sessions - then false (if there are none, then it will create)
@Dzendo
Dzendo / SkyAboutFragment.kt
Created June 19, 2021 18:31
observeEvent example
viewModel.starClicked.observeEvent(viewLifecycleOwner) {
Toast.makeText(activity, it, Toast.LENGTH_LONG).show()
}
viewModel.keyBoard.observeEvent(viewLifecycleOwner) {
it?.let {
if (it) showKeyboard()
else hideKeyboard()
}
}
@Dzendo
Dzendo / SkyAboutViewModel.kt
Created June 19, 2021 18:27
Create Event onClick
private val _starClicked = MutableLiveData<Event<String?>>()
val starClicked: LiveData<Event<String?>>
get() = _starClicked
fun onStarClick() {
_starClicked.value = Event("on Star ONCE Clicked")
}
@Dzendo
Dzendo / Event.kt
Created June 19, 2021 18:22
modify with inline and crossline
open class Event<out T>(private val content: T? = null) {
@Suppress("MemberVisibilityCanBePrivate")
var hasBeenHandled = false
private set
// Returns the content and prevents its use again.
fun getContentIfNotHandled(): T? = if (hasBeenHandled) null else content.also { hasBeenHandled = true }
// Returns the content, even if it's already been handled.
fun peekContent(): T? = content
}