code cleanup
This commit is contained in:
parent
97baa394fe
commit
442fd06611
7 changed files with 56 additions and 88 deletions
|
|
@ -20,7 +20,6 @@ import io.github.sspanak.tt9.ime.modes.InputModeKind;
|
||||||
import io.github.sspanak.tt9.languages.LanguageCollection;
|
import io.github.sspanak.tt9.languages.LanguageCollection;
|
||||||
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
|
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
|
||||||
import io.github.sspanak.tt9.ui.UI;
|
import io.github.sspanak.tt9.ui.UI;
|
||||||
import io.github.sspanak.tt9.ui.dialogs.PopupDialog;
|
|
||||||
import io.github.sspanak.tt9.ui.dialogs.RequestPermissionDialog;
|
import io.github.sspanak.tt9.ui.dialogs.RequestPermissionDialog;
|
||||||
import io.github.sspanak.tt9.util.Logger;
|
import io.github.sspanak.tt9.util.Logger;
|
||||||
import io.github.sspanak.tt9.util.sys.DeviceInfo;
|
import io.github.sspanak.tt9.util.sys.DeviceInfo;
|
||||||
|
|
@ -111,15 +110,9 @@ public class TraditionalT9 extends MainViewHandler {
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
int result = super.onStartCommand(intent, flags, startId);
|
int result = super.onStartCommand(intent, flags, startId);
|
||||||
|
|
||||||
String message = intent != null ? intent.getStringExtra(PopupDialog.INTENT_CLOSE) : null;
|
String wakeupCommand = intent != null ? intent.getStringExtra(UI.COMMAND_WAKEUP_MAIN) : null;
|
||||||
if (message == null) {
|
if (UI.COMMAND_WAKEUP_MAIN.equals(wakeupCommand)) {
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
forceShowWindow();
|
forceShowWindow();
|
||||||
|
|
||||||
if (!message.isEmpty()) {
|
|
||||||
UI.toastLong(this, message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ import java.util.HashMap;
|
||||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||||
|
|
||||||
public class UI {
|
public class UI {
|
||||||
|
public static final String COMMAND_WAKEUP_MAIN = "tt9.command.wakeup";
|
||||||
private static final HashMap<String, Toast> singleToasts = new HashMap<>();
|
private static final HashMap<String, Toast> singleToasts = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,10 @@ import io.github.sspanak.tt9.db.DataStore;
|
||||||
import io.github.sspanak.tt9.ime.TraditionalT9;
|
import io.github.sspanak.tt9.ime.TraditionalT9;
|
||||||
import io.github.sspanak.tt9.languages.Language;
|
import io.github.sspanak.tt9.languages.Language;
|
||||||
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
|
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
|
||||||
import io.github.sspanak.tt9.ui.PopupBuilder;
|
|
||||||
import io.github.sspanak.tt9.ui.UI;
|
import io.github.sspanak.tt9.ui.UI;
|
||||||
import io.github.sspanak.tt9.ui.main.MainView;
|
import io.github.sspanak.tt9.ui.main.MainView;
|
||||||
|
|
||||||
public class AddWordDialog extends ThemedPopupDialog {
|
public class AddWordDialog extends PopupDialog {
|
||||||
@Nullable private final MainView mainView;
|
@Nullable private final MainView mainView;
|
||||||
@NonNull private final Language language;
|
@NonNull private final Language language;
|
||||||
@NonNull private final SettingsStore settings;
|
@NonNull private final SettingsStore settings;
|
||||||
|
|
@ -24,7 +23,7 @@ public class AddWordDialog extends ThemedPopupDialog {
|
||||||
|
|
||||||
|
|
||||||
public AddWordDialog(@NonNull TraditionalT9 tt9, @NonNull Language language, @Nullable String word) {
|
public AddWordDialog(@NonNull TraditionalT9 tt9, @NonNull Language language, @Nullable String word) {
|
||||||
super(tt9, null, R.style.TTheme_AddWord);
|
super(tt9, R.style.TTheme_AddWord);
|
||||||
mainView = tt9.getMainView();
|
mainView = tt9.getMainView();
|
||||||
|
|
||||||
title = tt9.getResources().getString(R.string.add_word_title);
|
title = tt9.getResources().getString(R.string.add_word_title);
|
||||||
|
|
@ -36,16 +35,6 @@ public class AddWordDialog extends ThemedPopupDialog {
|
||||||
this.word = word;
|
this.word = word;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void close() {
|
|
||||||
if (popup != null) {
|
|
||||||
popup.dismiss();
|
|
||||||
popup = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void onOK() {
|
private void onOK() {
|
||||||
close();
|
close();
|
||||||
DataStore.put(
|
DataStore.put(
|
||||||
|
|
@ -56,7 +45,6 @@ public class AddWordDialog extends ThemedPopupDialog {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void show() {
|
public void show() {
|
||||||
if (word == null || word.isEmpty()) {
|
if (word == null || word.isEmpty()) {
|
||||||
UI.toastLong(context, R.string.add_word_no_selection);
|
UI.toastLong(context, R.string.add_word_no_selection);
|
||||||
|
|
@ -69,13 +57,6 @@ public class AddWordDialog extends ThemedPopupDialog {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
popup = new PopupBuilder(context)
|
render(this::onOK, null, null);
|
||||||
.setCancelable(true)
|
|
||||||
.setTitle(title)
|
|
||||||
.setMessage(message)
|
|
||||||
.setPositiveButton(OKLabel, this::onOK)
|
|
||||||
.setNegativeButton(true, null)
|
|
||||||
.setOnKeyListener(this)
|
|
||||||
.showFromIme(mainView);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,11 @@ import io.github.sspanak.tt9.languages.Language;
|
||||||
import io.github.sspanak.tt9.languages.LanguageCollection;
|
import io.github.sspanak.tt9.languages.LanguageCollection;
|
||||||
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
|
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
|
||||||
import io.github.sspanak.tt9.ui.LanguageRadioButton;
|
import io.github.sspanak.tt9.ui.LanguageRadioButton;
|
||||||
import io.github.sspanak.tt9.ui.PopupBuilder;
|
|
||||||
import io.github.sspanak.tt9.ui.main.MainView;
|
import io.github.sspanak.tt9.ui.main.MainView;
|
||||||
import io.github.sspanak.tt9.util.ConsumerCompat;
|
import io.github.sspanak.tt9.util.ConsumerCompat;
|
||||||
import io.github.sspanak.tt9.util.sys.DeviceInfo;
|
import io.github.sspanak.tt9.util.sys.DeviceInfo;
|
||||||
|
|
||||||
public class ChangeLanguageDialog extends ThemedPopupDialog {
|
public class ChangeLanguageDialog extends PopupDialog {
|
||||||
private final ArrayList<Language> languages;
|
private final ArrayList<Language> languages;
|
||||||
private final MainView mainView;
|
private final MainView mainView;
|
||||||
private final SettingsStore settings;
|
private final SettingsStore settings;
|
||||||
|
|
@ -34,7 +33,7 @@ public class ChangeLanguageDialog extends ThemedPopupDialog {
|
||||||
|
|
||||||
|
|
||||||
public ChangeLanguageDialog(@NonNull TraditionalT9 tt9, @Nullable ConsumerCompat<Integer> changeHandler) {
|
public ChangeLanguageDialog(@NonNull TraditionalT9 tt9, @Nullable ConsumerCompat<Integer> changeHandler) {
|
||||||
super(tt9, null, R.style.TTheme_AddWord);
|
super(tt9, R.style.TTheme_AddWord);
|
||||||
|
|
||||||
title = tt9.getResources().getString(R.string.language_popup_title);
|
title = tt9.getResources().getString(R.string.language_popup_title);
|
||||||
OKLabel = null;
|
OKLabel = null;
|
||||||
|
|
@ -102,10 +101,7 @@ public class ChangeLanguageDialog extends ThemedPopupDialog {
|
||||||
@Override
|
@Override
|
||||||
protected void close() {
|
protected void close() {
|
||||||
detachRadioButtons();
|
detachRadioButtons();
|
||||||
if (popup != null) {
|
super.close();
|
||||||
popup.dismiss();
|
|
||||||
popup = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -144,13 +140,6 @@ public class ChangeLanguageDialog extends ThemedPopupDialog {
|
||||||
|
|
||||||
|
|
||||||
public void show() {
|
public void show() {
|
||||||
popup = new PopupBuilder(context)
|
render(null, this::close, generateRadioButtons());
|
||||||
.setCancelable(true)
|
|
||||||
.setTitle(title)
|
|
||||||
.setMessage(message)
|
|
||||||
.setNegativeButton(true, this::close)
|
|
||||||
.setOnKeyListener(this)
|
|
||||||
.setView(generateRadioButtons())
|
|
||||||
.showFromIme(mainView);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,47 @@
|
||||||
package io.github.sspanak.tt9.ui.dialogs;
|
package io.github.sspanak.tt9.ui.dialogs;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.app.Dialog;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
|
import android.view.ContextThemeWrapper;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import io.github.sspanak.tt9.util.ConsumerCompat;
|
import io.github.sspanak.tt9.R;
|
||||||
|
import io.github.sspanak.tt9.ime.TraditionalT9;
|
||||||
|
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
|
||||||
|
import io.github.sspanak.tt9.ui.PopupBuilder;
|
||||||
|
import io.github.sspanak.tt9.ui.main.MainView;
|
||||||
|
import io.github.sspanak.tt9.util.ThemedContextBuilder;
|
||||||
|
import io.github.sspanak.tt9.util.sys.DeviceInfo;
|
||||||
|
|
||||||
abstract public class PopupDialog implements DialogInterface.OnKeyListener {
|
abstract public class PopupDialog implements DialogInterface.OnKeyListener {
|
||||||
public static final String INTENT_CLOSE = "tt9.popup_dialog.close";
|
protected final ContextThemeWrapper context;
|
||||||
public static final String PARAMETER_DIALOG_TYPE = "popup_type";
|
private final MainView mainView;
|
||||||
|
|
||||||
|
protected Dialog popup;
|
||||||
protected final Context context;
|
|
||||||
protected final ConsumerCompat<String> activityFinisher;
|
|
||||||
protected String title;
|
protected String title;
|
||||||
protected String message;
|
protected String message;
|
||||||
protected String OKLabel;
|
protected String OKLabel;
|
||||||
|
|
||||||
PopupDialog(@NonNull Context context, ConsumerCompat<String> activityFinisher) {
|
PopupDialog(@NonNull TraditionalT9 tt9, int theme) {
|
||||||
this.activityFinisher = activityFinisher;
|
this.context = new ThemedContextBuilder()
|
||||||
this.context = context;
|
.setConfiguration(tt9.getResources().getConfiguration())
|
||||||
|
.setContext(tt9.getApplicationContext())
|
||||||
|
.setSettings(new SettingsStore(tt9))
|
||||||
|
// The main theme does not work on Android <= 11 and the _AddWord theme does not work on 12+.
|
||||||
|
// Not sure why since they inherit from the same parent, but it is what it is.
|
||||||
|
.setTheme(DeviceInfo.AT_LEAST_ANDROID_12 ? R.style.TTheme : theme)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
mainView = tt9.getMainView();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void close() {
|
protected void close() {
|
||||||
if (activityFinisher != null) {
|
if (popup != null) {
|
||||||
activityFinisher.accept("");
|
popup.dismiss();
|
||||||
|
popup = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -35,5 +50,21 @@ abstract public class PopupDialog implements DialogInterface.OnKeyListener {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract void show();
|
protected Dialog render(Runnable onOK, Runnable onCancel, View customView) {
|
||||||
|
PopupBuilder popupBuilder = new PopupBuilder(context);
|
||||||
|
if (onOK != null) {
|
||||||
|
popupBuilder.setPositiveButton(OKLabel, onOK);
|
||||||
|
}
|
||||||
|
if (customView != null) {
|
||||||
|
popupBuilder.setView(customView);
|
||||||
|
}
|
||||||
|
|
||||||
|
return popup = popupBuilder
|
||||||
|
.setCancelable(true)
|
||||||
|
.setTitle(title)
|
||||||
|
.setMessage(message)
|
||||||
|
.setNegativeButton(true, onCancel)
|
||||||
|
.setOnKeyListener(this)
|
||||||
|
.showFromIme(mainView);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ public class RequestPermissionDialog extends AppCompatActivity {
|
||||||
|
|
||||||
private void reviveMain() {
|
private void reviveMain() {
|
||||||
Intent intent = new Intent(this, TraditionalT9.class);
|
Intent intent = new Intent(this, TraditionalT9.class);
|
||||||
intent.putExtra(PopupDialog.INTENT_CLOSE, "");
|
intent.putExtra(UI.COMMAND_WAKEUP_MAIN, UI.COMMAND_WAKEUP_MAIN);
|
||||||
startService(intent);
|
startService(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
package io.github.sspanak.tt9.ui.dialogs;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
import io.github.sspanak.tt9.R;
|
|
||||||
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
|
|
||||||
import io.github.sspanak.tt9.util.ConsumerCompat;
|
|
||||||
import io.github.sspanak.tt9.util.ThemedContextBuilder;
|
|
||||||
import io.github.sspanak.tt9.util.sys.DeviceInfo;
|
|
||||||
|
|
||||||
abstract class ThemedPopupDialog extends PopupDialog {
|
|
||||||
ThemedPopupDialog(@NonNull Context context, ConsumerCompat<String> activityFinisher, int theme) {
|
|
||||||
super(
|
|
||||||
new ThemedContextBuilder()
|
|
||||||
.setConfiguration(context.getApplicationContext().getResources().getConfiguration())
|
|
||||||
.setContext(context)
|
|
||||||
.setSettings(new SettingsStore(context))
|
|
||||||
// The main theme does not work on Android <= 11 and the _AddWord theme does not work on 12+.
|
|
||||||
// Not sure why since they inherit from the same parent, but it is what it is.
|
|
||||||
.setTheme(DeviceInfo.AT_LEAST_ANDROID_12 ? R.style.TTheme : theme)
|
|
||||||
.build(),
|
|
||||||
activityFinisher
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue