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>
<application
android:name=".MoneroApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"

View File

@ -1,9 +1,12 @@
package com.m2049r.xmrwallet;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
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.TxService;
import com.m2049r.xmrwallet.util.Constants;
import com.m2049r.xmrwallet.util.DayNightMode;
import com.m2049r.xmrwallet.util.NightmodeHelper;
import java.io.File;
@ -28,9 +32,7 @@ import java.io.File;
public class MainActivity extends AppCompatActivity implements MoneroHandlerThread.Listener, PasswordBottomSheetDialog.PasswordListener {
public final SingleLiveEvent restartEvents = new SingleLiveEvent();
private MoneroHandlerThread thread = null;
private TxService txService = null;
private BalanceService balanceService = null;
private AddressService addressService = null;
private HistoryService historyService = null;
private BlockchainService blockchainService = null;
@ -38,10 +40,7 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NightmodeHelper.getAndSetPreferredNightmode(this);
File walletFile = new File(getApplicationInfo().dataDir, Constants.WALLET_NAME);
new PrefService(this);
if (walletFile.exists()) {
boolean promptPassword = PrefService.getInstance().getBoolean(Constants.PREF_USES_PASSWORD, false);
if (!promptPassword) {
@ -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) {
FragmentActivity activity = this;
FragmentManager fm = activity.getSupportFragmentManager();
@ -73,11 +77,11 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre
public void init(File walletFile, String password) {
Wallet wallet = WalletManager.getInstance().openWallet(walletFile.getAbsolutePath(), password);
thread = new MoneroHandlerThread("WalletService", this, wallet);
this.txService = new TxService(this, thread);
this.balanceService = new BalanceService(this, thread);
this.addressService = new AddressService(this, thread);
this.historyService = new HistoryService(this, thread);
this.blockchainService = new BlockchainService(this, thread);
new TxService(thread);
this.balanceService = new BalanceService(thread);
new AddressService(thread);
this.historyService = new HistoryService(thread);
this.blockchainService = new BlockchainService(thread);
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");
walletInfoTextView.setText(stringBuilder.toString());
nightModeSwitch.setChecked(NightmodeHelper.getPreferredNightmode(getContext()) == DayNightMode.NIGHT);
nightModeSwitch.setChecked(NightmodeHelper.getPreferredNightmode() == DayNightMode.NIGHT);
nightModeSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
if(b) {
NightmodeHelper.setAndSavePreferredNightmode(getContext(), DayNightMode.NIGHT);
NightmodeHelper.setAndSavePreferredNightmode(DayNightMode.NIGHT);
} else {
NightmodeHelper.setAndSavePreferredNightmode(getContext(), DayNightMode.DAY);
NightmodeHelper.setAndSavePreferredNightmode(DayNightMode.DAY);
}
});

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,4 +5,5 @@ public class Constants {
public static final String MNEMONIC_LANGUAGE = "English";
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_NIGHT_MODE = "pref_night_mode";
}

View File

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