mirror of
https://github.com/k0shk0sh/FastHub.git
synced 2026-01-25 14:47:05 +00:00
made promo codes more easier to handle
This commit is contained in:
parent
7721b758af
commit
b4ea0d2f51
56
app/src/main/java/com/fastaccess/data/dao/ProUsersModel.java
Normal file
56
app/src/main/java/com/fastaccess/data/dao/ProUsersModel.java
Normal file
@ -0,0 +1,56 @@
|
||||
package com.fastaccess.data.dao;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
/**
|
||||
* Created by Hashemsergani on 03.10.17.
|
||||
*/
|
||||
|
||||
public class ProUsersModel implements Parcelable {
|
||||
private int count;
|
||||
private boolean allowed;
|
||||
private int type;
|
||||
|
||||
public int getCount() { return count;}
|
||||
|
||||
public void setCount(int count) { this.count = count;}
|
||||
|
||||
public boolean isAllowed() { return allowed;}
|
||||
|
||||
public void setAllowed(boolean allowed) { this.allowed = allowed;}
|
||||
|
||||
public int getType() { return type;}
|
||||
|
||||
public void setType(int type) { this.type = type;}
|
||||
|
||||
@Override public String toString() {
|
||||
return "ProUsersModel{" +
|
||||
", count=" + count +
|
||||
", allowed=" + allowed +
|
||||
", type=" + type +
|
||||
'}';
|
||||
}
|
||||
|
||||
public ProUsersModel() {}
|
||||
|
||||
@Override public int describeContents() { return 0; }
|
||||
|
||||
@Override public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeInt(this.count);
|
||||
dest.writeByte(this.allowed ? (byte) 1 : (byte) 0);
|
||||
dest.writeInt(this.type);
|
||||
}
|
||||
|
||||
protected ProUsersModel(Parcel in) {
|
||||
this.count = in.readInt();
|
||||
this.allowed = in.readByte() != 0;
|
||||
this.type = in.readInt();
|
||||
}
|
||||
|
||||
public static final Creator<ProUsersModel> CREATOR = new Creator<ProUsersModel>() {
|
||||
@Override public ProUsersModel createFromParcel(Parcel source) {return new ProUsersModel(source);}
|
||||
|
||||
@Override public ProUsersModel[] newArray(int size) {return new ProUsersModel[size];}
|
||||
};
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package com.github.b3er.rxfirebase.common;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import com.google.android.gms.tasks.OnCompleteListener;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
import io.reactivex.CompletableEmitter;
|
||||
import io.reactivex.SingleEmitter;
|
||||
|
||||
public final class GmsTaskListeners {
|
||||
|
||||
private GmsTaskListeners() {
|
||||
throw new AssertionError("No instances");
|
||||
}
|
||||
|
||||
public static <T> OnCompleteListener<T> listener(@NonNull final SingleEmitter<T> emitter) {
|
||||
return new OnCompleteListener<T>() {
|
||||
@Override public void onComplete(@NonNull Task<T> task) {
|
||||
if (!task.isSuccessful()) {
|
||||
if (!emitter.isDisposed()) {
|
||||
emitter.onError(task.getException());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!emitter.isDisposed()) {
|
||||
emitter.onSuccess(task.getResult());
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static OnCompleteListener<Void> listener(@NonNull final CompletableEmitter emitter) {
|
||||
return new OnCompleteListener<Void>() {
|
||||
@Override public void onComplete(@NonNull Task<Void> task) {
|
||||
if (!task.isSuccessful()) {
|
||||
if (!emitter.isDisposed()) {
|
||||
emitter.onError(task.getException());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!emitter.isDisposed()) {
|
||||
emitter.onComplete();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -1,9 +1,12 @@
|
||||
package com.fastaccess.ui.modules.main.premium
|
||||
|
||||
import com.fastaccess.data.dao.ProUsersModel
|
||||
import com.fastaccess.helper.Logger
|
||||
import com.fastaccess.helper.PrefGetter
|
||||
import com.fastaccess.helper.RxHelper
|
||||
import com.fastaccess.ui.base.mvp.presenter.BasePresenter
|
||||
import com.github.b3er.rxfirebase.database.data
|
||||
import com.github.b3er.rxfirebase.database.rxUpdateChildren
|
||||
import com.google.firebase.database.FirebaseDatabase
|
||||
import com.google.firebase.database.GenericTypeIndicator
|
||||
import io.reactivex.Observable
|
||||
@ -15,32 +18,43 @@ import io.reactivex.Observable
|
||||
class PremiumPresenter : BasePresenter<PremiumMvp.View>(), PremiumMvp.Presenter {
|
||||
override fun onCheckPromoCode(promo: String) {
|
||||
val ref = FirebaseDatabase.getInstance().reference
|
||||
manageDisposable(RxHelper.getObservable(ref.child("promoCodes")
|
||||
manageDisposable(RxHelper.getObservable(ref.child("fasthub_pro").child(promo)
|
||||
.data()
|
||||
.toObservable())
|
||||
.doOnSubscribe { sendToView { it.showProgress(0) } }
|
||||
.flatMap {
|
||||
var exists: Boolean? = false
|
||||
var user = ProUsersModel()
|
||||
Logger.e(it.exists(), it.hasChildren(), it.value)
|
||||
if (it.exists()) {
|
||||
val gti = object : GenericTypeIndicator<ArrayList<String>>() {}
|
||||
val map = it.getValue(gti)
|
||||
exists = map?.contains(promo)
|
||||
}
|
||||
return@flatMap Observable.just(exists)
|
||||
}
|
||||
.doOnComplete { sendToView { it.hideProgress() } }
|
||||
.subscribe({
|
||||
when (it) {
|
||||
true -> sendToView {
|
||||
if (promo.contains("student")) {
|
||||
val gti = object : GenericTypeIndicator<ProUsersModel>() {}
|
||||
user = it.getValue(gti) ?: ProUsersModel()
|
||||
Logger.e(user)
|
||||
if (user.isAllowed) {
|
||||
if (user.type == 1) {
|
||||
PrefGetter.setProItems()
|
||||
user.isAllowed = false
|
||||
user.count = user.count + 1
|
||||
return@flatMap ref.child("fasthub_pro").rxUpdateChildren(hashMapOf(Pair(promo, user)))
|
||||
.toObservable<ProUsersModel>()
|
||||
.map { true }
|
||||
} else {
|
||||
PrefGetter.setProItems()
|
||||
PrefGetter.setEnterpriseItem()
|
||||
user.count = user.count + 1
|
||||
return@flatMap ref.child("fasthub_pro").rxUpdateChildren(hashMapOf(Pair(promo, user)))
|
||||
.toObservable<ProUsersModel>()
|
||||
.map { true }
|
||||
}
|
||||
it.onSuccessfullyActivated()
|
||||
}
|
||||
else -> sendToView { it.onNoMatch() }
|
||||
}
|
||||
return@flatMap Observable.just(user.isAllowed)
|
||||
}
|
||||
.doOnComplete { sendToView { it.hideProgress() } }
|
||||
.subscribe({
|
||||
if (it) {
|
||||
sendToView { it.onSuccessfullyActivated() }
|
||||
} else {
|
||||
sendToView { it.onNoMatch() }
|
||||
}
|
||||
}, ::println))
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user