Skip to content

Commit

Permalink
Merge pull request #12 from myadzel/issue/11
Browse files Browse the repository at this point in the history
App appearance in night mode
  • Loading branch information
sembruk authored Nov 15, 2024
2 parents cad2e2f + e9f5a86 commit d321fc3
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 60 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/org/sportiduino/app/FragmentReadCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

public class FragmentReadCard extends NfcFragment implements IntentReceiver {
private FragmentReadCardBinding binding;
private View currentView;

@Override
public View onCreateView(
Expand All @@ -34,6 +35,7 @@ public View onCreateView(
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.currentView = view;

binding.textViewInfo.setText(R.string.bring_card);
binding.layoutTagInfo.setVisibility(View.GONE);
Expand Down Expand Up @@ -100,7 +102,7 @@ protected void onPostExecute(Boolean result) {
binding.textViewTagType.setText(String.format(getString(R.string.tag_type_s), card.adapter.tagType.name()));
binding.textViewInfo.setText(card.parseData(buffer));
} else {
binding.textViewInfo.setText(Util.error(getString(R.string.reading_card_failed)));
binding.textViewInfo.setText(Util.error(getString(R.string.reading_card_failed), currentView));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class FragmentStationSettings extends NfcFragment {
private int timerCount;
private Timer timer;
private SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener;
private View currentView;

@Override
public View onCreateView(
Expand All @@ -62,6 +63,7 @@ public View onCreateView(
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.currentView = view;

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(requireActivity());
updatePasswordFromSharedPreferences(sharedPref);
Expand Down Expand Up @@ -360,12 +362,12 @@ protected Boolean doInBackground(Void... params) {
@Override
protected void onPostExecute(Boolean result) {
if (result) {
binding.textViewNfcInfo.setText(Util.ok(getString(R.string.data_written_to_card_successfully)));
binding.textViewNfcInfo.setText(Util.ok(getString(R.string.data_written_to_card_successfully), currentView));
if (binding.radioButtonMasterTime.isChecked()) {
startCountdownTimer();
}
} else {
binding.textViewNfcInfo.setText(Util.error(getString(R.string.writing_card_failed)));
binding.textViewNfcInfo.setText(Util.error(getString(R.string.writing_card_failed), currentView));
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/org/sportiduino/app/FragmentWriteCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
public class FragmentWriteCard extends NfcFragment {
private FragmentWriteCardBinding binding;
private int cardNumber;
private View currentView;

@Override
public View onCreateView(
Expand All @@ -37,6 +38,7 @@ public View onCreateView(
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.currentView = view;

binding.textViewNfcInfo.setText(R.string.bring_card);

Expand Down Expand Up @@ -113,12 +115,12 @@ protected Boolean doInBackground(Void... params) {
protected void onPostExecute(Boolean result) {
binding.progressBar.setVisibility(View.GONE);
if (result) {
binding.textViewNfcInfo.setText(Util.ok(getString(R.string.data_written_to_card_successfully)));
binding.textViewNfcInfo.setText(Util.ok(getString(R.string.data_written_to_card_successfully), currentView));
if (binding.checkBoxAutoIncrement.isChecked() && !binding.checkBoxCleaningOnly.isChecked()) {
binding.editTextCardNumber.setText(String.valueOf(cardNumber + 1));
}
} else {
binding.textViewNfcInfo.setText(Util.error(getString(R.string.writing_card_failed)));
binding.textViewNfcInfo.setText(Util.error(getString(R.string.writing_card_failed), currentView));
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/java/org/sportiduino/app/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.preference.PreferenceManager;

import org.sportiduino.app.databinding.ActivityMainBinding;
Expand Down
19 changes: 18 additions & 1 deletion app/src/main/java/org/sportiduino/app/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@

import android.os.Bundle;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.DialogFragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

public class SettingsFragment extends PreferenceFragmentCompat {
public class SettingsFragment extends PreferenceFragmentCompat implements PreferenceManager.OnPreferenceTreeClickListener {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}

@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (preference.getKey().equals("night_mode")) {
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_NO) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}

return true;
}

return false;
}

@Override
public void onDisplayPreferenceDialog(Preference preference) {
// Try if the preference is one of our custom Preferences
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/org/sportiduino/app/sportiduino/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ public String toString() {
}

if (isOk()) {
return Util.coloredHtmlString(App.str(R.string.battery_ok) + voltageText, "#008000");
return Util.coloredHtmlString(App.str(R.string.battery_ok) + voltageText, Util.colorToHexCode(R.color.green));
}
return Util.coloredHtmlString(App.str(R.string.battery_low) + voltageText, "red");
return Util.coloredHtmlString(App.str(R.string.battery_low) + voltageText, Util.colorToHexCode(R.color.red));
}
}

Expand Down Expand Up @@ -166,9 +166,9 @@ public String toString() {
}
clockStr += deltaStr;
if (timestamp < (nowSec - 20) || timestamp > (nowSec + 17)) {
clockStr = Util.coloredHtmlString(clockStr, "red");
clockStr = Util.coloredHtmlString(clockStr, Util.colorToHexCode(R.color.red));
} else if (timestamp < (nowSec - 5) || timestamp > nowSec) {
clockStr = Util.coloredHtmlString(clockStr, "#FFC300");
clockStr = Util.coloredHtmlString(clockStr, Util.colorToHexCode(R.color.yellow));
}
stringState += App.str(R.string.state_clock_) + " " + clockStr;
stringState += App.str(R.string.state_alarm_) + " " + Util.dformat.format(new Date(wakeupTimestamp*1000));
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/org/sportiduino/app/sportiduino/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;

import android.view.View;
import androidx.core.content.ContextCompat;
import org.sportiduino.app.App;
import org.sportiduino.app.R;

Expand Down Expand Up @@ -60,12 +62,28 @@ public static SpannableString error(String s) {
return colorString(s, App.color(R.color.error_text_bg));
}

public static SpannableString error(String s, View view) {
int color = ContextCompat.getColor(view.getContext(), R.color.error_text_bg);

return colorString(s, color);
}

public static SpannableString ok(String s) {
return colorString(s, App.color(R.color.ok_text_bg));
}

public static SpannableString ok(String s, View view) {
int color = ContextCompat.getColor(view.getContext(), R.color.ok_text_bg);

return colorString(s, color);
}

public static String coloredHtmlString(String s, String color) {
return String.format("<font color=\"%s\">%s</font>", color, s);
}

public static String colorToHexCode(Integer color) {
return String.format("#%06x", App.color(color) & 0x00ffffff);
}
}

7 changes: 7 additions & 0 deletions app/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary">#999999</color>
<color name="secondary">#980b0b</color>
<color name="error_text_bg">@color/red</color>
<color name="ok_text_bg">@color/green</color>
</resources>
19 changes: 6 additions & 13 deletions app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<resources>
<style name="Theme.SportiduinoApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/primary_light</item>
<item name="colorPrimaryVariant">@color/primary_dark</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/primary</item>
<item name="colorSecondary">@color/secondary</item>
</style>

<style name="Theme.SportiduinoApp.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark" />
<style name="Theme.SportiduinoApp.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark" />
</resources>
23 changes: 10 additions & 13 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="primary">#343b9f</color>
<color name="primary_light">#6a65d1</color>
<color name="primary_dark">#001570</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="gray">#FF808080</color>
<color name="error_text_bg">#FFF6C0C0</color>
<color name="ok_text_bg">#FFC5E1B0</color>
<color name="primary">#333333</color>
<color name="secondary">#980b0b</color>
<color name="black">#ff000000</color>
<color name="white">#ffffffff</color>
<color name="gray">#ff808080</color>
<color name="red">#ff0000</color>
<color name="green">#008000</color>
<color name="yellow">#ffc300</color>
<color name="error_text_bg">#fff6c0c0</color>
<color name="ok_text_bg">#ffc5E1b0</color>
<color name="border_color">@color/gray</color>
</resources>
22 changes: 7 additions & 15 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<resources>
<style name="Theme.SportiduinoApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryVariant">@color/primary_dark</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<item name="android:textSize">@dimen/text_size_medium</item>
<item name="colorPrimary">@color/primary</item>
<item name="colorSecondary">@color/secondary</item>
</style>

<style name="Theme.SportiduinoApp.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="Theme.SportiduinoApp.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="Theme.SportiduinoApp.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark" />
<style name="Theme.SportiduinoApp.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="TextViewNfcInfo">
<item name="android:background">@drawable/rounded_corner</item>
<item name="android:padding">5dp</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:layout_marginBottom">5dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textAlignment">center</item>
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:android="http://schemas.android.com/apk/res/android">

<org.sportiduino.app.PasswordPreference
android:key="password"
android:title="@string/password"
android:summary="" />

<!--
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="night_mode"
android:defaultValue="false"
android:title="@string/night_mode" />
-->

</androidx.preference.PreferenceScreen>

0 comments on commit d321fc3

Please sign in to comment.