Skip to content

Commit

Permalink
Use expandable groups in all list views (#487)
Browse files Browse the repository at this point in the history
* Migrate from ListView to ExpandableListView

* Group alert log by year

* Group test alarms in overview

* Group test alarm contexts by activate state

* Group alert log by year

* Group test alarm contexts by activate state

* Group shifts by year month
  • Loading branch information
frimtec authored Dec 17, 2023
1 parent e4f1322 commit c41f916
Show file tree
Hide file tree
Showing 27 changed files with 877 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.time.Duration;
import java.time.LocalTime;
import java.time.YearMonth;
import java.util.Set;

public interface ApplicationPreferences {
Expand Down Expand Up @@ -112,4 +113,20 @@ static ApplicationPreferences instance() {

boolean isDayProfile(Context context, LocalTime currentTime);

Set<Integer> getExpandedAlertLogGroups(Context context);

void setExpandedAlertLogGroups(Context context, Set<Integer> values);

Set<YearMonth> getExpandedShiftGroups(Context context);

void setExpandedShiftGroups(Context context, Set<YearMonth> values);

Set<Boolean> getExpandedTestAlertGroups(Context context);

void setExpandedTestAlertGroups(Context context, Set<Boolean> values);

boolean isTestAlarmStatesExpanded(Context context);

void setTestAlarmStatesExpanded(Context context, boolean values);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.time.Duration;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Set;
Expand Down Expand Up @@ -47,6 +48,10 @@ final class SharedPreferencesApplicationPreferences implements ApplicationPrefer
private static final String PREF_KEY_DAY_START_TIME = "day_start_time";
private static final String PREF_KEY_NIGHT_START_TIME = "night_start_time";
private static final String PREF_APP_THEME = "app_theme";
private static final String PREF_KEY_ALERT_LOG_EXPANDED_GROUPS = "alert_log_expanded_groups";
private static final String PREF_KEY_SHIFT_EXPANDED_GROUPS = "shift_expanded_groups";
private static final String PREF_KEY_TEST_ALERT_EXPANDED_GROUPS = "test_alert_expanded_groups";
private static final String PREF_KEY_TEST_ALARM_EXPANDED_GROUP = "test_alarm_expanded_group";

public static final SharedPreferencesApplicationPreferences INSTANCE = new SharedPreferencesApplicationPreferences();

Expand Down Expand Up @@ -250,6 +255,62 @@ public boolean isDayProfile(Context context, LocalTime currentTime) {
return currentTime.isAfter(getDayProfileStartTime(context)) && currentTime.isBefore(getNightProfileStartTime(context));
}

@Override
public Set<Integer> getExpandedAlertLogGroups(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getStringSet(PREF_KEY_ALERT_LOG_EXPANDED_GROUPS, Collections.emptySet()).stream()
.map(Integer::parseInt)
.collect(Collectors.toSet());
}

@Override
public void setExpandedAlertLogGroups(Context context, Set<Integer> values) {
setSharedPreferences(context, setter -> setter.putStringSet(PREF_KEY_ALERT_LOG_EXPANDED_GROUPS, values.stream()
.map(String::valueOf)
.collect(Collectors.toSet())));
}

@Override
public Set<YearMonth> getExpandedShiftGroups(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getStringSet(PREF_KEY_SHIFT_EXPANDED_GROUPS, Collections.emptySet()).stream()
.map(YearMonth::parse)
.collect(Collectors.toSet());
}

@Override
public void setExpandedShiftGroups(Context context, Set<YearMonth> values) {
setSharedPreferences(context, setter -> setter.putStringSet(PREF_KEY_SHIFT_EXPANDED_GROUPS, values.stream()
.map(String::valueOf)
.collect(Collectors.toSet())));
}

@Override
public Set<Boolean> getExpandedTestAlertGroups(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getStringSet(PREF_KEY_TEST_ALERT_EXPANDED_GROUPS, Set.of(String.valueOf(true), String.valueOf(false))).stream()
.map(Boolean::parseBoolean)
.collect(Collectors.toSet());
}

@Override
public void setExpandedTestAlertGroups(Context context, Set<Boolean> values) {
setSharedPreferences(context, setter -> setter.putStringSet(PREF_KEY_TEST_ALERT_EXPANDED_GROUPS, values.stream()
.map(String::valueOf)
.collect(Collectors.toSet())));
}

@Override
public boolean isTestAlarmStatesExpanded(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(PREF_KEY_TEST_ALARM_EXPANDED_GROUP, false);
}

@Override
public void setTestAlarmStatesExpanded(Context context, boolean values) {
setSharedPreferences(context, setter -> setter.putBoolean(PREF_KEY_TEST_ALARM_EXPANDED_GROUP, values));
}

private static int getOnCallNightVolume(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getInt(PREF_KEY_ON_CALL_NIGHT_VOLUME, R.integer.default_volume_night);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private void registerOnSmsAdapter() {
private void refresh() {
refreshTabLabels();
FragmentManager fm = getSupportFragmentManager();
var activeFragment = (AbstractListFragment<?>) fm.findFragmentByTag("f" + viewPager.getCurrentItem());
var activeFragment = (AbstractListFragment) fm.findFragmentByTag("f" + viewPager.getCurrentItem());
if (activeFragment != null) {
activeFragment.refresh();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.github.frimtec.android.pikettassist.ui.alerts;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.frimtec.android.pikettassist.R;
import com.github.frimtec.android.pikettassist.domain.Alert;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

class AlertExpandableListAdapter extends BaseExpandableListAdapter {

private final Context context;
private final List<YearGroup> years;

AlertExpandableListAdapter(Context context, List<Alert> alerts) {
this.context = context;
Map<Integer, List<Alert>> groupedAlerts = alerts.stream()
.collect(Collectors.groupingBy(
alert -> LocalDateTime.ofInstant(alert.startTime(), ZoneId.systemDefault()).getYear())
);
this.years = groupedAlerts.keySet()
.stream()
.sorted(Comparator.reverseOrder())
.map(year -> new YearGroup(year, groupedAlerts.get(year)))
.collect(Collectors.toList());
}

@Override
public int getGroupCount() {
return this.years.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return this.years.get(groupPosition).alerts().size();
}

@Override
public Object getGroup(int groupPosition) {
return this.years.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return this.years.get(groupPosition).alerts().get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition * 1_000_000L + childPosition;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
YearGroup yearGroup = this.years.get(groupPosition);
if (convertView == null) {
convertView = LayoutInflater.from(this.context).inflate(R.layout.general_list_group_item, parent, false);
}
TextView title = convertView.findViewById(R.id.general_list_group_item_title);
title.setText(String.format(Locale.getDefault(), "%d (%d)", yearGroup.year(), yearGroup.alerts().size()));
return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Alert alert = this.years.get(groupPosition).alerts().get(childPosition);
Objects.requireNonNull(alert);
if (convertView == null) {
convertView = LayoutInflater.from(this.context).inflate(R.layout.alert_log_item, parent, false);
}
ImageView playIcon = convertView.findViewById(R.id.alert_log_item_image_play);
playIcon.setVisibility(alert.isClosed() ? View.INVISIBLE : View.VISIBLE);
TextView timeWindow = convertView.findViewById(R.id.alert_log_item_time_window);
TextView durations = convertView.findViewById(R.id.alert_log_item_durations);
timeWindow.setText(AlertViewHelper.getTimeWindow(alert));
durations.setText(AlertViewHelper.getDurations(this.context, alert));
return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Loading

0 comments on commit c41f916

Please sign in to comment.