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
/// 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;
///
/**
* Compatibility
*
* See Also: androidx.core.content.res.use
*/
@OptIn(ExperimentalContracts::class)
inline fun <T: TypedArray, R> T.use(block: (T) -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
class Post {
final String? content;
const Post([this.content]);
}
class UserProfile {
final List<Post> posts;
final int followersCount;
const UserProfile(this.posts, this.followersCount);
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);
}
}
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);
}
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) }
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)))
}
}
/**
* See Also: BackHandler { onBack() }
*/
@Composable
fun BackCompatHandler(
enabled: Boolean = true,
onBack: () -> Unit = {}
) {
val currentOnBack by rememberUpdatedState(onBack)
val backCallback = remember {
/**
* Interface representing a read-only property with a value
*
* Usage:
*
* ```
* val doubleValue by propertyOf(1).map { it * 2 }.map { it * 2 }
*
* doubleValue // 4
* ```
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),