Skip to content

Instantly share code, notes, and snippets.

View AAverin's full-sized avatar

Anton Averin AAverin

View GitHub Profile
@AAverin
AAverin / Cmpnt.kt
Created April 11, 2023 18:22
Component-based adapter
class CmpntViewHolder<T>(val cmpnt: Cmpnt<T>, view: View) : RecyclerView.ViewHolder(view)
abstract class Cmpnt<T>(@field:LayoutRes private val cmpntLayoutId: Int) {
open fun setData(data: T) {}
protected open fun onViewCreated(view: View) {}
fun createView(inflater: LayoutInflater, parent: ViewGroup?): View {
val view = inflater.inflate(cmpntLayoutId, parent, false)
@AAverin
AAverin / main.dart
Created March 8, 2023 09:40
Bug listening to AsyncNotifierProvider
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
final rnd = Random();
void main() => runApp(ProviderScope(child: MyApp()));
@AAverin
AAverin / sharelogcat.java
Created November 24, 2015 17:44
Share logcat for Android
public void shareLogcat() {
String sdcardPath = null;
String path = Environment.getExternalStorageDirectory() + "/my_application_folder";
File dir = new File(path);
if (dir.mkdirs() || dir.isDirectory()) {
sdcardPath = path + File.separator + "logcat_" + System.currentTimeMillis() + ".log";
}
if (sdcardPath == null) {
return;
@AAverin
AAverin / KotlinRxExt.kt
Last active April 17, 2020 11:09
Kotlin Rx Extensions
import rx.Observable
import rx.Observer
import rx.Subscriber
import rx.Subscription
import rx.functions.Action0
import rx.functions.Action1
fun <T> Observable<T>.uiSubscribe(schedulers: Schedulers, subscriber: Subscriber<in T>): Subscription {
return subscribeOn(schedulers.io)
.observeOn(schedulers.mainThread)
@AAverin
AAverin / ConsistencyManager.kt
Last active October 6, 2017 17:35
A simple consistency manager implementation on Kotlin. You can subscribe on some class changes, and notify subscribers about that class being updated. Convinient when you have data being displayed in many different places and you need it up to date any time it changes, eg. somebody is making item favorite and 'star' icons should update everywhere
interface ConsistencyManagerContract {
fun notifyUpdateModel(model: Any)
fun notifyUpdateClass(kclass: KClass<*>)
fun subscribe(modelClass: KClass<*>, callback: WeakReference<(Any?) -> Unit>)
}
class ConsistencyManager : ConsistencyManagerContract {
private val subscriptionsMap: MutableMap<KClass<*>, MutableList<WeakReference<(Any?) -> Unit>>> = mutableMapOf()
override fun notifyUpdateModel(model: Any) {
class ResolvedSubscriber<T> constructor(
val resolution: Resolution,
val onNextFunc: (T) -> Unit,
val onCompletedFunc: () -> Unit = {},
val onErrorFunc: (Throwable?) -> Unit = {}) : Subscriber<T>() {
override fun onNext(p0: T) {
if (!isUnsubscribed) {
onNextFunc(p0)
}
interface RxHttpResolution {
fun onHttpException(httpException: HttpException)
fun onGenericRxException(t: Throwable)
}
interface NetworkConnectivityResolution {
fun onConnectivityAvailable()
fun onConnectivityUnavailable()
}
open class UIResolution @Inject constructor(val uiResolver: UIResolver) : ResolutionByCase() {
override fun onConnectivityAvailable() {
uiResolver.hidePersistentSnackBar()
}
override fun onConnectivityUnavailable() {
uiResolver.showPersistentSnackBar(R.string.error_no_network_connection)
}
@RunWith(PowerMockRunner::class)
@PrepareForTest(RxBus::class, CompositeSubscription::class, Observable::class)
class BusSubscriberTest {
@Mock lateinit var subscription: Subscription
@Mock lateinit var bus: RxBus
@Mock lateinit var observable: Observable<Any>
@Captor var subscriptionCaptor = ArgumentCaptor.forClass(Subscription::class.java)
interface BusSubscriberContract {
fun bus(bus: RxBus)
fun <T : Any> subscribe(clazz: KClass<T>, callback: (T) -> Unit)
fun unsubscribe()
}
open class BusSubscriber @Inject constructor() : BusSubscriberContract {
val subscriptions = mutableListOf<Subscription>()