Migrate to AndroidX

This commit is contained in:
Moez Bhatti
2018-06-20 19:13:10 -04:00
parent 735cd03626
commit 327bb52c60
108 changed files with 1042 additions and 239 deletions
@@ -0,0 +1,166 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.common.androidxcompat;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import com.uber.autodispose.LifecycleEndedException;
import com.uber.autodispose.LifecycleScopeProvider;
import io.reactivex.Observable;
import io.reactivex.functions.Function;
/**
* A {@link LifecycleScopeProvider} that can provide scoping for Android {@link Lifecycle} and
* {@link LifecycleOwner} classes.
* <p>
* <pre><code>
* AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner))
* </code></pre>
*/
public final class AndroidLifecycleScopeProvider
implements LifecycleScopeProvider<Lifecycle.Event> {
private static final Function<Lifecycle.Event, Lifecycle.Event> DEFAULT_CORRESPONDING_EVENTS =
new Function<Lifecycle.Event, Lifecycle.Event>() {
@Override public Lifecycle.Event apply(Lifecycle.Event lastEvent) throws Exception {
switch (lastEvent) {
case ON_CREATE:
return Lifecycle.Event.ON_DESTROY;
case ON_START:
return Lifecycle.Event.ON_STOP;
case ON_RESUME:
return Lifecycle.Event.ON_PAUSE;
case ON_PAUSE:
return Lifecycle.Event.ON_STOP;
case ON_STOP:
case ON_DESTROY:
default:
throw new LifecycleEndedException("Lifecycle has ended! Last event was " + lastEvent);
}
}
};
private final Function<Lifecycle.Event, Lifecycle.Event> boundaryResolver;
/**
* Creates a {@link AndroidLifecycleScopeProvider} for Android LifecycleOwners.
*
* @param owner the owner to scope for.
* @return a {@link AndroidLifecycleScopeProvider} against this owner.
*/
public static AndroidLifecycleScopeProvider from(LifecycleOwner owner) {
return from(owner.getLifecycle());
}
/**
* Creates a {@link AndroidLifecycleScopeProvider} for Android LifecycleOwners.
*
* @param owner the owner to scope for.
* @param untilEvent the event until the scope is valid.
* @return a {@link AndroidLifecycleScopeProvider} against this owner.
*/
public static AndroidLifecycleScopeProvider from(
LifecycleOwner owner,
Lifecycle.Event untilEvent) {
return from(owner.getLifecycle(), untilEvent);
}
/**
* Creates a {@link AndroidLifecycleScopeProvider} for Android Lifecycles.
*
* @param lifecycle the lifecycle to scope for.
* @return a {@link AndroidLifecycleScopeProvider} against this lifecycle.
*/
public static AndroidLifecycleScopeProvider from(Lifecycle lifecycle) {
return from(lifecycle, DEFAULT_CORRESPONDING_EVENTS);
}
/**
* Creates a {@link AndroidLifecycleScopeProvider} for Android Lifecycles.
*
* @param lifecycle the lifecycle to scope for.
* @param untilEvent the event until the scope is valid.
* @return a {@link AndroidLifecycleScopeProvider} against this lifecycle.
*/
public static AndroidLifecycleScopeProvider from(
Lifecycle lifecycle,
Lifecycle.Event untilEvent) {
return from(lifecycle, new UntilEventFunction(untilEvent));
}
/**
* Creates a {@link AndroidLifecycleScopeProvider} for Android Lifecycles.
*
* @param owner the owner to scope for.
* @param boundaryResolver function that resolves the event boundary.
* @return a {@link AndroidLifecycleScopeProvider} against this owner.
*/
public static AndroidLifecycleScopeProvider from(
LifecycleOwner owner,
Function<Lifecycle.Event, Lifecycle.Event> boundaryResolver) {
return from(owner.getLifecycle(), boundaryResolver);
}
/**
* Creates a {@link AndroidLifecycleScopeProvider} for Android Lifecycles.
*
* @param lifecycle the lifecycle to scope for.
* @param boundaryResolver function that resolves the event boundary.
* @return a {@link AndroidLifecycleScopeProvider} against this lifecycle.
*/
public static AndroidLifecycleScopeProvider from(
Lifecycle lifecycle,
Function<Lifecycle.Event, Lifecycle.Event> boundaryResolver) {
return new AndroidLifecycleScopeProvider(lifecycle, boundaryResolver);
}
private final LifecycleEventsObservable lifecycleObservable;
private AndroidLifecycleScopeProvider(Lifecycle lifecycle,
Function<Lifecycle.Event, Lifecycle.Event> boundaryResolver) {
this.lifecycleObservable = new LifecycleEventsObservable(lifecycle);
this.boundaryResolver = boundaryResolver;
}
@Override public Observable<Lifecycle.Event> lifecycle() {
return lifecycleObservable;
}
@Override public Function<Lifecycle.Event, Lifecycle.Event> correspondingEvents() {
return boundaryResolver;
}
@Override public Lifecycle.Event peekLifecycle() {
lifecycleObservable.backfillEvents();
return lifecycleObservable.getValue();
}
private static class UntilEventFunction implements Function<Lifecycle.Event, Lifecycle.Event> {
private final Lifecycle.Event untilEvent;
UntilEventFunction(Lifecycle.Event untilEvent) {
this.untilEvent = untilEvent;
}
@Override public Lifecycle.Event apply(Lifecycle.Event event) throws Exception {
return untilEvent;
}
}
}
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.common.androidxcompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.Event
import androidx.lifecycle.LifecycleOwner
import com.uber.autodispose.LifecycleScopeProvider
import io.reactivex.annotations.CheckReturnValue
import io.reactivex.functions.Function
/**
* Extension that returns a [LifecycleScopeProvider] for this [LifecycleOwner].
*/
@CheckReturnValue
inline fun LifecycleOwner.scope(): LifecycleScopeProvider<*> = AndroidLifecycleScopeProvider.from(this)
/**
* Extension that returns a [LifecycleScopeProvider] for this [LifecycleOwner].
*
* @param untilEvent the event until the scope is valid.
*/
@CheckReturnValue
inline fun LifecycleOwner.scope(untilEvent: Lifecycle.Event): LifecycleScopeProvider<*>
= AndroidLifecycleScopeProvider.from(this, untilEvent)
/**
* Extension that returns a [LifecycleScopeProvider] for this [LifecycleOwner].
*
* @param boundaryResolver function that resolves the event boundary.
*/
@CheckReturnValue
inline fun LifecycleOwner.scope(boundaryResolver: Function<Event, Event>): LifecycleScopeProvider<*>
= AndroidLifecycleScopeProvider.from(this, boundaryResolver)
/**
* Extension that returns a [LifecycleScopeProvider] for this [Lifecycle].
*/
@CheckReturnValue
inline fun Lifecycle.scope(): LifecycleScopeProvider<*> = AndroidLifecycleScopeProvider.from(this)
/**
* Extension that returns a [LifecycleScopeProvider] for this [Lifecycle].
*
* @param untilEvent the event until the scope is valid.
*/
@CheckReturnValue
inline fun Lifecycle.scope(untilEvent: Lifecycle.Event): LifecycleScopeProvider<*>
= AndroidLifecycleScopeProvider.from(this, untilEvent)
/**
* Extension that returns a [LifecycleScopeProvider] for this [Lifecycle].
*
* @param boundaryResolver function that resolves the event boundary.
*/
@CheckReturnValue
inline fun Lifecycle.scope(boundaryResolver: Function<Event, Event>): LifecycleScopeProvider<*>
= AndroidLifecycleScopeProvider.from(this, boundaryResolver)
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.common.androidxcompat;
import android.view.View;
import androidx.drawerlayout.widget.DrawerLayout;
import com.jakewharton.rxbinding2.InitialValueObservable;
import io.reactivex.Observer;
import io.reactivex.android.MainThreadDisposable;
import static com.jakewharton.rxbinding2.internal.Preconditions.checkMainThread;
final class DrawerLayoutDrawerOpenedObservable extends InitialValueObservable<Boolean> {
private final DrawerLayout view;
private final int gravity;
DrawerLayoutDrawerOpenedObservable(DrawerLayout view, int gravity) {
this.view = view;
this.gravity = gravity;
}
@Override protected void subscribeListener(Observer<? super Boolean> observer) {
if (!checkMainThread(observer)) {
return;
}
Listener listener = new Listener(view, gravity, observer);
observer.onSubscribe(listener);
view.addDrawerListener(listener);
}
@Override protected Boolean getInitialValue() {
return view.isDrawerOpen(gravity);
}
static final class Listener extends MainThreadDisposable implements DrawerLayout.DrawerListener {
private final DrawerLayout view;
private final int gravity;
private final Observer<? super Boolean> observer;
Listener(DrawerLayout view, int gravity, Observer<? super Boolean> observer) {
this.view = view;
this.gravity = gravity;
this.observer = observer;
}
@Override public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override public void onDrawerOpened(View drawerView) {
if (!isDisposed()) {
int drawerGravity = ((DrawerLayout.LayoutParams) drawerView.getLayoutParams()).gravity;
if (drawerGravity == gravity) {
observer.onNext(true);
}
}
}
@Override public void onDrawerClosed(View drawerView) {
if (!isDisposed()) {
int drawerGravity = ((DrawerLayout.LayoutParams) drawerView.getLayoutParams()).gravity;
if (drawerGravity == gravity) {
observer.onNext(false);
}
}
}
@Override public void onDrawerStateChanged(int newState) {
}
@Override protected void onDispose() {
view.removeDrawerListener(this);
}
}
}
@@ -0,0 +1,124 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.common.androidxcompat;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Lifecycle.Event;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.MainThreadDisposable;
import io.reactivex.subjects.BehaviorSubject;
import static androidx.annotation.RestrictTo.Scope.LIBRARY;
import static androidx.lifecycle.Lifecycle.Event.ON_CREATE;
import static androidx.lifecycle.Lifecycle.Event.ON_DESTROY;
import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
import static androidx.lifecycle.Lifecycle.Event.ON_START;
import static com.uber.autodispose.android.internal.AutoDisposeAndroidUtil.isMainThread;
@RestrictTo(LIBRARY) class LifecycleEventsObservable extends Observable<Event> {
private final Lifecycle lifecycle;
private final BehaviorSubject<Event> eventsObservable = BehaviorSubject.create();
@SuppressWarnings("CheckReturnValue") LifecycleEventsObservable(Lifecycle lifecycle) {
this.lifecycle = lifecycle;
}
Event getValue() {
return eventsObservable.getValue();
}
/**
* Backfill if already created for boundary checking. We do a trick here for corresponding events
* where we pretend something is created upon initialized state so that it assumes the
* corresponding event is DESTROY.
*/
void backfillEvents() {
@Nullable Lifecycle.Event correspondingEvent;
switch (lifecycle.getCurrentState()) {
case INITIALIZED:
correspondingEvent = ON_CREATE;
break;
case CREATED:
correspondingEvent = ON_START;
break;
case STARTED:
case RESUMED:
correspondingEvent = ON_RESUME;
break;
case DESTROYED:
default:
correspondingEvent = ON_DESTROY;
break;
}
eventsObservable.onNext(correspondingEvent);
}
@Override protected void subscribeActual(Observer<? super Event> observer) {
ArchLifecycleObserver archObserver =
new ArchLifecycleObserver(lifecycle, observer, eventsObservable);
observer.onSubscribe(archObserver);
if (!isMainThread()) {
observer.onError(
new IllegalStateException("Lifecycles can only be bound to on the main thread!"));
return;
}
lifecycle.addObserver(archObserver);
if (archObserver.isDisposed()) {
lifecycle.removeObserver(archObserver);
}
}
static final class ArchLifecycleObserver extends MainThreadDisposable
implements LifecycleObserver {
private final Lifecycle lifecycle;
private final Observer<? super Event> observer;
private final BehaviorSubject<Event> eventsObservable;
ArchLifecycleObserver(Lifecycle lifecycle, Observer<? super Event> observer,
BehaviorSubject<Event> eventsObservable) {
this.lifecycle = lifecycle;
this.observer = observer;
this.eventsObservable = eventsObservable;
}
@Override protected void onDispose() {
lifecycle.removeObserver(this);
}
@OnLifecycleEvent(Event.ON_ANY)
void onStateChange(@SuppressWarnings("unused") LifecycleOwner owner, Event event) {
if (!isDisposed()) {
if (!(event == ON_CREATE && eventsObservable.getValue() == event)) {
// Due to the INITIALIZED->ON_CREATE mapping trick we do in backfill(),
// we fire this conditionally to avoid duplicate CREATE events.
eventsObservable.onNext(event);
}
observer.onNext(event);
}
}
}
}
@@ -0,0 +1,228 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.common.androidxcompat;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import io.realm.OrderedCollectionChangeSet;
import io.realm.OrderedRealmCollection;
import io.realm.OrderedRealmCollectionChangeListener;
import io.realm.RealmList;
import io.realm.RealmModel;
import io.realm.RealmResults;
/**
* The RealmBaseRecyclerAdapter class is an abstract utility class for binding RecyclerView UI elements to Realm data.
* <p>
* This adapter will automatically handle any updates to its data and call {@code notifyDataSetChanged()},
* {@code notifyItemInserted()}, {@code notifyItemRemoved()} or {@code notifyItemRangeChanged(} as appropriate.
* <p>
* The RealmAdapter will stop receiving updates if the Realm instance providing the {@link OrderedRealmCollection} is
* closed.
*
* @param <T> type of {@link RealmModel} stored in the adapter.
* @param <S> type of RecyclerView.ViewHolder used in the adapter.
*/
public abstract class RealmRecyclerViewAdapter<T extends RealmModel, S extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<S> {
private final boolean hasAutoUpdates;
private final boolean updateOnModification;
private final OrderedRealmCollectionChangeListener listener;
@Nullable
private OrderedRealmCollection<T> adapterData;
private OrderedRealmCollectionChangeListener createListener() {
return new OrderedRealmCollectionChangeListener() {
@Override
public void onChange(Object collection, OrderedCollectionChangeSet changeSet) {
// null Changes means the async query returns the first time.
if (changeSet == null) {
notifyDataSetChanged();
return;
}
// For deletions, the adapter has to be notified in reverse order.
OrderedCollectionChangeSet.Range[] deletions = changeSet.getDeletionRanges();
for (int i = deletions.length - 1; i >= 0; i--) {
OrderedCollectionChangeSet.Range range = deletions[i];
notifyItemRangeRemoved(range.startIndex, range.length);
}
OrderedCollectionChangeSet.Range[] insertions = changeSet.getInsertionRanges();
for (OrderedCollectionChangeSet.Range range : insertions) {
notifyItemRangeInserted(range.startIndex, range.length);
}
if (!updateOnModification) {
return;
}
OrderedCollectionChangeSet.Range[] modifications = changeSet.getChangeRanges();
for (OrderedCollectionChangeSet.Range range : modifications) {
notifyItemRangeChanged(range.startIndex, range.length);
}
}
};
}
/**
* This is equivalent to {@code RealmRecyclerViewAdapter(data, autoUpdate, true)}.
*
* @see #RealmRecyclerViewAdapter(OrderedRealmCollection, boolean, boolean)
*/
public RealmRecyclerViewAdapter(@Nullable OrderedRealmCollection<T> data, boolean autoUpdate) {
this(data, autoUpdate, true);
}
/**
* @param data collection data to be used by this adapter.
* @param autoUpdate when it is {@code false}, the adapter won't be automatically updated when collection data
* changes.
* @param updateOnModification when it is {@code true}, this adapter will be updated when deletions, insertions or
* modifications happen to the collection data. When it is {@code false}, only
* deletions and insertions will trigger the updates. This param will be ignored if
* {@code autoUpdate} is {@code false}.
*/
public RealmRecyclerViewAdapter(@Nullable OrderedRealmCollection<T> data, boolean autoUpdate,
boolean updateOnModification) {
if (data != null && !data.isManaged())
throw new IllegalStateException("Only use this adapter with managed RealmCollection, " +
"for un-managed lists you can just use the BaseRecyclerViewAdapter");
this.adapterData = data;
this.hasAutoUpdates = autoUpdate;
this.listener = hasAutoUpdates ? createListener() : null;
this.updateOnModification = updateOnModification;
}
@Override
public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
if (hasAutoUpdates && isDataValid()) {
//noinspection ConstantConditions
addListener(adapterData);
}
}
@Override
public void onDetachedFromRecyclerView(final RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
if (hasAutoUpdates && isDataValid()) {
//noinspection ConstantConditions
removeListener(adapterData);
}
}
/**
* Returns the current ID for an item. Note that item IDs are not stable so you cannot rely on the item ID being the
* same after notifyDataSetChanged() or {@link #updateData(OrderedRealmCollection)} has been called.
*
* @param index position of item in the adapter.
* @return current item ID.
*/
@Override
public long getItemId(final int index) {
return index;
}
@Override
public int getItemCount() {
//noinspection ConstantConditions
return isDataValid() ? adapterData.size() : 0;
}
/**
* Returns the item associated with the specified position.
* Can return {@code null} if provided Realm instance by {@link OrderedRealmCollection} is closed.
*
* @param index index of the item.
* @return the item at the specified position, {@code null} if adapter data is not valid.
*/
@SuppressWarnings("WeakerAccess")
@Nullable
public T getItem(int index) {
//noinspection ConstantConditions
return isDataValid() ? adapterData.get(index) : null;
}
/**
* Returns data associated with this adapter.
*
* @return adapter data.
*/
@Nullable
public OrderedRealmCollection<T> getData() {
return adapterData;
}
/**
* Updates the data associated to the Adapter. Useful when the query has been changed.
* If the query does not change you might consider using the automaticUpdate feature.
*
* @param data the new {@link OrderedRealmCollection} to display.
*/
@SuppressWarnings("WeakerAccess")
public void updateData(@Nullable OrderedRealmCollection<T> data) {
if (hasAutoUpdates) {
if (isDataValid()) {
//noinspection ConstantConditions
removeListener(adapterData);
}
if (data != null) {
addListener(data);
}
}
this.adapterData = data;
notifyDataSetChanged();
}
private void addListener(@NonNull OrderedRealmCollection<T> data) {
if (data instanceof RealmResults) {
RealmResults<T> results = (RealmResults<T>) data;
//noinspection unchecked
results.addChangeListener(listener);
} else if (data instanceof RealmList) {
RealmList<T> list = (RealmList<T>) data;
//noinspection unchecked
list.addChangeListener(listener);
} else {
throw new IllegalArgumentException("RealmCollection not supported: " + data.getClass());
}
}
private void removeListener(@NonNull OrderedRealmCollection<T> data) {
if (data instanceof RealmResults) {
RealmResults<T> results = (RealmResults<T>) data;
//noinspection unchecked
results.removeChangeListener(listener);
} else if (data instanceof RealmList) {
RealmList<T> list = (RealmList<T>) data;
//noinspection unchecked
list.removeChangeListener(listener);
} else {
throw new IllegalArgumentException("RealmCollection not supported: " + data.getClass());
}
}
private boolean isDataValid() {
return adapterData != null && adapterData.isValid();
}
}
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.common.androidxcompat;
import androidx.annotation.CheckResult;
import androidx.annotation.NonNull;
import androidx.drawerlayout.widget.DrawerLayout;
import com.jakewharton.rxbinding2.InitialValueObservable;
import io.reactivex.functions.Consumer;
import static com.jakewharton.rxbinding2.internal.Preconditions.checkNotNull;
public final class RxDrawerLayout {
/**
* Create an observable of the open state of the drawer of {@code view}.
* <p>
* <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
* to free this reference.
* <p>
* <em>Note:</em> A value will be emitted immediately on subscribe.
*/
@CheckResult
@NonNull
public static InitialValueObservable<Boolean> drawerOpen(
@NonNull DrawerLayout view, int gravity) {
checkNotNull(view, "view == null");
return new DrawerLayoutDrawerOpenedObservable(view, gravity);
}
/**
* An action which sets whether the drawer with {@code gravity} of {@code view} is open.
* <p>
* <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
* to free this reference.
*/
@CheckResult @NonNull public static Consumer<? super Boolean> open(
@NonNull final DrawerLayout view, final int gravity) {
checkNotNull(view, "view == null");
return new Consumer<Boolean>() {
@Override public void accept(Boolean aBoolean) {
if (aBoolean) {
view.openDrawer(gravity);
} else {
view.closeDrawer(gravity);
}
}
};
}
private RxDrawerLayout() {
throw new AssertionError("No instances.");
}
}
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
@file:Suppress("NOTHING_TO_INLINE")
package com.moez.QKSMS.common.androidxcompat
import androidx.annotation.CheckResult
import androidx.drawerlayout.widget.DrawerLayout
import com.jakewharton.rxbinding2.InitialValueObservable
import io.reactivex.functions.Consumer
import kotlin.Int
import kotlin.Suppress
/**
* Create an observable of the open state of the drawer of `view`.
*
* *Warning:* The created observable keeps a strong reference to `view`. Unsubscribe
* to free this reference.
*
* *Note:* A value will be emitted immediately on subscribe.
*/
@CheckResult
inline fun DrawerLayout.drawerOpen(gravity: Int): InitialValueObservable<Boolean> = RxDrawerLayout.drawerOpen(this, gravity)
/**
* An action which sets whether the drawer with `gravity` of `view` is open.
*
* *Warning:* The created observable keeps a strong reference to `view`. Unsubscribe
* to free this reference.
*/
@CheckResult
inline fun DrawerLayout.open(gravity: Int): Consumer<in Boolean> = RxDrawerLayout.open(this, gravity)