Skip to content

Instantly share code, notes, and snippets.

@GargoyleLizy
GargoyleLizy / MapFragment.kt
Last active October 28, 2021 10:16
Google MapView version_03
class MapFragment : Fragment(), OnMapReadyCallback {
companion object {
private const val MAP_VIEW_BUNDLE_KEY = "mapview_bundle_key"
}
private var _binding: FragmentMapsBinding? = null
private val binding: FragmentMapsBinding get() = _binding!!
private var hasMapConfigured: Boolean = false
@GargoyleLizy
GargoyleLizy / listFileName.py
Created March 31, 2019 07:20
List the file names in curent directory.
import os
def listFileName(directoryName):
scandir_iterator = os.scandir(directoryName)
for item in scandir_iterator:
if os.path.isfile(item.path):
print(item.name)
if __name__ == "__main__":
@GargoyleLizy
GargoyleLizy / RxLiveData.kt
Created March 7, 2019 01:32
RxLiveData_filter
/**
* With Given LiveData, produce a new LiveData, which will filter given one's emitted item with
* supplied predicate.
*/
fun <T> LiveData<T>.filter(predicate: (T) -> Boolean): LiveData<T> {
val filtered = MediatorLiveData<T>()
filtered.addSource(this) {
if (predicate(it)) {
filtered.value = it
}
@GargoyleLizy
GargoyleLizy / RxLiveData.kt
Created March 7, 2019 01:32
RxLiveData_filter
/**
* With Given LiveData, produce a new LiveData, which will filter given one's emitted item with
* supplied predicate.
*/
fun <T> LiveData<T>.filter(predicate: (T) -> Boolean): LiveData<T> {
val filtered = MediatorLiveData<T>()
filtered.addSource(this) {
if (predicate(it)) {
filtered.value = it
}
@GargoyleLizy
GargoyleLizy / RxLiveData.kt
Created March 7, 2019 01:20
RxLiveData_doOnce
/**
* Monitor the LiveData and when an emitted item satisfies predicate. Trigger an action, execute
* the statements supplied. Then stop monitoring the LiveData.
*/
fun <T> LiveData<T>.doOnce(predicate: (T) -> Boolean, action: () -> Unit): LiveData<T> {
val liveData: LiveData<T> = this
val observer = object : Observer<T> {
override fun onChanged(t: T) {
if (predicate(t)) {
action()
@GargoyleLizy
GargoyleLizy / RxLiveData.kt
Created March 7, 2019 00:59
RxLiveData_doWhen
/**
* Monitor the LiveData and when an emitted item satisfies predicate. Trigger an action,
* execute the statements supplied.
*/
fun <T> LiveData<T>.doWhen(predicate: (T) -> Boolean, action: () -> Unit): LiveData<T> {
this.observeForever {
if (predicate(it)) {
action()
}
}
@GargoyleLizy
GargoyleLizy / RxLiveData.kt
Created March 7, 2019 00:46
RxLiveData_combineLatest
/**
* When an item is emitted by either of two LiveData, combine the latest item emitted by each
* LiveData via a specified function and emit items based on the result of this function.
*/
@Suppress("UNCHECKED_CAST")
fun <Y, T1, T2> LiveData<T1>.combineLatest(another: LiveData<T2>,
combiner: (T1, T2) -> Y): LiveData<Y> {
val mediatorLiveData = MediatorLiveData<Y>()
var latestT1: T1? = null
@GargoyleLizy
GargoyleLizy / No_Rx_version.kt
Last active March 7, 2019 00:45
Medium_RxLiveData_no_rx_version
private val mediatorData = MediatorLiveData<List<Item>>().apply {
this.addSource(itemListLiveData) { itemList ->
val apiResponse = apiLiveData.value
if (apiResponse != null) {
this.value = combineItemListAndApi(itemList, apiResponse)
}
}
this.addSource(apiLiveData) { apiResponse ->
val itemList = itemListLiveData.value
if (itemList != null) {
@GargoyleLizy
GargoyleLizy / BigInteger.kt
Created February 19, 2019 04:23
A class to solve "+/-" with big integer values.
class BigInteger private constructor(private val valueArray: CharArray,
private val isPositive: Boolean) {
constructor(strValue: String) : this(strValue.toProperCharArray(),
strValue.isPositive())
fun getValueArray(): CharArray {
return this.valueArray.copyOf()
}
@GargoyleLizy
GargoyleLizy / CountingSort.kt
Created February 15, 2019 11:19
CountingSort
/**
* Counting Sort assumes every element of input array was inside [0,k]
*/
object CountingSort {
fun sort(input: Array<Int>, upperLimit: Int): Array<Int> {
val C = IntArray(upperLimit + 1) { 0 }
val output = Array<Int>(input.size) { 0 }
input.forEach { element ->
C[element]++