Clean up some code and fix issue with dark/light mode initialization

This commit is contained in:
pokkst 2022-09-10 14:42:43 -05:00
parent d66e8976a5
commit 069970ea23
No known key found for this signature in database
GPG Key ID: 90C2ED85E67A50FF
13 changed files with 58 additions and 55 deletions

View File

@ -29,6 +29,7 @@
</queries> </queries>
<application <application
android:name=".MoneroApplication"
android:allowBackup="false" android:allowBackup="false"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"

View File

@ -1,9 +1,12 @@
package com.m2049r.xmrwallet; package com.m2049r.xmrwallet;
import android.os.Bundle; import android.os.Bundle;
import android.os.PersistableBundle;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.navigation.fragment.NavHostFragment; import androidx.navigation.fragment.NavHostFragment;
@ -21,6 +24,7 @@ import com.m2049r.xmrwallet.service.MoneroHandlerThread;
import com.m2049r.xmrwallet.service.PrefService; import com.m2049r.xmrwallet.service.PrefService;
import com.m2049r.xmrwallet.service.TxService; import com.m2049r.xmrwallet.service.TxService;
import com.m2049r.xmrwallet.util.Constants; import com.m2049r.xmrwallet.util.Constants;
import com.m2049r.xmrwallet.util.DayNightMode;
import com.m2049r.xmrwallet.util.NightmodeHelper; import com.m2049r.xmrwallet.util.NightmodeHelper;
import java.io.File; import java.io.File;
@ -28,9 +32,7 @@ import java.io.File;
public class MainActivity extends AppCompatActivity implements MoneroHandlerThread.Listener, PasswordBottomSheetDialog.PasswordListener { public class MainActivity extends AppCompatActivity implements MoneroHandlerThread.Listener, PasswordBottomSheetDialog.PasswordListener {
public final SingleLiveEvent restartEvents = new SingleLiveEvent(); public final SingleLiveEvent restartEvents = new SingleLiveEvent();
private MoneroHandlerThread thread = null; private MoneroHandlerThread thread = null;
private TxService txService = null;
private BalanceService balanceService = null; private BalanceService balanceService = null;
private AddressService addressService = null;
private HistoryService historyService = null; private HistoryService historyService = null;
private BlockchainService blockchainService = null; private BlockchainService blockchainService = null;
@ -38,13 +40,10 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
NightmodeHelper.getAndSetPreferredNightmode(this);
File walletFile = new File(getApplicationInfo().dataDir, Constants.WALLET_NAME); File walletFile = new File(getApplicationInfo().dataDir, Constants.WALLET_NAME);
new PrefService(this); if (walletFile.exists()) {
if(walletFile.exists()) {
boolean promptPassword = PrefService.getInstance().getBoolean(Constants.PREF_USES_PASSWORD, false); boolean promptPassword = PrefService.getInstance().getBoolean(Constants.PREF_USES_PASSWORD, false);
if(!promptPassword) { if (!promptPassword) {
init(walletFile, ""); init(walletFile, "");
} else { } else {
PasswordBottomSheetDialog passwordDialog = new PasswordBottomSheetDialog(); PasswordBottomSheetDialog passwordDialog = new PasswordBottomSheetDialog();
@ -56,6 +55,11 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre
} }
} }
@Override
public void onPostCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
}
private void navigate(int destination) { private void navigate(int destination) {
FragmentActivity activity = this; FragmentActivity activity = this;
FragmentManager fm = activity.getSupportFragmentManager(); FragmentManager fm = activity.getSupportFragmentManager();
@ -73,11 +77,11 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre
public void init(File walletFile, String password) { public void init(File walletFile, String password) {
Wallet wallet = WalletManager.getInstance().openWallet(walletFile.getAbsolutePath(), password); Wallet wallet = WalletManager.getInstance().openWallet(walletFile.getAbsolutePath(), password);
thread = new MoneroHandlerThread("WalletService", this, wallet); thread = new MoneroHandlerThread("WalletService", this, wallet);
this.txService = new TxService(this, thread); new TxService(thread);
this.balanceService = new BalanceService(this, thread); this.balanceService = new BalanceService(thread);
this.addressService = new AddressService(this, thread); new AddressService(thread);
this.historyService = new HistoryService(this, thread); this.historyService = new HistoryService(thread);
this.blockchainService = new BlockchainService(this, thread); this.blockchainService = new BlockchainService(thread);
thread.start(); thread.start();
} }

View File

@ -0,0 +1,15 @@
package com.m2049r.xmrwallet;
import android.app.Application;
import com.m2049r.xmrwallet.service.PrefService;
import com.m2049r.xmrwallet.util.NightmodeHelper;
public class MoneroApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
new PrefService(this);
NightmodeHelper.getAndSetPreferredNightmode();
}
}

View File

@ -54,12 +54,12 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
stringBuilder.append("Daemon height: " + BlockchainService.getInstance().getDaemonHeight() + "\n\n"); stringBuilder.append("Daemon height: " + BlockchainService.getInstance().getDaemonHeight() + "\n\n");
walletInfoTextView.setText(stringBuilder.toString()); walletInfoTextView.setText(stringBuilder.toString());
nightModeSwitch.setChecked(NightmodeHelper.getPreferredNightmode(getContext()) == DayNightMode.NIGHT); nightModeSwitch.setChecked(NightmodeHelper.getPreferredNightmode() == DayNightMode.NIGHT);
nightModeSwitch.setOnCheckedChangeListener((compoundButton, b) -> { nightModeSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
if(b) { if(b) {
NightmodeHelper.setAndSavePreferredNightmode(getContext(), DayNightMode.NIGHT); NightmodeHelper.setAndSavePreferredNightmode(DayNightMode.NIGHT);
} else { } else {
NightmodeHelper.setAndSavePreferredNightmode(getContext(), DayNightMode.DAY); NightmodeHelper.setAndSavePreferredNightmode(DayNightMode.DAY);
} }
}); });

