This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Calculator { | |
//obviously a bit silly for a calculator but you get the idea, just think of this as business logic/data/state management | |
fun add(v1: Int, v2: Int) { | |
return v1 + v2 | |
} | |
} | |
//in this case going to make the presenter stateful, even though in a real apps you shouldn't | |
class SomePresenter(val calculator: Calculator) { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BindView<V: View>(@IdRes val id: Int): ReadOnlyProperty<Any, V> { | |
private var view: V? = null | |
override fun getValue(thisRef: Any, property: KProperty<*>): V { | |
return view ?: getViewInitial(thisRef) | |
} | |
private fun getViewInitial(thisRef: Any): V { | |
val foundView: V = when (thisRef) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class SearchByTagRequest(val tag: String) | |
data class ImageColor(val hexValue: String) //some validation on the value here | |
data class SearchByColorRequest(val includedColors: List<ImageColor>, val excludedColors: List<ImageColor>) | |
interface ImageSearcher { | |
fun searchForImageByTag(requests: Observable<SearchByTagRequest>): Observable<SearchResults> | |
fun searchForImageByColors(requests: Observable<SearchByColorRequest>): Observable<SearchResults> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//bad | |
interface ShoppingCart { | |
fun observe(): Observable<ShoppingCart> | |
fun setShoppingCart(cart: ShoppingCart) | |
} | |
//good | |
interface ShoppingCart { | |
fun observe(): Observable<ShoppingCart> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//many ways to slice this up, just an example | |
//syntax may be missing something, been writing Kotlin for a couple years now | |
//also this sort of thing is general a much better API with RxJava but here's a basic implementation. | |
public interface CitiesRepo { | |
//could give an executor here or something to let caller dictate threading/scheduling context | |
Cancelable getCities(AsyncCallback<List<Cities> callback); | |
} | |
//Could just be a class as well. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface UserLocationTracker { | |
fun getUserLocation(): String | |
} | |
class AndroidUserLocationTracker(val context: Context): UserLocationTracker { | |
@Override | |
fun getUserLocation(): String { | |
context.getUserLocationOrWhatever() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
using RxJava2, I'd like to "join" two lists. | |
I have a List<ObjectA> and List<ObjectB> whereas ObjectA (val id: String, val something: Int) and ObjectB(val id: String, val somethingElse: Boolean) and want to combine them to an ObjectC (val id: String, val something: Int, val somethingElse: Boolean but in such a way, where it will only join elements, if their ids are equal and all of that possibly in the most efficient way | |
think of an sql-join for this | |
Below is an example implementation: | |
should be noted depending on specifics this might be a very | |
wierd thing to do (some data is live and reactive, other potentially stale etc.) | |
also the runtime perfomance would vary depending on if these lists were ordered or not (and how many items) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Presenter<V> { | |
fun getView(): V | |
fun attachView(view: V) | |
fun detachView() | |
} | |
/* | |
Functional style of handling view null-checking/handling | |
Functions apply methods on the view, or pass view as argument to functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <T> BehaviorRelay<T>.withInitIfEmpty(initializer: () -> Single<T>): Observable<T> { | |
if (hasValue()) return hide() | |
else return initializer.invoke().flatMapObservable { | |
accept(it) | |
hide() | |
} | |
} | |
fun <T> RxPaperBook.writeThenForward(key: String, value: T, consumer: Consumer<T>): Completable { | |
return write(key, value).doOnComplete { consumer.accept(value) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PersistedLoginDetailsRepo(internal val persistence: RxPaperBook): LoginDetailsRepo { | |
internal object PersistenceKeys { | |
val details = "details" | |
val method = "method" | |
} | |
internal val detailsRelay = BehaviorRelay.create<LoginDetails>() | |
internal val methodRelay = BehaviorRelay.create<LoginMethod>() |