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 / 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
}
@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 / 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 / DiffUtil.kt
Last active July 22, 2020 07:14
[Android][RecyclerView] My Easy DiffUtil function for RecyclerView.
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
// allocate first
val defaultCallback = MutableDiffCallback()
fun RecyclerView.Adapter<*>.calculateDiff(
old: List<*>,
new: List<*>,
callback: MutableDiffCallback = defaultCallback,
@ShinichiroFunatsu
ShinichiroFunatsu / 01ViewModelTemplate
Last active July 15, 2020 05:19
IntelliJ File Template for Android App Development work.
package ${PACKAGE_NAME}
import androidx.annotation.MainThread
import androidx.lifecycle.*
import kotlinx.coroutines.coroutineScope
#parse("File Header.java")
class ${NAME} (
private val getFooUseCase: GetFooUseCase
@ShinichiroFunatsu
ShinichiroFunatsu / NetworkConnectivityChecker.kt
Created June 23, 2020 09:20
NetworkInfo.isConnected another option due to NetworkInfo deprecated.
enum class NetworkType {
WIFI, MOBILE, VPN, OTHER
}
object NetworkConnectivityChecker {
@JvmStatic
fun isConnected(context: Context): Boolean {
val connectivityManager: ConnectivityManager = context.getSystemService() ?: return false
val networkType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
@ShinichiroFunatsu
ShinichiroFunatsu / gist:0b60dcc2f14b70cdcf361eb9afa8aa6f
Created April 16, 2020 09:47 — forked from rxaviers/gist:7360908
Complete list of github markdown emoji markup

People

:bowtie: :bowtie: 😄 :smile: 😆 :laughing:
😊 :blush: 😃 :smiley: ☺️ :relaxed:
😏 :smirk: 😍 :heart_eyes: 😘 :kissing_heart:
😚 :kissing_closed_eyes: 😳 :flushed: 😌 :relieved:
😆 :satisfied: 😁 :grin: 😉 :wink:
😜 :stuck_out_tongue_winking_eye: 😝 :stuck_out_tongue_closed_eyes: 😀 :grinning:
😗 :kissing: 😙 :kissing_smiling_eyes: 😛 :stuck_out_tongue:
@ShinichiroFunatsu
ShinichiroFunatsu / DebugTools.kt
Last active March 30, 2020 07:22
Beta Android DebugTools
fun Fragment.LogD(tag: String, msg: String) {
val activity: String? = activity?.toStringSimple()
val fragment: String = this.toStringSimple()
Log.d(tag, "Fragment %s %s %s".format(activity, fragment, msg))
}
private val nameList = ('A'..'Z').toList()
private val nameCache = mutableMapOf<String, String>()
private var lastUsedIndexForActivity: Int = 0
@ShinichiroFunatsu
ShinichiroFunatsu / SuspendableItemKeyedDataSource.kt
Last active March 26, 2020 05:55
Paging-Coroutine-DataSource-Wrapper. version-0.1.0-alpha
import androidx.paging.ItemKeyedDataSource
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.plus
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.CoroutineContext
@ShinichiroFunatsu
ShinichiroFunatsu / RxJavaSample.kt
Last active January 10, 2020 02:19
RxJava Composition Sample
private fun println2(str: Any) {
val s = "[${Thread.currentThread().name}] $str"
println(s)
}
fun rxJavaSample1() {
println2("step 0")
val sInt = Single.create<Int> { emitter -> println2("step 2"); emitter.onSuccess(1) }
.doOnSuccess { println2("success") }