Skip to content

Instantly share code, notes, and snippets.

View ShinichiroFunatsu's full-sized avatar

Shinichiro Funatsu ShinichiroFunatsu

  • Freelance Android Dev
  • Tokyo
View GitHub Profile
@ShinichiroFunatsu
ShinichiroFunatsu / EventMediator.kt
Last active July 31, 2020 05:27
LiveData Event Mediator Beta
interface EventMediator<T> {
interface Input<T> {
fun bind(input: LiveData<T>)
}
interface Output<T> {
val onAction: LiveData<T>
}
val input: Input<T>
@ShinichiroFunatsu
ShinichiroFunatsu / ViewModelLiveTemplate.kt
Last active February 8, 2022 05:26
Android "Guide to app architecture : UI events" ViewModel Live Template code for Android dev. see: "https://developer.android.com/jetpack/guide/ui-layer/events"
data class UserMessage(val id: Long, val title: String, val message: String)
data class $name$UiState(
val userMessages: List<UserMessage> = kotlin.collections.emptyList(),
)
class $name$ViewModel : androidx.lifecycle.ViewModel() {
private val _uiState = kotlinx.coroutines.flow.MutableStateFlow($name$UiState())
val uiState: kotlinx.coroutines.flow.StateFlow<$name$UiState> = _uiState
private fun showMessage(title: String, message: String) {
@ShinichiroFunatsu
ShinichiroFunatsu / ContextEx.kt
Last active March 25, 2022 09:09
Android get current process
fun Context.currentProcess(): ActivityManager.RunningAppProcessInfo? {
val manager = getSystemService<ActivityManager>()!!
val pid = android.os.Process.myPid()
return manager.runningAppProcesses.firstOrNull { it.pid == pid }
}
fun Context.isMainProcess(): Boolean {
val currentProcess = currentProcess()
return currentProcess?.processName == BuildConfig.APPLICATION_ID
}