Skip to content

Instantly share code, notes, and snippets.

@Marchuck
Marchuck / GattUpdatesHelper.java
Created Mar 25, 2017
enable or disable gatt subscriptions
View GattUpdatesHelper.java
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.UUID;
/**
* Helper which enables or disables BLE gatt updates from connected peripheral, not tested in production
*/
@Marchuck
Marchuck / RxPlayer.java
Created Jun 30, 2017
example of rxPlayer implementation
View RxPlayer.java
package xD;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.util.Log;
import hugo.weaving.DebugLog;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
View CatPresenter.java
CatPresenter{
BehaviorSubject<List<Cat>> catsSubject = BehaviorSubject.create();
Disposable catsDisposable;
void onCreate(){
someClient.subscribe( cats -> { catsSubject.onNext(cats) })
@Marchuck
Marchuck / ReactiveScrollView.java
Created Aug 2, 2017
reactive scrollview (able to detect scroll gesture)
View ReactiveScrollView.java
package evalu.com.evalu.utils.rx;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;
import io.reactivex.subjects.PublishSubject;
View ANDROID
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/textview_bold"
android:layout_marginBottom="@dimen/activity_small_padding"
android:layout_marginLeft="@dimen/activity_default_padding"
android:layout_marginRight="@dimen/activity_default_padding"
View StarWarsApi.kt
interface StarWarsApi {
@GET("people/")
fun getPeople(@Query("page") page: Int): Deferred<PeopleResponse>
}
View GetPeopleUseCase.kt
class GetPeopleUseCase(private val api: StarWarsApi) {
fun execute(page: Int): Deferred<List<Person?>?> {
return async { transform(page) }
}
private suspend fun transform(page: Int): List<Person?> {
val response = api.getPeople(page).await()
return response.results ?: emptyList()
View SwapiPeopleDataSource.kt
class SwapiPeopleDataSource(val useCase: GetPeopleUseCase) : PageKeyedDataSource<Int, Person?>() {
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, Person?>) {
async {
val items = useCase.execute(page = 1).await()
callback.onResult(items.orEmpty(), null, 2)
}
}
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Person?>) {
View PagedListProvider.kt
interface PagedListProvider<Value> {
fun provide() : LiveData<PagedList<Value>>
}
View PaginationItemCallback.kt
object PaginationItemCallback : DiffUtil.ItemCallback<Person?>() {
override fun areItemsTheSame(oldItem: Person?, newItem: Person?): Boolean {
if (oldItem == null || newItem == null) return false
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: Person?, newItem: Person?): Boolean {
if (oldItem == null || newItem == null) return false
return oldItem.name == newItem.name