Skip to content

Commit

Permalink
Merge changes from Case Study 2 - Sprint 2 for Team 2 (rilling#139)
Browse files Browse the repository at this point in the history
**Describe the pull request**
Merge changes from Case Study 2 - Sprint 2 for Team 2

**Link to the the issue**
Each commit should link to the respective issues already.

Closing rilling#58, rilling#85, rilling#110, rilling#101, rilling#94, rilling#96
  • Loading branch information
thanhpd authored Apr 2, 2024
2 parents 2a0695c + 1eff4ae commit 1f228c9
Show file tree
Hide file tree
Showing 13 changed files with 799 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package de.dennisguse.opentracks.settings;

import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.NumberPicker;

import androidx.fragment.app.DialogFragment;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Locale;

import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.models.ActivityType;
Expand All @@ -22,6 +29,14 @@ public class DefaultsSettingsFragment extends PreferenceFragmentCompat implement
getActivity().runOnUiThread(this::updateUnits);
}
};
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (preference.getKey().equals(getString(R.string.ski_season_start_key))) {
showCustomDatePickerDialog(); // Call method to show the dialog
return true;
}
return super.onPreferenceTreeClick(preference);
}

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Expand All @@ -37,8 +52,8 @@ public void onStart() {
@Override
public void onResume() {
super.onResume();
PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
updateUnits();
updateSkiSeasonStartPreferenceSummary(); // This will update the summary on resume
}

@Override
Expand All @@ -63,7 +78,101 @@ public void onDisplayPreferenceDialog(Preference preference) {

super.onDisplayPreferenceDialog(preference);
}

private void showCustomDatePickerDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
View dialogView = getLayoutInflater().inflate(R.layout.custom_date_picker_dialog, null);
builder.setView(dialogView);

NumberPicker monthPicker = dialogView.findViewById(R.id.monthPicker);
NumberPicker dayPicker = dialogView.findViewById(R.id.dayPicker);

String defaultStartDate = PreferencesUtils.getSkiSeasonStartDate();

// Initialize month picker
String[] months = new DateFormatSymbols().getMonths(); // Full months array
monthPicker.setMinValue(0);
monthPicker.setMaxValue(months.length - 1);
monthPicker.setDisplayedValues(months);

// Initialize day picker with maximum value based on the month
int month = Integer.parseInt(defaultStartDate.split("-")[0]) - 1;
int day = Integer.parseInt(defaultStartDate.split("-")[1]);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, month);
dayPicker.setMaxValue(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
dayPicker.setMinValue(1);

// Set current values
monthPicker.setValue(month);
dayPicker.setValue(Math.min(day, dayPicker.getMaxValue())); // Ensure day is within the valid range

monthPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
// Adjust the maximum number of days according to the selected month
calendar.set(Calendar.MONTH, newVal);
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
dayPicker.setMaxValue(maxDay);

// Adjust day value if it exceeds the max day for the new month
if (dayPicker.getValue() > maxDay) {
dayPicker.setValue(maxDay);
}
});

builder.setTitle("Select Date");
builder.setPositiveButton("OK", (dialog, which) -> {
// Save selected date and update summary
int selectedMonth = monthPicker.getValue();
int selectedDay = dayPicker.getValue();
String selectedDate = String.format(Locale.getDefault(), "%02d-%02d", selectedMonth + 1, selectedDay);

PreferencesUtils.setSkiSeasonStartDate(selectedDate);

// Update the preference summary
updateSkiSeasonStartPreferenceSummary();
});

builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();
}


private void ensureDefaultSkiSeasonStartDate() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
if (!prefs.contains(getString(R.string.ski_season_start_key))) {
// Set the default date only if it hasn't been set before
PreferencesUtils.setSkiSeasonStartDate("09-01");
}
}

