Add basic switch for day/night mode

This commit is contained in:
pokkst 2022-09-08 21:08:39 -05:00
parent 967345e1c3
commit 5d93041ee3
No known key found for this signature in database
GPG Key ID: 90C2ED85E67A50FF
6 changed files with 61 additions and 7 deletions

View File

@ -21,6 +21,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.NightmodeHelper;
import java.io.File;
@ -37,6 +38,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);

View File

@ -4,10 +4,12 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
@ -15,6 +17,8 @@ import com.m2049r.xmrwallet.R;
import com.m2049r.xmrwallet.model.Wallet;
import com.m2049r.xmrwallet.model.WalletManager;
import com.m2049r.xmrwallet.service.BlockchainService;
import com.m2049r.xmrwallet.util.DayNightMode;
import com.m2049r.xmrwallet.util.NightmodeHelper;
public class SettingsFragment extends Fragment {
@ -31,7 +35,10 @@ public class SettingsFragment extends Fragment {
super.onViewCreated(view, savedInstanceState);
mViewModel = new ViewModelProvider(this).get(SettingsViewModel.class);
Wallet wallet = WalletManager.getInstance().getWallet();
TextView walletInfoTextView = view.findViewById(R.id.wallet_info_textview);
SwitchCompat nightModeSwitch = view.findViewById(R.id.day_night_switch);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Seed: " + wallet.getSeed("")+"\n\n");
stringBuilder.append("Private view-key: " + wallet.getSecretViewKey()+"\n\n");
@ -39,5 +46,14 @@ public class SettingsFragment extends Fragment {
stringBuilder.append("Wallet height: " + wallet.getBlockChainHeight() + "\n\n");
stringBuilder.append("Daemon height: " + BlockchainService.getInstance().getDaemonHeight() + "\n\n");
walletInfoTextView.setText(stringBuilder.toString());
nightModeSwitch.setChecked(NightmodeHelper.getPreferredNightmode(getContext()) == DayNightMode.NIGHT);
nightModeSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
if(b) {
NightmodeHelper.setAndSavePreferredNightmode(getContext(), DayNightMode.NIGHT);
} else {
NightmodeHelper.setAndSavePreferredNightmode(getContext(), DayNightMode.DAY);
}
});
}
}

View File

@ -26,17 +26,22 @@ import com.m2049r.xmrwallet.R;
public class NightmodeHelper {
public static DayNightMode getPreferredNightmode(Context context) {
return DayNightMode.valueOf(PreferenceManager.getDefaultSharedPreferences(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 void setPreferredNightmode(Context context) {
final DayNightMode mode = DayNightMode.valueOf(PreferenceManager.getDefaultSharedPreferences(context)
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)
setAndSavePreferredNightmode(context, DayNightMode.AUTO);
else
setNightMode(mode);
if(mode == DayNightMode.UNKNOWN) {
mode = DayNightMode.NIGHT;
}
setNightMode(mode);
}
public static void setAndSavePreferredNightmode(Context context, DayNightMode mode) {

View File

@ -24,4 +24,13 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/day_night_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginBottom="24dp"
android:layout_marginStart="24dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyMaterialTheme" parent="Theme.MaterialComponents.DayNight">
<item name="android:windowLightStatusBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="materialCardViewStyle">@style/AppCard</item>
<item name="materialButtonStyle">@style/AppButton</item>
</style>
</resources>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyMaterialTheme" parent="Theme.MaterialComponents.DayNight">
<item name="android:windowLightStatusBar">true</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="materialCardViewStyle">@style/AppCard</item>
<item name="materialButtonStyle">@style/AppButton</item>
</style>
</resources>