1
0
Fork 0

code cleanup

This commit is contained in:
sspanak 2025-05-10 14:34:45 +03:00 committed by Dimo Karaivanov
parent 97baa394fe
commit 442fd06611
7 changed files with 56 additions and 88 deletions

View file

@ -20,7 +20,6 @@ import io.github.sspanak.tt9.ime.modes.InputModeKind;
import io.github.sspanak.tt9.languages.LanguageCollection;
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
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.util.Logger;
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) {
int result = super.onStartCommand(intent, flags, startId);
String message = intent != null ? intent.getStringExtra(PopupDialog.INTENT_CLOSE) : null;
if (message == null) {
return result;
}
forceShowWindow();
if (!message.isEmpty()) {
UI.toastLong(this, message);
String wakeupCommand = intent != null ? intent.getStringExtra(UI.COMMAND_WAKEUP_MAIN) : null;
if (UI.COMMAND_WAKEUP_MAIN.equals(wakeupCommand)) {
forceShowWindow();
}
return result;

View file

@ -16,6 +16,7 @@ import java.util.HashMap;
import io.github.sspanak.tt9.preferences.PreferencesActivity;
public class UI {
public static final String COMMAND_WAKEUP_MAIN = "tt9.command.wakeup";
private static final HashMap<String, Toast> singleToasts = new HashMap<>();

View file

@ -10,11 +10,10 @@ import io.github.sspanak.tt9.db.DataStore;
import io.github.sspanak.tt9.ime.TraditionalT9;
import io.github.sspanak.tt9.languages.Language;
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.main.MainView;
public class AddWordDialog extends ThemedPopupDialog {
public class AddWordDialog extends PopupDialog {
@Nullable private final MainView mainView;
@NonNull private final Language language;
@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) {
super(tt9, null, R.style.TTheme_AddWord);
super(tt9, R.style.TTheme_AddWord);
mainView = tt9.getMainView();
title = tt9.getResources().getString(R.string.add_word_title);
@ -36,16 +35,6 @@ public class AddWordDialog extends ThemedPopupDialog {
this.word = word;
}
@Override
protected void close() {
if (popup != null) {
popup.dismiss();
popup = null;
}
}
private void onOK() {
close();
DataStore.put(
@ -56,7 +45,6 @@ public class AddWordDialog extends ThemedPopupDialog {
}
@Override
public void show() {
if (word == null || word.isEmpty()) {
UI.toastLong(context, R.string.add_word_no_selection);
@ -69,13 +57,6 @@ public class AddWordDialog extends ThemedPopupDialog {
return;
}
popup = new PopupBuilder(context)
.setCancelable(true)
.setTitle(title)
.setMessage(message)
.setPositiveButton(OKLabel, this::onOK)
.setNegativeButton(true, null)
.setOnKeyListener(this)
.showFromIme(mainView);
render(this::onOK, null, null);
}
}

View file

@ -18,12 +18,11 @@ import io.github.sspanak.tt9.languages.Language;
import io.github.sspanak.tt9.languages.LanguageCollection;
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
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.util.ConsumerCompat;
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 MainView mainView;
private final SettingsStore settings;
@ -34,7 +33,7 @@ public class ChangeLanguageDialog extends ThemedPopupDialog {
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);
OKLabel = null;
@ -102,10 +101,7 @@ public class ChangeLanguageDialog extends ThemedPopupDialog {
@Override
protected void close() {
detachRadioButtons();
if (popup != null) {
popup.dismiss();
popup = null;
}
super.close();
}
@ -144,13 +140,6 @@ public class ChangeLanguageDialog extends ThemedPopupDialog {
public void show() {
popup = new PopupBuilder(context)
.setCancelable(true)
.setTitle(title)
.setMessage(message)
.setNegativeButton(true, this::close)
.setOnKeyListener(this)
.setView(generateRadioButtons())
.showFromIme(mainView);
render(null, this::close, generateRadioButtons());
}
}

View file

@ -1,32 +1,47 @@
package io.github.sspanak.tt9.ui.dialogs;
import android.content.Context;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.View;
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 {
public static final String INTENT_CLOSE = "tt9.popup_dialog.close";
public static final String PARAMETER_DIALOG_TYPE = "popup_type";
protected final ContextThemeWrapper context;
private final MainView mainView;
protected final Context context;
protected final ConsumerCompat<String> activityFinisher;
protected Dialog popup;
protected String title;
protected String message;
protected String OKLabel;
PopupDialog(@NonNull Context context, ConsumerCompat<String> activityFinisher) {
this.activityFinisher = activityFinisher;
this.context = context;
PopupDialog(@NonNull TraditionalT9 tt9, int theme) {
this.context = new ThemedContextBuilder()
.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() {
if (activityFinisher != null) {
activityFinisher.accept("");
if (popup != null) {
popup.dismiss();
popup = null;
}
}
@ -35,5 +50,21 @@ abstract public class PopupDialog implements DialogInterface.OnKeyListener {
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);
}
}

View file

@ -49,7 +49,7 @@ public class RequestPermissionDialog extends AppCompatActivity {
private void reviveMain() {
Intent intent = new Intent(this, TraditionalT9.class);
intent.putExtra(PopupDialog.INTENT_CLOSE, "");
intent.putExtra(UI.COMMAND_WAKEUP_MAIN, UI.COMMAND_WAKEUP_MAIN);
startService(intent);
}

View file

@ -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
);
}
}