Skip to content

Instantly share code, notes, and snippets.

View scottroemeschke's full-sized avatar

Scott Roe-Meschke scottroemeschke

  • Snap
  • Denver, Colorado
View GitHub Profile
@scottroemeschke
scottroemeschke / CalculatorMVP.kt
Created February 4, 2018 19:55
very basic example of M in mvp
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) {
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) {
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>
}
//bad
interface ShoppingCart {
fun observe(): Observable<ShoppingCart>
fun setShoppingCart(cart: ShoppingCart)
}
//good
interface ShoppingCart {
fun observe(): Observable<ShoppingCart>
//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.
@scottroemeschke
scottroemeschke / Basicinterfacelocation.kt
Last active January 9, 2018 01:08
part of androiddev weekly questions help
interface UserLocationTracker {
fun getUserLocation(): String
}
class AndroidUserLocationTracker(val context: Context): UserLocationTracker {
@Override
fun getUserLocation(): String {
context.getUserLocationOrWhatever()
}
@scottroemeschke
scottroemeschke / MergingObsAndNotObs.kt
Created October 29, 2017 17:29
Example of combining observable<list<T> and a list field, where they merge together sharing an id, from reddit question
/*
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)
@scottroemeschke
scottroemeschke / PresenterViewRefInlineFuncs.kt
Created October 19, 2017 16:40
Just an example of handling view null checks in presenter in a low boilerplate, functional style with Kotlin (inline functions, extension functions, lambda arguments, etc)
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.
@scottroemeschke
scottroemeschke / Extensions.kt
Created May 21, 2017 20:06
kotlin extensions for kotlin komparisons: login details
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) }
@scottroemeschke
scottroemeschke / PersistedLoginDetailsRepo.kt
Last active May 21, 2017 20:51
kotlin implementation of persisted login details repo
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>()