mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-20 20:59:00 +00:00
fix error when trying to show dialog without window
This commit is contained in:
parent
6abab307ea
commit
1a121ded2f
3 changed files with 67 additions and 45 deletions
|
|
@ -0,0 +1,8 @@
|
|||
package com.habitrpg.android.habitica.events;
|
||||
|
||||
import com.habitrpg.android.habitica.models.Notification;
|
||||
|
||||
public class ShowCheckinDialog {
|
||||
public Notification notification;
|
||||
public String nextUnlockText;
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@ import android.widget.TextView;
|
|||
import com.facebook.drawee.view.SimpleDraweeView;
|
||||
import com.habitrpg.android.habitica.R;
|
||||
import com.habitrpg.android.habitica.data.ApiClient;
|
||||
import com.habitrpg.android.habitica.events.ShowCheckinDialog;
|
||||
import com.habitrpg.android.habitica.events.ShowSnackbarEvent;
|
||||
import com.habitrpg.android.habitica.models.Notification;
|
||||
import com.habitrpg.android.habitica.ui.helpers.DataBindingUtils;
|
||||
|
|
@ -50,51 +51,10 @@ public class PopupNotificationsManager {
|
|||
Boolean displayNotification(Notification notification) {
|
||||
String nextUnlockText = context.getString(R.string.nextPrizeUnlocks, notification.data.nextRewardAt);
|
||||
if (notification.data.rewardKey != null) {
|
||||
String title = notification.data.message;
|
||||
|
||||
LayoutInflater factory = LayoutInflater.from(context);
|
||||
final View view = factory.inflate(R.layout.dialog_login_incentive, null);
|
||||
|
||||
SimpleDraweeView imageView = (SimpleDraweeView) view.findViewById(R.id.imageView);
|
||||
String imageKey = notification.data.rewardKey.get(0);
|
||||
DataBindingUtils.loadImage(imageView, imageKey);
|
||||
|
||||
String youEarnedMessage = context.getString(R.string.checkInRewardEarned, notification.data.rewardText);
|
||||
|
||||
TextView titleTextView = new TextView(context);
|
||||
titleTextView.setBackgroundResource(R.color.blue_100);
|
||||
titleTextView.setTextColor(ContextCompat.getColor(context, R.color.white));
|
||||
float density = context.getResources().getDisplayMetrics().density;
|
||||
int paddingDp = (int) (16 * density);
|
||||
titleTextView.setPadding(paddingDp, paddingDp, paddingDp, paddingDp);
|
||||
titleTextView.setTextSize(18);
|
||||
titleTextView.setGravity(Gravity.CENTER_HORIZONTAL);
|
||||
titleTextView.setText(title);
|
||||
|
||||
TextView youEarnedTexView = (TextView) view.findViewById(R.id.you_earned_message);
|
||||
youEarnedTexView.setText(youEarnedMessage);
|
||||
|
||||
TextView nextUnlockTextView = (TextView) view.findViewById(R.id.next_unlock_message);
|
||||
nextUnlockTextView.setText(nextUnlockText);
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialogTheme)
|
||||
.setView(view)
|
||||
.setCustomTitle(titleTextView)
|
||||
.setPositiveButton(R.string.start_day, (dialog, which) -> {
|
||||
if (apiClient != null) {
|
||||
// @TODO: This should be handled somewhere else? MAybe we notifiy via event
|
||||
apiClient.readNotification(notification.getId())
|
||||
.subscribe(next -> {}, RxErrorHandler.handleEmptyError());
|
||||
}
|
||||
})
|
||||
.setMessage("");
|
||||
|
||||
Observable.just(null)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(o -> {
|
||||
final AlertDialog dialog = builder.create();
|
||||
dialog.show();
|
||||
}, RxErrorHandler.handleEmptyError());
|
||||
ShowCheckinDialog event = new ShowCheckinDialog();
|
||||
event.notification = notification;
|
||||
event.nextUnlockText = nextUnlockText;
|
||||
EventBus.getDefault().post(event);
|
||||
} else {
|
||||
ShowSnackbarEvent event = new ShowSnackbarEvent();
|
||||
event.title = notification.data.message;
|
||||
|
|
|
|||
|
|
@ -27,7 +27,9 @@ import android.support.v4.content.FileProvider;
|
|||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.TypedValue;
|
||||
import android.view.Gravity;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.Window;
|
||||
|
|
@ -52,6 +54,7 @@ import com.habitrpg.android.habitica.events.HabitScoreEvent;
|
|||
import com.habitrpg.android.habitica.events.OpenMysteryItemEvent;
|
||||
import com.habitrpg.android.habitica.events.SelectClassEvent;
|
||||
import com.habitrpg.android.habitica.events.ShareEvent;
|
||||
import com.habitrpg.android.habitica.events.ShowCheckinDialog;
|
||||
import com.habitrpg.android.habitica.events.ShowSnackbarEvent;
|
||||
import com.habitrpg.android.habitica.events.commands.BuyRewardCommand;
|
||||
import com.habitrpg.android.habitica.events.commands.ChecklistCheckedCommand;
|
||||
|
|
@ -120,6 +123,8 @@ import java.util.Map;
|
|||
import javax.inject.Inject;
|
||||
|
||||
import butterknife.BindView;
|
||||
import rx.Observable;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static com.habitrpg.android.habitica.interactors.NotifyUserUseCase.MIN_LEVEL_FOR_SKILLS;
|
||||
|
|
@ -931,4 +936,53 @@ public class MainActivity extends BaseActivity implements TutorialView.OnTutoria
|
|||
public boolean isAppBarExpanded() {
|
||||
return (appBar.getHeight() - appBar.getBottom()) == 0;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void showCheckinDialog(ShowCheckinDialog event) {
|
||||
String title = event.notification.data.message;
|
||||
|
||||
LayoutInflater factory = LayoutInflater.from(this);
|
||||
final View view = factory.inflate(R.layout.dialog_login_incentive, null);
|
||||
|
||||
SimpleDraweeView imageView = (SimpleDraweeView) view.findViewById(R.id.imageView);
|
||||
String imageKey = event.notification.data.rewardKey.get(0);
|
||||
DataBindingUtils.loadImage(imageView, imageKey);
|
||||
|
||||
String youEarnedMessage = this.getString(R.string.checkInRewardEarned, event.notification.data.rewardText);
|
||||
|
||||
TextView titleTextView = new TextView(this);
|
||||
titleTextView.setBackgroundResource(R.color.blue_100);
|
||||
titleTextView.setTextColor(ContextCompat.getColor(this, R.color.white));
|
||||
float density = this.getResources().getDisplayMetrics().density;
|
||||
int paddingDp = (int) (16 * density);
|
||||
titleTextView.setPadding(paddingDp, paddingDp, paddingDp, paddingDp);
|
||||
titleTextView.setTextSize(18);
|
||||
titleTextView.setGravity(Gravity.CENTER_HORIZONTAL);
|
||||
titleTextView.setText(title);
|
||||
|
||||
TextView youEarnedTexView = (TextView) view.findViewById(R.id.you_earned_message);
|
||||
youEarnedTexView.setText(youEarnedMessage);
|
||||
|
||||
TextView nextUnlockTextView = (TextView) view.findViewById(R.id.next_unlock_message);
|
||||
nextUnlockTextView.setText(event.nextUnlockText);
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme)
|
||||
.setView(view)
|
||||
.setCustomTitle(titleTextView)
|
||||
.setPositiveButton(R.string.start_day, (dialog, which) -> {
|
||||
if (apiClient != null) {
|
||||
// @TODO: This should be handled somewhere else? MAybe we notifiy via event
|
||||
apiClient.readNotification(event.notification.getId())
|
||||
.subscribe(next -> {}, RxErrorHandler.handleEmptyError());
|
||||
}
|
||||
})
|
||||
.setMessage("");
|
||||
|
||||
Observable.just(null)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(o -> {
|
||||
final AlertDialog dialog = builder.create();
|
||||
dialog.show();
|
||||
}, RxErrorHandler.handleEmptyError());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue