Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
sembruk committed Nov 15, 2024
2 parents d321fc3 + 3317eaa commit 5886d36
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 7 deletions.
12 changes: 10 additions & 2 deletions app/src/main/java/org/sportiduino/app/FragmentStationSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class FragmentStationSettings extends NfcFragment {
private Timer timer;
private SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener;
private View currentView;
private int countdownTimer;

@Override
public View onCreateView(
Expand All @@ -67,10 +68,13 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(requireActivity());
updatePasswordFromSharedPreferences(sharedPref);
updateCountdownTimerFromSharedPreferences(sharedPref);

preferenceChangeListener = (sharedPreferences, key) -> {
if (key.equals("password")) {
updatePasswordFromSharedPreferences(sharedPreferences);
} else if (key.equals("countdown_timer")) {
updateCountdownTimerFromSharedPreferences(sharedPreferences);
}
};
sharedPref.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
Expand Down Expand Up @@ -171,6 +175,10 @@ private void updatePasswordFromSharedPreferences(SharedPreferences sharedPrefere
binding.mpNewPassword3.setText(String.valueOf(password.getValue(2)));
}

private void updateCountdownTimerFromSharedPreferences(SharedPreferences sharedPreferences) {
countdownTimer = sharedPreferences.getInt("countdown_timer", getResources().getInteger(R.integer.countdown_timer_default));
}

private void updateWakeupTime() {
binding.textViewWakeupDate.setText(DateFormat.getDateInstance().format(wakeupTime.getTime()));
binding.textViewWakeupTime.setText(DateFormat.getTimeInstance(DateFormat.SHORT).format(wakeupTime.getTime()));
Expand Down Expand Up @@ -281,7 +289,7 @@ private MasterCard getMasterCard(CardAdapter adapter) {
masterCard.dataForWriting = MasterCard.packStationNumber(stationNumber);
} else if (binding.radioButtonMasterTime.isChecked()) {
Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, 2);
c.add(Calendar.SECOND, countdownTimer);
masterCard.dataForWriting = MasterCard.packTime(c);
} else if (binding.radioButtonMasterSleep.isChecked()) {
masterCard.dataForWriting = MasterCard.packTime(wakeupTime);
Expand Down Expand Up @@ -314,7 +322,7 @@ private void startCountdownTimer() {
}
timer = new Timer();
CountdownTimerTask countdownTimerTask = new CountdownTimerTask();
timerCount = 2;
timerCount = countdownTimer;
timer.scheduleAtFixedRate(countdownTimerTask, 0, 1000);
}

Expand Down
62 changes: 62 additions & 0 deletions app/src/main/java/org/sportiduino/app/NumberPickerPreference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.sportiduino.app;

import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import android.widget.NumberPicker;

import androidx.preference.DialogPreference;

public class NumberPickerPreference extends DialogPreference {

private NumberPicker picker;
private Integer initialValue;
//private int minValue = 0; // Default minimum
//private int maxValue = 10; // Default maximum

public NumberPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.preference_number_picker);

// Read custom min and max attributes from XML if provided
//TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumberPickerPreference);
//minValue = a.getInt(R.styleable.NumberPickerPreference_minValue, minValue);
//maxValue = a.getInt(R.styleable.NumberPickerPreference_maxValue, maxValue);
//a.recycle();
}

@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
int def = defaultValue instanceof Integer ? (Integer)defaultValue : 0;
initialValue = restorePersistedValue ? getPersistedInt(def) : def;
setSummary(initialValue.toString());
}

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getInt(index, 0);
}

public int getValue() {
return initialValue != null ? initialValue : 0;
}

public void setValue(int value) {
initialValue = value;
persistInt(value);
setSummary(initialValue.toString());
}

//public int getMinValue() {
// return minValue;
//}

//public int getMaxValue() {
// return maxValue;
//}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.sportiduino.app;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.NumberPicker;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceDialogFragmentCompat;

public class NumberPickerPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat {

private NumberPicker picker;

public static NumberPickerPreferenceDialogFragmentCompat newInstance(String key) {
final NumberPickerPreferenceDialogFragmentCompat fragment = new NumberPickerPreferenceDialogFragmentCompat();
final Bundle b = new Bundle(1);
b.putString(ARG_KEY, key);
fragment.setArguments(b);
return fragment;
}

@Override
protected View onCreateDialogView(@NonNull Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.preference_number_picker, null);

picker = view.findViewById(R.id.pref_num_picker);
NumberPickerPreference preference = (NumberPickerPreference) getPreference();
picker.setMinValue(getResources().getInteger(R.integer.countdown_timer_min));
picker.setMaxValue(getResources().getInteger(R.integer.countdown_timer_max));
picker.setValue(preference.getValue());
Log.d("onCreateDialogView", String.valueOf(preference.getValue()));

return view;
}

@Override
public void onDialogClosed(boolean positiveResult) {
if (positiveResult && picker != null) {
int value = picker.getValue();
Log.d("onDialogClosed", String.valueOf(value));
NumberPickerPreference preference = (NumberPickerPreference) getPreference();
if (preference.callChangeListener(value)) {
preference.setValue(value);
}
}
}
}
9 changes: 4 additions & 5 deletions app/src/main/java/org/sportiduino/app/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ public void onDisplayPreferenceDialog(Preference preference) {
// Try if the preference is one of our custom Preferences
DialogFragment dialogFragment = null;
if (preference instanceof PasswordPreference) {
dialogFragment = PasswordPreferenceDialog
.newInstance(preference.getKey());
dialogFragment = PasswordPreferenceDialog.newInstance(preference.getKey());
} else if (preference instanceof NumberPickerPreference) {
dialogFragment = NumberPickerPreferenceDialogFragmentCompat.newInstance(preference.getKey());
}

// If it was one of our custom Preferences, show its dialog
if (dialogFragment != null) {
dialogFragment.setTargetFragment(this, 0);
dialogFragment.show(this.getParentFragmentManager(),
"androidx.preference" +
".PreferenceFragment.DIALOG");
dialogFragment.show(getParentFragmentManager(),"androidx.preference.PreferenceFragment.DIALOG");
}
// Could not be handled here. Try with the super method.
else {
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/layout/preference_number_picker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical">

<android.widget.NumberPicker
android:id="@+id/pref_num_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@
<string name="enable_fast_punch_when_clear">Вкл. быструю отметку при очистке</string>
<string name="fast_punch">Быстрая отметка</string>
<string name="cleaning_only">Только очистка</string>
<string name="countdown_timer">Таймер обратного отсчёта, сек</string>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/integers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="countdown_timer_default">2</integer>
<integer name="countdown_timer_max">10</integer>
<integer name="countdown_timer_min">0</integer>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@
<string name="enable_fast_punch_when_clear">Enable fast punch when clear</string>
<string name="fast_punch">Fast punch</string>
<string name="cleaning_only">Cleaning only</string>
<string name="countdown_timer">Countdown timer, sec</string>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
android:title="@string/password"
android:summary="" />

<org.sportiduino.app.NumberPickerPreference
android:key="countdown_timer"
android:title="@string/countdown_timer"
android:summary=""
android:defaultValue="2"
/>

<SwitchPreferenceCompat
android:key="night_mode"
android:defaultValue="false"
Expand Down

0 comments on commit 5886d36

Please sign in to comment.