private void updateSkiSeasonStartPreferenceSummary() {
Preference preference = findPreference(getString(R.string.ski_season_start_key));

// The ensureDefaultSkiSeasonStartDate() method makes sure that a default is always set
ensureDefaultSkiSeasonStartDate();

String date = PreferencesUtils.getSkiSeasonStartDate();
String[] dateParts = date.split("-");
int monthIndex = Integer.parseInt(dateParts[0]) - 1;
// Ensure the format is correctly applied to display as "Sep 1"
String readableDate = new DateFormatSymbols().getMonths()[monthIndex].substring(0, 3) + " " + Integer.parseInt(dateParts[1]);

if (preference != null) {
preference.setSummary(readableDate);
}
}




private int getMaxDayOfMonth(int month) {
// Get the maximum day for the given month
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.MONTH, month);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
private void updateUnits() {
UnitSystem unitSystem = PreferencesUtils.getUnitSystem();

Expand All @@ -74,7 +183,7 @@ private void updateUnits() {
case IMPERIAL_FEET, IMPERIAL_METER ->
R.array.stats_rate_imperial_options;
case NAUTICAL_IMPERIAL ->
R.array.stats_rate_nautical_options;
R.array.stats_rate_nautical_options;
};

String[] entries = getResources().getStringArray(entriesId);
Expand All @@ -89,4 +198,4 @@ public void onChooseActivityTypeDone(ActivityType activityType) {
activityPreferenceDialog.updateUI(activityType);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class MainSettingsFragment extends PreferenceFragmentCompat {
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.settings);

findPreference(getString(R.string.settings_profile_key)).setOnPreferenceClickListener(preference -> {
((SettingsActivity) getActivity()).openScreen(getString(R.string.settings_profile_key));
return true;
});

findPreference(getString(R.string.settings_defaults_key)).setOnPreferenceClickListener(preference -> {
((SettingsActivity) getActivity()).openScreen(getString(R.string.settings_defaults_key));
return true;
Expand Down Expand Up @@ -67,4 +72,4 @@ public void onDisplayPreferenceDialog(@NonNull Preference preference) {

super.onDisplayPreferenceDialog(preference);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ public static void initPreferences(final Context context, final Resources resour
PreferencesOpenHelper.newInstance(PREFERENCES_VERSION).check();
}

public static void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener changeListener) {
public static void registerOnSharedPreferenceChangeListener(
SharedPreferences.OnSharedPreferenceChangeListener changeListener) {
sharedPreferences.registerOnSharedPreferenceChangeListener(changeListener);
changeListener.onSharedPreferenceChanged(sharedPreferences, null);
}

public static void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener changeListener) {
public static void unregisterOnSharedPreferenceChangeListener(
SharedPreferences.OnSharedPreferenceChangeListener changeListener) {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(changeListener);
}

Expand All @@ -99,6 +101,14 @@ public static void setDefaultActivityLocalized(String newDefaultActivity) {
setString(R.string.default_activity_key, newDefaultActivity);
}

public static String getSkiSeasonStartDate() {
return getString(R.string.ski_season_start_key, "09-01");
}

public static void setSkiSeasonStartDate(String newStartDate) {
setString(R.string.ski_season_start_key, newStartDate);
}

/**
* Gets a preference key
*
Expand Down Expand Up @@ -260,6 +270,10 @@ public static SensorType getSensorType(String address) {
.orElse(SensorType.REMOTE);
}

public static String getNickName(){
return getString(R.string.settings_profile_nickname_key, null);
}

public static String getBarometerSensorAddress() {
return getString(R.string.settings_sensor_bluetooth_pressure_key, getBluetoothSensorAddressNone());
}
Expand Down Expand Up @@ -301,7 +315,8 @@ public static HeartRateZones getHeartRateZones() {
}

public static boolean shouldShowStatsOnLockscreen() {
final boolean STATS_SHOW_ON_LOCKSCREEN_DEFAULT = resources.getBoolean(R.bool.stats_show_on_lockscreen_while_recording_default);
final boolean STATS_SHOW_ON_LOCKSCREEN_DEFAULT = resources
.getBoolean(R.bool.stats_show_on_lockscreen_while_recording_default);
return getBoolean(R.string.stats_show_on_lockscreen_while_recording_key, STATS_SHOW_ON_LOCKSCREEN_DEFAULT);
}

Expand Down Expand Up @@ -855,4 +870,12 @@ public static void resetTotalRowsDeleted() {
setInt(R.string.total_rows_deleted_key, 0);
}

}
public static String getSelectedCountry() {
return getString(R.string.settings_profile_country_key, "");
}

public static void setSelectedCountry(final String selectedCountry) {
setString(R.string.settings_profile_country_key, selectedCountry);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package de.dennisguse.opentracks.settings;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputFilter;


import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceFragmentCompat;

import de.dennisguse.opentracks.R;

public class ProfileSettingsFragment extends PreferenceFragmentCompat {

private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = (sharedPreferences, key) -> {
if (PreferencesUtils.isKey(R.string.night_mode_key, key)) {
getActivity().runOnUiThread(PreferencesUtils::applyNightMode);
}
};

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.settings_profile);
ListPreference countryPreference = findPreference(getString(R.string.settings_profile_country_key));


String selectedCountryValue = PreferencesUtils.getSelectedCountry();
countryPreference.setSummary(selectedCountryValue);

countryPreference.setOnPreferenceChangeListener((preference, newValue) -> {
// Save the selected country to SharedPreferences
PreferencesUtils.setSelectedCountry((String) newValue);

// Update summary with selected country
preference.setSummary((String) newValue);
return true;
});
}

@Override
public void onStart() {
super.onStart();
((SettingsActivity) getActivity()).getSupportActionBar().setTitle(R.string.settings_profile_title);
}

@Override
public void onResume() {
super.onResume();

EditTextPreference nickNameInput = findPreference(getString(R.string.settings_profile_nickname_key));
nickNameInput.setDialogTitle(getString(R.string.settings_profile_nickname_dialog_title));
nickNameInput.setOnBindEditTextListener(editText -> {
editText.setSingleLine(true);
editText.selectAll(); // select all text
int maxNicknameLength = 20;
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxNicknameLength)});
});

ListPreference countryPreference = findPreference(getString(R.string.settings_profile_country_key));
String selectedCountryValue = PreferencesUtils.getSelectedCountry();
if (selectedCountryValue != null && !selectedCountryValue.isEmpty()) {
// Update summary with saved selected country
countryPreference.setSummary(selectedCountryValue);
}

PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
}

@Override
public void onPause() {
super.onPause();
PreferencesUtils.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ private PreferenceFragmentCompat getPreferenceScreen(String key) {

if (key.equals(getString(R.string.settings_defaults_key))) {
fragment = new DefaultsSettingsFragment();
} else if (key.equals(getString(R.string.settings_profile_key))) {
fragment = new ProfileSettingsFragment();
} else if (key.equals(getString(R.string.settings_ui_key))) {
fragment = new UserInterfaceSettingsFragment();
} else if (key.equals(getString(R.string.settings_gps_key))) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/res/drawable/ic_profile.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,5c1.66,0 3,1.34 3,3s-1.34,3 -3,3 -3,-1.34 -3,-3 1.34,-3 3,-3zM12,19.2c-2.5,0 -4.71,-1.28 -6,-3.22 0.03,-1.99 4,-3.08 6,-3.08 1.99,0 5.97,1.09 6,3.08 -1.29,1.94 -3.5,3.22 -6,3.22z"/>
</vector>
20 changes: 20 additions & 0 deletions src/main/res/layout/custom_date_picker_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="1dp"
android:gravity="center">

<NumberPicker
android:id="@+id/monthPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"/>

<NumberPicker
android:id="@+id/dayPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"/>

</LinearLayout>
Loading

0 comments on commit 1f228c9

Please sign in to comment.