notifications code cleanup
This commit is contained in:
parent
fda074b7e0
commit
17f1681be5
6 changed files with 110 additions and 61 deletions
|
|
@ -24,8 +24,8 @@ import io.github.sspanak.tt9.languages.Language;
|
|||
import io.github.sspanak.tt9.languages.exceptions.InvalidLanguageCharactersException;
|
||||
import io.github.sspanak.tt9.languages.exceptions.InvalidLanguageException;
|
||||
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
|
||||
import io.github.sspanak.tt9.ui.DictionaryLoadingBar;
|
||||
import io.github.sspanak.tt9.ui.dialogs.ConfirmDictionaryUpdateDialog;
|
||||
import io.github.sspanak.tt9.ui.notifications.DictionaryLoadingBar;
|
||||
import io.github.sspanak.tt9.util.ConsumerCompat;
|
||||
import io.github.sspanak.tt9.util.Logger;
|
||||
import io.github.sspanak.tt9.util.Timer;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import io.github.sspanak.tt9.R;
|
|||
import io.github.sspanak.tt9.db.exporter.AbstractExporter;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
import io.github.sspanak.tt9.preferences.items.ItemClickable;
|
||||
import io.github.sspanak.tt9.ui.DictionaryNotification;
|
||||
import io.github.sspanak.tt9.ui.notifications.DictionaryProgressNotification;
|
||||
|
||||
abstract class ItemExportAbstract extends ItemClickable {
|
||||
final protected PreferencesActivity activity;
|
||||
|
|
@ -59,12 +59,12 @@ abstract class ItemExportAbstract extends ItemClickable {
|
|||
setReadyStatus();
|
||||
|
||||
if (outputFile == null) {
|
||||
DictionaryNotification.getInstance(activity).showError(
|
||||
DictionaryProgressNotification.getInstance(activity).showError(
|
||||
activity.getString(R.string.dictionary_export_failed),
|
||||
activity.getString(R.string.dictionary_export_failed_more_info)
|
||||
);
|
||||
} else {
|
||||
DictionaryNotification.getInstance(activity).showMessage(
|
||||
DictionaryProgressNotification.getInstance(activity).showMessage(
|
||||
activity.getString(R.string.dictionary_export_finished),
|
||||
activity.getString(R.string.dictionary_export_finished_more_info, outputFile),
|
||||
activity.getString(R.string.dictionary_export_finished_more_info, outputFile)
|
||||
|
|
@ -80,7 +80,7 @@ abstract class ItemExportAbstract extends ItemClickable {
|
|||
|
||||
String loadingMessage = getExporter().getStatusMessage();
|
||||
item.setSummary(loadingMessage);
|
||||
DictionaryNotification.getInstance(activity).showLoadingMessage(loadingMessage, "");
|
||||
DictionaryProgressNotification.getInstance(activity).showLoadingMessage(loadingMessage, "");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ import io.github.sspanak.tt9.languages.Language;
|
|||
import io.github.sspanak.tt9.languages.LanguageCollection;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
import io.github.sspanak.tt9.preferences.items.ItemClickable;
|
||||
import io.github.sspanak.tt9.ui.DictionaryLoadingBar;
|
||||
import io.github.sspanak.tt9.ui.UI;
|
||||
import io.github.sspanak.tt9.ui.notifications.DictionaryLoadingBar;
|
||||
|
||||
|
||||
class ItemLoadDictionary extends ItemClickable {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package io.github.sspanak.tt9.ui;
|
||||
package io.github.sspanak.tt9.ui.notifications;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
|
|
@ -9,13 +9,13 @@ import java.util.Locale;
|
|||
|
||||
import io.github.sspanak.tt9.R;
|
||||
import io.github.sspanak.tt9.db.exceptions.DictionaryImportException;
|
||||
import io.github.sspanak.tt9.languages.exceptions.InvalidLanguageCharactersException;
|
||||
import io.github.sspanak.tt9.languages.exceptions.InvalidLanguageException;
|
||||
import io.github.sspanak.tt9.languages.Language;
|
||||
import io.github.sspanak.tt9.languages.LanguageCollection;
|
||||
import io.github.sspanak.tt9.languages.exceptions.InvalidLanguageCharactersException;
|
||||
import io.github.sspanak.tt9.languages.exceptions.InvalidLanguageException;
|
||||
|
||||
|
||||
public class DictionaryLoadingBar extends DictionaryNotification {
|
||||
public class DictionaryLoadingBar extends DictionaryProgressNotification {
|
||||
private static DictionaryLoadingBar self;
|
||||
|
||||
|
||||
|
|
@ -1,31 +1,23 @@
|
|||
package io.github.sspanak.tt9.ui;
|
||||
package io.github.sspanak.tt9.ui.notifications;
|
||||
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Build;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import io.github.sspanak.tt9.R;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
import io.github.sspanak.tt9.preferences.screens.languages.LanguagesScreen;
|
||||
|
||||
public abstract class DictionaryNotification {
|
||||
private static DictionaryNotification self;
|
||||
private static final int NOTIFICATION_ID = 1;
|
||||
private static final String NOTIFICATION_CHANNEL_ID = "dictionary-notifications";
|
||||
|
||||
private final NotificationManager manager;
|
||||
private final NotificationCompat.Builder notificationBuilder;
|
||||
protected final NotificationCompat.Builder notificationBuilder;
|
||||
protected final Resources resources;
|
||||
|
||||
protected int maxProgress = 0;
|
||||
protected int progress = 0;
|
||||
protected boolean indeterminate = false;
|
||||
protected String title = "";
|
||||
protected String message = "";
|
||||
|
|
@ -45,23 +37,7 @@ public abstract class DictionaryNotification {
|
|||
.setOnlyAlertOnce(true);
|
||||
}
|
||||
|
||||
|
||||
public static DictionaryNotification getInstance(Context context) {
|
||||
if (self == null) {
|
||||
self = new DictionaryNotification(context) {
|
||||
};
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
private PendingIntent createNavigationIntent(Context context) {
|
||||
Intent intent = new Intent(context, PreferencesActivity.class);
|
||||
intent.putExtra("screen", LanguagesScreen.NAME);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
return PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
|
||||
}
|
||||
|
||||
protected abstract PendingIntent createNavigationIntent(Context context);
|
||||
|
||||
private NotificationCompat.Builder getNotificationBuilderCompat(Context context) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
|
|
@ -79,7 +55,6 @@ public abstract class DictionaryNotification {
|
|||
|
||||
|
||||
public void showMessage(@NonNull String title, @NonNull String message, @NonNull String messageLong) {
|
||||
progress = maxProgress = 0;
|
||||
indeterminate = false;
|
||||
this.title = title;
|
||||
this.message = message;
|
||||
|
|
@ -88,19 +63,7 @@ public abstract class DictionaryNotification {
|
|||
}
|
||||
|
||||
|
||||
public void showLoadingMessage(@NonNull String title, @NonNull String message) {
|
||||
this.title = title;
|
||||
this.message = message;
|
||||
messageLong = "";
|
||||
indeterminate = true;
|
||||
progress = 1;
|
||||
maxProgress = 2;
|
||||
renderMessage();
|
||||
}
|
||||
|
||||
|
||||
public void showError(@NonNull String title, @NonNull String message) {
|
||||
progress = maxProgress = 0;
|
||||
indeterminate = false;
|
||||
this.title = title;
|
||||
this.message = message;
|
||||
|
|
@ -109,16 +72,10 @@ public abstract class DictionaryNotification {
|
|||
|
||||
|
||||
protected void hide() {
|
||||
progress = maxProgress = 0;
|
||||
manager.cancel(NOTIFICATION_ID);
|
||||
}
|
||||
|
||||
|
||||
public boolean inProgress() {
|
||||
return progress < maxProgress;
|
||||
}
|
||||
|
||||
|
||||
protected void renderError() {
|
||||
NotificationCompat.BigTextStyle bigMessage = new NotificationCompat.BigTextStyle();
|
||||
bigMessage.setBigContentTitle(title);
|
||||
|
|
@ -129,8 +86,7 @@ public abstract class DictionaryNotification {
|
|||
.setContentTitle(title)
|
||||
.setContentText(message)
|
||||
.setOngoing(false)
|
||||
.setStyle(bigMessage)
|
||||
.setProgress(maxProgress, progress, false);
|
||||
.setStyle(bigMessage);
|
||||
|
||||
manager.notify(NOTIFICATION_ID, notificationBuilder.build());
|
||||
}
|
||||
|
|
@ -142,9 +98,6 @@ public abstract class DictionaryNotification {
|
|||
bigMessage.bigText(messageLong.isEmpty() ? message : messageLong);
|
||||
|
||||
notificationBuilder
|
||||
.setSmallIcon(inProgress() ? android.R.drawable.stat_notify_sync : R.drawable.ic_done)
|
||||
.setOngoing(inProgress())
|
||||
.setProgress(maxProgress, progress, indeterminate)
|
||||
.setStyle(bigMessage)
|
||||
.setContentTitle(title)
|
||||
.setContentText(message);
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
package io.github.sspanak.tt9.ui.notifications;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import io.github.sspanak.tt9.R;
|
||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||
import io.github.sspanak.tt9.preferences.screens.languages.LanguagesScreen;
|
||||
|
||||
public class DictionaryProgressNotification extends DictionaryNotification {
|
||||
private static DictionaryProgressNotification self;
|
||||
|
||||
protected int maxProgress = 0;
|
||||
protected int progress = 0;
|
||||
|
||||
|
||||
protected DictionaryProgressNotification(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
|
||||
public static DictionaryProgressNotification getInstance(Context context) {
|
||||
if (self == null) {
|
||||
self = new DictionaryProgressNotification(context) {
|
||||
};
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected final PendingIntent createNavigationIntent(Context context) {
|
||||
Intent intent = new Intent(context, PreferencesActivity.class);
|
||||
intent.putExtra("screen", LanguagesScreen.NAME);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
return PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void showMessage(@NonNull String title, @NonNull String message, @NonNull String messageLong) {
|
||||
progress = maxProgress = 0;
|
||||
super.showMessage(title, message, messageLong);
|
||||
}
|
||||
|
||||
|
||||
public void showLoadingMessage(@NonNull String title, @NonNull String message) {
|
||||
this.title = title;
|
||||
this.message = message;
|
||||
messageLong = "";
|
||||
indeterminate = true;
|
||||
progress = 1;
|
||||
maxProgress = 2;
|
||||
renderMessage();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void showError(@NonNull String title, @NonNull String message) {
|
||||
progress = maxProgress = 0;
|
||||
super.showError(title, message);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void hide() {
|
||||
progress = maxProgress = 0;
|
||||
super.hide();
|
||||
}
|
||||
|
||||
|
||||
public boolean inProgress() {
|
||||
return progress < maxProgress;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void renderError() {
|
||||
notificationBuilder.setProgress(maxProgress, progress, false);
|
||||
super.renderError();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void renderMessage() {
|
||||
notificationBuilder
|
||||
.setSmallIcon(inProgress() ? android.R.drawable.stat_notify_sync : R.drawable.ic_done)
|
||||
.setOngoing(inProgress())
|
||||
.setProgress(maxProgress, progress, indeterminate);
|
||||
|
||||
super.renderMessage();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue