Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Andrew Chen yongjhih

🏠
Working from home
View GitHub Profile
View client.saveOrThrow(context).kt
fun Context.outputStreamOrThrow(fileName: String, type: String = Environment.DIRECTORY_DOWNLOADS) =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
contentResolver.run {
openOutputStream(insert(
MediaStore.Downloads.EXTERNAL_CONTENT_URI,
ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
put(MediaStore.MediaColumns.RELATIVE_PATH, type)
}
)!!)!!
View InterceptedInterceptor.kt
class InterceptedInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response =
try {
val invocation = chain.request().tag<Invocation>()
val annotation = invocation?.method()?.annotation<Intercepted>()
(annotation?.value?.java?.constructors?.first()
?.newInstance(invocation.arguments()) as Interceptor?)?.intercept(chain)
} catch (e: Throwable) { null } ?: chain.proceed(chain.request())
}
View CallCallAdapterFactory.kt
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import retrofit2.Call
import retrofit2.CallAdapter
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
open class CallCallAdapterFactory(private val onResponse: (Call<*>, Response<*>) -> Unit) : CallAdapter.Factory() {
override fun get(returnType: Type, annotations: Array<out Annotation>, retrofit: Retrofit): CallAdapter<*, *>? {
View ViewFadeX.kt
@UiThread
fun View.fadeIn(
duration: Long = 250,
fromAlpha: Float? = null,
onAnimation: AlphaAnimation.() -> Unit = {},
) {
clearAnimation()
val oldAlpha = alpha
alpha = 1f
startAnimation(AlphaAnimation(fromAlpha ?: oldAlpha, 1f).apply {
View ConnectivityManager.getNetworkConnectedFlow.kt
@RequiresPermission(ACCESS_NETWORK_STATE)
fun ConnectivityManager.getNetworkConnectedFlow(): Flow<Boolean> = callbackFlow {
trySend(isConnected)
val networkStatusCallback = object : ConnectivityManager.NetworkCallback() {
override fun onUnavailable() {
trySend(false)
}
override fun onAvailable(network: Network) {
View fix-RecyclerView.SCROLL_STATE_IDLE.kt
/**
* Since the onScrollStateChanged(RecyclerView.SCROLL_STATE_IDLE) is not reliable,
* We should check the recyclerView.scrollState after fling instead
*
* ref. https://stackoverflow.com/questions/8140623/android-onscrollstatechanged-scroll-state-idle-sometimes-doesnt-fire
*/
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
scrollStateFlow.tryEmit(newState)
}
View List.putAllBy.dart
extension PutListX<E> on List<E> {
List<E> putAllBy<K>(Iterable<E> other, K onKey(E item), [void onAdd(E item)?]) {
final Map<K, E> otherMap = other.associateBy<K>((it) => onKey(it));
for (final i in indices) {
final key = onKey(this[i]);
if (otherMap.containsKey(key)) {
this[i] = otherMap.remove(key) as E;
}
}
View BuildContextX.dart
xtension BuildContextX on BuildContext {
/// contextOrNull?.navigator().maybePop();
NavigatorState navigator({
bool rootNavigator = false,
}) => Navigator.of(this, rootNavigator: rootNavigator);
NavigatorState? navigatorOrNull({
bool rootNavigator = false,
}) => tryOrNull<NavigatorState>(() => navigator(rootNavigator: rootNavigator));
View lang_test.dart
test("lazy", () async {
final now = DateTime.now();
late final DateTime lazyNow = () {
return DateTime.now();
}();
await 2.seconds.delayed();
expect(lazyNow.difference(now) > 2.seconds, isTrue);
});
View git-owner.sh
#!/usr/bin/env bash
while read f; do
# Max commits
# owner="$(git shortlog -n -s "$f" | head -1 | cut -f 2)"
# Max lines
#owner="$(git blame -c "$f" | awk '{print $3}' | sort | uniq -c | sort -r | head -1 | awk '{print $2}')"
owner="$(git blame --line-porcelain "$f" | grep '^author ' | sed 's|^author ||' | sort | uniq -c | sort -r | head -1 | awk '!($1="")' | sed 's|^ *||g' | sed 's| *$||g')"
echo -e "$f\t$owner"