Skip to content

Instantly share code, notes, and snippets.

View YuanLiou's full-sized avatar
:octocat:
a museum enthusiast.

Jui Yuan Liu (Ray) YuanLiou

:octocat:
a museum enthusiast.
View GitHub Profile
@YuanLiou
YuanLiou / ViewEffectSample.kt
Created February 2, 2021 16:04
ViewEffect in ViewModel sample
// ViewState
sealed class ViewEffect {
data class ShowToast(val message: String) : ViewEffect()
}
// Set up LiveData
private val _viewEffectPublisher: MutableLiveData<Event<ViewEffect>>()
val viewEffectPublisher = LiveData<Event<ViewEffect>>
get() = _viewEffectPublisher
@YuanLiou
YuanLiou / Event.kt
Created February 1, 2021 16:11
LiveEffect sending example #kotlin #LiveData #MVVM #MVI
class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set
fun getContentIfNotHandled(): T? {
if (hasBeenHandled) {
return null
} else {
hasBeenHandled = true
@YuanLiou
YuanLiou / SingleLiveData.kt
Last active February 1, 2021 15:53
SingleLiveData Sample #Kotlin #sample #LiveData
public class SingleLiveData<T> : MutableLiveData<T> {
private val pending = AtomicBoolean(false)
@MainThread
override fun observe(owner: LifecycleOwner, observer: Observer<T>) {
super.observe(owner, object : Observer<T>() {
override fun onChanged(t: T?) {
// compareAndSet(expect, update)
// here if `true` then set to false
@YuanLiou
YuanLiou / Project.kt
Created January 11, 2021 06:39
how to apply local.properties to project
fun Project.getLocalOrDefaultProperties(): Map<String, Any?> {
return getEnvOrDefaultProperties("local.properties")
}
private fun Project.getEnvOrDefaultProperties(path: String): Map<String, Any?> {
return rootProject.file(path)
.takeIf { it.canRead() }
?.let { readProperties(it).toMap() as Map<String, Any?> }
?: properties.toMap()
}
@YuanLiou
YuanLiou / profile.json
Last active May 29, 2020 16:04
Windows Terminal Profile
// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
"$schema": "https://aka.ms/terminal-profiles-schema",
"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"profiles":
@YuanLiou
YuanLiou / md5_extension.kt
Created August 25, 2018 03:11
MD5 Extension Function
fun main(argv: Array<String>) {
val md5Result = "Hello World".saltMD5(null)
println("The Salted MD5 Hash is = $md5Result")
}
private fun String.saltMD5(salt: String?): String {
val saltedString = salt?.let { this + it } ?: this
val md5MessageDigest = MessageDigest.getInstance("MD5")
val digest = md5MessageDigest.digest(saltedString.toByteArray())
val stringBuilder = StringBuilder()
@YuanLiou
YuanLiou / versionCodeGenerator.gradle
Created August 7, 2018 03:24
Gradle Version Code Script
if (beta > 0 ) {
version = major * 1000000 + minor * 10000 + (bugfix -1) * 100 + beta
} else {
version = major * 1000000 + minor * 10000 + bugfix * 100;
}
@YuanLiou
YuanLiou / App.js
Created May 7, 2018 08:56
First Try React Native
import React from 'react';
import { StyleSheet, Text, Image, View } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={{alignItems: 'center', marginTop: 250}} >
<Greeting name="Denkeni" relationship="Boss" />
<Greeting name="Joan" relationship="Classmate" />
<Greeting name="Billy" relationship="Roommate" />
@YuanLiou
YuanLiou / config.fish
Last active April 8, 2025 14:40
Ray's fish settings 2.3.1
if status is-interactive
# Commands to run in interactive sessions can go here
end
# Oh My Posh
oh-my-posh init fish --config ~/.config/ohmyposh/omp.toml | source
# Misc
set fish_greeting "Hello Fish!"
set TERM "xterm-256color"
@YuanLiou
YuanLiou / LiskovSubstitutionPrinciple.java
Created January 18, 2018 10:39
Liskov Substitution Principle
public class somebody {
public static void main(String[] argv) {
Store store = new Store();
List<Cellphone> cellphones = store.getCellphones();
for (int i = 0; i < cellphones; i++) {
cellphones[i].power(false);
}
iPhone iphoneX = new iPhone();
cellphones.add(iphoneX);