View File

@ -13,8 +13,8 @@ public class AddressService extends ServiceBase {
return instance; return instance;
} }
public AddressService(MainActivity mainActivity, MoneroHandlerThread thread) { public AddressService(MoneroHandlerThread thread) {
super(mainActivity, thread); super(thread);
instance = this; instance = this;
} }

View File

@ -18,8 +18,8 @@ public class BalanceService extends ServiceBase {
private final MutableLiveData<Long> _lockedBalance = new MutableLiveData<>(0L); private final MutableLiveData<Long> _lockedBalance = new MutableLiveData<>(0L);
public LiveData<Long> lockedBalance = _lockedBalance; public LiveData<Long> lockedBalance = _lockedBalance;
public BalanceService(MainActivity mainActivity, MoneroHandlerThread thread) { public BalanceService(MoneroHandlerThread thread) {
super(mainActivity, thread); super(thread);
instance = this; instance = this;
} }

View File

@ -18,8 +18,8 @@ public class BlockchainService extends ServiceBase {
private final MutableLiveData<Long> _currentHeight = new MutableLiveData<>(0L); private final MutableLiveData<Long> _currentHeight = new MutableLiveData<>(0L);
public LiveData<Long> height = _currentHeight; public LiveData<Long> height = _currentHeight;
public BlockchainService(MainActivity mainActivity, MoneroHandlerThread thread) { public BlockchainService(MoneroHandlerThread thread) {
super(mainActivity, thread); super(thread);
instance = this; instance = this;
} }

View File

@ -20,8 +20,8 @@ public class HistoryService extends ServiceBase {
private final MutableLiveData<List<TransactionInfo>> _history = new MutableLiveData<>(); private final MutableLiveData<List<TransactionInfo>> _history = new MutableLiveData<>();
public LiveData<List<TransactionInfo>> history = _history; public LiveData<List<TransactionInfo>> history = _history;
public HistoryService(MainActivity mainActivity, MoneroHandlerThread thread) { public HistoryService(MoneroHandlerThread thread) {
super(mainActivity, thread); super(thread);
instance = this; instance = this;
} }

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import com.m2049r.xmrwallet.MainActivity; import com.m2049r.xmrwallet.MainActivity;
import com.m2049r.xmrwallet.MoneroApplication;
public class PrefService extends ServiceBase { public class PrefService extends ServiceBase {
public static SharedPreferences instance = null; public static SharedPreferences instance = null;
@ -12,8 +13,8 @@ public class PrefService extends ServiceBase {
return instance; return instance;
} }
public PrefService(MainActivity mainActivity) { public PrefService(MoneroApplication application) {
super(mainActivity, null); super(null);
instance = mainActivity.getSharedPreferences(mainActivity.getApplicationInfo().packageName, Context.MODE_PRIVATE); instance = application.getSharedPreferences(application.getApplicationInfo().packageName, Context.MODE_PRIVATE);
} }
} }

View File

@ -1,20 +1,12 @@
package com.m2049r.xmrwallet.service; package com.m2049r.xmrwallet.service;
import com.m2049r.xmrwallet.MainActivity;
public class ServiceBase { public class ServiceBase {
private final MainActivity mainActivity;
private final MoneroHandlerThread thread; private final MoneroHandlerThread thread;
public ServiceBase(MainActivity mainActivity, MoneroHandlerThread thread) { public ServiceBase(MoneroHandlerThread thread) {
this.mainActivity = mainActivity;
this.thread = thread; this.thread = thread;
} }
public MainActivity getMainActivity() {
return mainActivity;
}
public MoneroHandlerThread getThread() { public MoneroHandlerThread getThread() {
return thread; return thread;
} }

View File

@ -10,8 +10,8 @@ public class TxService extends ServiceBase {
return instance; return instance;
} }
public TxService(MainActivity mainActivity, MoneroHandlerThread thread) { public TxService(MoneroHandlerThread thread) {
super(mainActivity, thread); super(thread);
instance = this; instance = this;
} }

View File

@ -5,4 +5,5 @@ public class Constants {
public static final String MNEMONIC_LANGUAGE = "English"; public static final String MNEMONIC_LANGUAGE = "English";
public static final String PREF_USES_PASSWORD = "pref_uses_password"; public static final String PREF_USES_PASSWORD = "pref_uses_password";
public static final String PREF_USES_TOR = "pref_uses_tor"; public static final String PREF_USES_TOR = "pref_uses_tor";
public static final String PREF_NIGHT_MODE = "pref_night_mode";
} }

View File

@ -23,30 +23,19 @@ import android.preference.PreferenceManager;
import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.app.AppCompatDelegate;
import com.m2049r.xmrwallet.R; import com.m2049r.xmrwallet.R;
import com.m2049r.xmrwallet.service.PrefService;
public class NightmodeHelper { public class NightmodeHelper {
public static DayNightMode getPreferredNightmode(Context context) { public static DayNightMode getPreferredNightmode() {
DayNightMode mode = DayNightMode.valueOf(PreferenceManager.getDefaultSharedPreferences(context) return DayNightMode.valueOf(PrefService.getInstance().getString(Constants.PREF_NIGHT_MODE, DayNightMode.NIGHT.name()));
.getString(context.getString(R.string.preferred_nightmode), "UNKNOWN"));
if(mode == DayNightMode.UNKNOWN) {
mode = DayNightMode.NIGHT;
}
return mode;
} }
public static void getAndSetPreferredNightmode(Context context) { public static void getAndSetPreferredNightmode() {
DayNightMode mode = DayNightMode.valueOf(PreferenceManager.getDefaultSharedPreferences(context) setNightMode(getPreferredNightmode());
.getString(context.getString(R.string.preferred_nightmode), "UNKNOWN"));
if(mode == DayNightMode.UNKNOWN) {
mode = DayNightMode.NIGHT;
}
setNightMode(mode);
} }
public static void setAndSavePreferredNightmode(Context context, DayNightMode mode) { public static void setAndSavePreferredNightmode(DayNightMode mode) {
PreferenceManager.getDefaultSharedPreferences(context).edit() PrefService.getInstance().edit().putString(Constants.PREF_NIGHT_MODE, mode.name()).apply();
.putString(context.getString(R.string.preferred_nightmode), mode.name()).apply();
setNightMode(mode); setNightMode(mode);
} }