Skip to content

Instantly share code, notes, and snippets.

View yongjhih's full-sized avatar
🏠
Working from home

Andrew Chen yongjhih

🏠
Working from home
View GitHub Profile
extension FutureZipX<T> on Future<T> {
Future<(T, T2)> zipWith<T2>(Future<T2> future2) async {
late T result1;
late T2 result2;
await Future.wait(
[then((it) => result1 = it), future2.then((it) => result2 = it)]);
return (result1, result2);
}
}
class Post {
final String? content;
const Post([this.content]);
}
class UserProfile {
final List<Post> posts;
final int followersCount;
const UserProfile(this.posts, this.followersCount);
import 'package:rxdart/rxdart.dart';
void main() async {
final (name, year) = await Stream.fromFuture(Future.value("a"))
.zipWith(Stream.fromFuture(Future.value(2)), (a, b) => (a, b))
.first;
print(name);
print(year);
}
/// For dart 2:
///
/// ```
/// import 'package:tuple/tuple.dart';
///
/// Future<Tuple2<T1, T2>> zipAsync<T1, T2>(
/// Future<T1> future1, Future<T2> future2) async {
/// late T1 result1;
/// late T2 result2;
///
class GsonConverterFactory2(private val gson: Gson = Gson()) : Converter.Factory() {
override fun responseBodyConverter(type: Type, annotations: Array<Annotation>, retrofit: Retrofit): Converter<ResponseBody, *> {
return GsonResponseBodyConverter2(gson, gson.getAdapter(TypeToken.get(type)))
}
override fun requestBodyConverter(type: Type, parameterAnnotations: Array<Annotation>, methodAnnotations: Array<Annotation>, retrofit: Retrofit): Converter<*, RequestBody> {
return GsonRequestBodyConverter2(gson, gson.getAdapter(TypeToken.get(type)))
}
}
fun <R> TypeAdapter<R>.convertResponse(onConvert: TypeAdapter<R>.(ResponseBody) -> R): Converter<ResponseBody, R> =
Converter<ResponseBody, R> { onConvert(it) }
fun <T> TypeAdapter<T>.convertRequest(onConvert: TypeAdapter<T>.(T) -> RequestBody): Converter<T, RequestBody> =
Converter<T, RequestBody> { onConvert(it) }
fun <T, R, I : Converter<in T, out R>> I.wrap(onConvert: I.(T) -> R): Converter<T, R> =
Converter<T, R> { this@wrap.onConvert(it) }
/**
* See Also: BackHandler { onBack() }
*/
@Composable
fun BackCompatHandler(
enabled: Boolean = true,
onBack: () -> Unit = {}
) {
val currentOnBack by rememberUpdatedState(onBack)
val backCallback = remember {
fun <T> byWeakReference(initialValue: T? = null): MutableProperty<T?> = mutablePropertyOf(WeakReference(initialValue)).map(
set = { if (this.field.get() == it) this.field else WeakReference(it) },
get = { it.get() },
)
import android.os.Build
import androidx.annotation.RequiresApi
import java.text.BreakIterator
import java.text.CharacterIterator
interface IBreakIterator {
fun first(): Int
fun last(): Int
fun next(): Int
fun next(value: Int): Int
fun Modifier.border(
top: Dp = 0.dp,
bottom: Dp = 0.dp,
start: Dp = 0.dp,
end: Dp = 0.dp,
color: Color,
) = border(
top = BorderStroke(top, color),
bottom = BorderStroke(bottom, color),
start = BorderStroke(start, color),