Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Marchuck
Marchuck / GattUpdatesHelper.java
Created March 25, 2017 18:36
enable or disable gatt subscriptions
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 June 30, 2017 21:56
example of rxPlayer implementation
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;
CatPresenter{
BehaviorSubject<List<Cat>> catsSubject = BehaviorSubject.create();
Disposable catsDisposable;
void onCreate(){
someClient.subscribe( cats -> { catsSubject.onNext(cats) })
@Marchuck
Marchuck / ReactiveScrollView.java
Created August 2, 2017 15:35
reactive scrollview (able to detect scroll gesture)
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;
<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"
interface StarWarsApi {
@GET("people/")
fun getPeople(@Query("page") page: Int): Deferred<PeopleResponse>
}
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()
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?>) {
interface PagedListProvider<Value> {
fun provide() : LiveData<PagedList<Value>>
}
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