Move formatting of date periods to a shared file

This commit is contained in:
Simon Brand 2025-01-02 05:40:10 +00:00
parent e134a3bfbb
commit 08fec63892
5 changed files with 15 additions and 23 deletions

View File

@ -33,6 +33,7 @@ import java.util.Locale;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
public class RespiratoryRatePeriodFragment extends RespiratoryRateFragment<RespiratoryRatePeriodFragment.RespiratoryRateData> {
protected static final Logger LOG = LoggerFactory.getLogger(BodyEnergyFragment.class);
@ -121,10 +122,8 @@ public class RespiratoryRatePeriodFragment extends RespiratoryRateFragment<Respi
protected RespiratoryRateData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) {
Calendar day = Calendar.getInstance();
Date to = new Date((long) this.getTSEnd() * 1000);
Date from = DateUtils.addDays(to,-(TOTAL_DAYS - 1));
String toFormattedDate = new SimpleDateFormat("E, MMM dd").format(to);
String fromFormattedDate = new SimpleDateFormat("E, MMM dd").format(from);
mDateView.setText(fromFormattedDate + " - " + toFormattedDate);
mDateView.setText(DateTimeUtils.formatDaysUntil(TOTAL_DAYS, getTSEnd()));
day.setTime(to);
List<RespiratoryRateDay> respiratoryRateDaysData = getMyRespiratoryRateDaysData(db, day, device);
return new RespiratoryRateData(respiratoryRateDaysData);

View File

@ -19,7 +19,6 @@ import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -36,6 +35,7 @@ import nodomain.freeyourgadget.gadgetbridge.activities.workouts.WorkoutValueForm
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.StepsData> {
protected static final Logger LOG = LoggerFactory.getLogger(BodyEnergyFragment.class);
@ -164,12 +164,7 @@ public class StepsPeriodFragment extends StepsFragment<StepsPeriodFragment.Steps
@Override
protected void updateChartsnUIThread(StepsData stepsData) {
Date to = new Date((long) getTSEnd() * 1000);
Date from = DateUtils.addDays(to, -(TOTAL_DAYS - 1));
String toFormattedDate = new SimpleDateFormat("E, MMM dd").format(to);
String fromFormattedDate = new SimpleDateFormat("E, MMM dd").format(from);
mDateView.setText(fromFormattedDate + " - " + toFormattedDate);
mDateView.setText(DateTimeUtils.formatDaysUntil(TOTAL_DAYS, getTSEnd()));
stepsChart.setData(null);
List<BarEntry> entries = new ArrayList<>();

View File

@ -37,7 +37,6 @@ import com.github.mikephil.charting.formatter.ValueFormatter;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.time.DateUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -221,11 +220,7 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
awakeSleepTimeTextWrapper.setVisibility(View.GONE);
}
Date to = new Date((long) this.getTSEnd() * 1000);
Date from = DateUtils.addDays(to,-(TOTAL_DAYS - 1));
String toFormattedDate = new SimpleDateFormat("E, MMM dd").format(to);
String fromFormattedDate = new SimpleDateFormat("E, MMM dd").format(from);
sleepDatesText.setText(fromFormattedDate + " - " + toFormattedDate);
sleepDatesText.setText(DateTimeUtils.formatDaysUntil(TOTAL_DAYS, getTSEnd()));
mBalanceView.setText(mcd.getWeekBeforeData().getBalanceMessage());
}

View File

@ -126,14 +126,9 @@ public class WeightChartFragment extends AbstractChartFragment<WeightChartFragme
chart.getXAxis().setValueFormatter(chartsData.getXValueFormatter());
chart.getXAxis().setAvoidFirstLastClipping(true);
chart.setData(chartsData.getData());
textTimeSpan.setText(DateTimeUtils.formatDaysUntil(totalDays, getTSEnd()));
Date dateStart = DateTimeUtils.parseTimeStamp(getTSStart());
Date dateEnd = DateTimeUtils.parseTimeStamp(getTSEnd());
SimpleDateFormat format = new SimpleDateFormat("E, MMM dd");
WeightSample latestSample = chartsData.getLatestSample();
textTimeSpan.setText(format.format(dateStart) + " - " + format.format(dateEnd));
if (latestSample != null)
textWeightLatest.setText(formatWeight(weightFromKg(latestSample.getWeightKg())));

View File

@ -319,4 +319,12 @@ public class DateTimeUtils {
return context.getString(R.string.unknown);
}
public static String formatDaysUntil(int days, int endTs) {
Date to = new Date((long) endTs * 1000);
Date from = org.apache.commons.lang3.time.DateUtils.addDays(to, - (days - 1));
String toFormattedDate = new SimpleDateFormat("E, MMM dd").format(to);
String fromFormattedDate = new SimpleDateFormat("E, MMM dd").format(from);
return fromFormattedDate + " - " + toFormattedDate;
}
}