-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboardAdapter.java
More file actions
283 lines (249 loc) · 11.4 KB
/
DashboardAdapter.java
File metadata and controls
283 lines (249 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package com.example.theloop;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.chip.ChipGroup;
public class DashboardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
static final int TYPE_HEADER = 0;
static final int TYPE_WEATHER = 1;
static final int TYPE_HEADLINES = 2;
static final int TYPE_CALENDAR = 3;
static final int TYPE_FUN_FACT = 4;
static final int TYPE_HEALTH = 5;
static final int TYPE_UK_NEWS = 6;
static final int TYPE_FOOTER = 7;
// Callbacks for data binding
public interface Binder {
void bindHeader(HeaderViewHolder holder);
void bindWeather(WeatherViewHolder holder);
void bindHeadlines(HeadlinesViewHolder holder);
void bindUkNews(HeadlinesViewHolder holder);
void bindCalendar(CalendarViewHolder holder);
void bindFunFact(FunFactViewHolder holder);
void bindHealth(HealthViewHolder holder);
void bindFooter(FooterViewHolder holder);
}
private final Binder binder;
private final String[] sectionOrder; // E.g., {"headlines", "calendar", "fun_fact", "health"}
public DashboardAdapter(Binder binder, String[] sectionOrder) {
this.binder = binder;
this.sectionOrder = sectionOrder;
}
@Override
public int getItemViewType(int position) {
if (position == 0) return TYPE_HEADER;
if (position == 1) return TYPE_WEATHER;
if (position == getItemCount() - 1) return TYPE_FOOTER;
// Remaining positions map to sectionOrder
String section = sectionOrder[position - 2];
return switch (section) {
case MainActivity.SECTION_HEADLINES -> TYPE_HEADLINES;
case MainActivity.SECTION_UK_NEWS -> TYPE_UK_NEWS;
case MainActivity.SECTION_CALENDAR -> TYPE_CALENDAR;
case MainActivity.SECTION_FUN_FACT -> TYPE_FUN_FACT;
case MainActivity.SECTION_HEALTH -> TYPE_HEALTH;
default -> -1; // Should not happen
};
}
@Override
public int getItemCount() {
return 2 + sectionOrder.length + 1; // Header + Weather + Sections + Footer
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return switch (viewType) {
case TYPE_HEADER -> new HeaderViewHolder(inflater.inflate(R.layout.card_day_ahead, parent, false));
case TYPE_WEATHER -> new WeatherViewHolder(inflater.inflate(R.layout.card_weather, parent, false));
case TYPE_HEADLINES -> new HeadlinesViewHolder(inflater.inflate(R.layout.card_headlines, parent, false));
case TYPE_UK_NEWS -> new HeadlinesViewHolder(inflater.inflate(R.layout.card_headlines, parent, false));
case TYPE_CALENDAR -> new CalendarViewHolder(inflater.inflate(R.layout.card_calendar, parent, false));
case TYPE_FUN_FACT -> new FunFactViewHolder(inflater.inflate(R.layout.card_fun_fact, parent, false));
case TYPE_HEALTH -> new HealthViewHolder(inflater.inflate(R.layout.card_health, parent, false));
case TYPE_FOOTER -> new FooterViewHolder(inflater.inflate(R.layout.item_settings_footer, parent, false));
default -> throw new IllegalArgumentException("Invalid view type");
};
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HeaderViewHolder headerHolder) {
binder.bindHeader(headerHolder);
} else if (holder instanceof WeatherViewHolder weatherHolder) {
binder.bindWeather(weatherHolder);
} else if (holder instanceof HeadlinesViewHolder headlinesHolder) {
if (holder.getItemViewType() == TYPE_UK_NEWS) {
binder.bindUkNews(headlinesHolder);
} else {
binder.bindHeadlines(headlinesHolder);
}
} else if (holder instanceof CalendarViewHolder calendarHolder) {
binder.bindCalendar(calendarHolder);
} else if (holder instanceof FunFactViewHolder funFactHolder) {
binder.bindFunFact(funFactHolder);
} else if (holder instanceof HealthViewHolder healthHolder) {
binder.bindHealth(healthHolder);
} else if (holder instanceof FooterViewHolder footerHolder) {
binder.bindFooter(footerHolder);
} else {
throw new IllegalStateException("Unhandled ViewHolder type: " + holder.getClass().getName());
}
}
// ViewHolders
static class HeaderViewHolder extends RecyclerView.ViewHolder {
final TextView greeting;
final TextView summary;
final ImageButton playButton;
HeaderViewHolder(View v) {
super(v);
greeting = v.findViewById(R.id.day_ahead_greeting);
summary = v.findViewById(R.id.day_ahead_summary);
playButton = v.findViewById(R.id.day_ahead_play_button);
}
}
static class WeatherViewHolder extends RecyclerView.ViewHolder {
final ProgressBar progressBar;
final TextView errorText;
final LinearLayout contentLayout;
final ImageView icon;
final TextView temp;
final TextView conditions;
final TextView highLow;
final LinearLayout forecastContainer;
final ForecastDayViewHolder[] forecastViews;
final TextView location;
final ImageView settingsIcon;
static class ForecastDayViewHolder {
final View parent;
final TextView day;
final ImageView icon;
final TextView high;
final TextView low;
ForecastDayViewHolder(View v) {
parent = v;
day = v.findViewById(R.id.forecast_day);
icon = v.findViewById(R.id.forecast_icon);
high = v.findViewById(R.id.forecast_high);
low = v.findViewById(R.id.forecast_low);
}
}
WeatherViewHolder(View v) {
super(v);
progressBar = v.findViewById(R.id.weather_progress_bar);
errorText = v.findViewById(R.id.weather_error_text);
contentLayout = v.findViewById(R.id.weather_content_layout);
icon = v.findViewById(R.id.weather_icon);
temp = v.findViewById(R.id.current_temp);
conditions = v.findViewById(R.id.current_conditions);
highLow = v.findViewById(R.id.high_low_temp);
forecastContainer = v.findViewById(R.id.daily_forecast_container);
location = v.findViewById(R.id.weather_location);
settingsIcon = v.findViewById(R.id.weather_settings_icon);
forecastViews = new ForecastDayViewHolder[] {
new ForecastDayViewHolder(v.findViewById(R.id.forecast_day_1)),
new ForecastDayViewHolder(v.findViewById(R.id.forecast_day_2)),
new ForecastDayViewHolder(v.findViewById(R.id.forecast_day_3)),
new ForecastDayViewHolder(v.findViewById(R.id.forecast_day_4)),
new ForecastDayViewHolder(v.findViewById(R.id.forecast_day_5))
};
}
}
static class HeadlinesViewHolder extends RecyclerView.ViewHolder {
final ProgressBar progressBar;
final TextView errorText;
final TextView cardTitle;
final LinearLayout container;
final ChipGroup chipGroup;
final HeadlineItemViewHolder[] headlineViews;
static class HeadlineItemViewHolder {
final View parent;
final TextView title;
final TextView source;
HeadlineItemViewHolder(View v) {
parent = v;
title = v.findViewById(R.id.headline_title);
source = v.findViewById(R.id.headline_source_time);
}
}
HeadlinesViewHolder(View v) {
super(v);
progressBar = v.findViewById(R.id.headlines_progress_bar);
errorText = v.findViewById(R.id.headlines_error_text);
cardTitle = v.findViewById(R.id.headlines_card_title);
container = v.findViewById(R.id.headlines_container);
chipGroup = v.findViewById(R.id.headlines_category_chips);
headlineViews = new HeadlineItemViewHolder[] {
new HeadlineItemViewHolder(v.findViewById(R.id.headline_1)),
new HeadlineItemViewHolder(v.findViewById(R.id.headline_2)),
new HeadlineItemViewHolder(v.findViewById(R.id.headline_3))
};
}
}
static class CalendarViewHolder extends RecyclerView.ViewHolder {
final TextView permissionDeniedText;
final TextView noEventsText;
final TextView errorText;
final LinearLayout eventsContainer;
final CalendarEventItemViewHolder[] eventViews;
static class CalendarEventItemViewHolder {
final View parent;
final TextView title;
final TextView time;
final TextView location;
final TextView owner;
CalendarEventItemViewHolder(View v) {
parent = v;
title = v.findViewById(R.id.event_title);
time = v.findViewById(R.id.event_time);
location = v.findViewById(R.id.event_location);
owner = v.findViewById(R.id.event_owner);
}
}
CalendarViewHolder(View v) {
super(v);
permissionDeniedText = v.findViewById(R.id.calendar_permission_denied_text);
noEventsText = v.findViewById(R.id.calendar_no_events_text);
errorText = v.findViewById(R.id.calendar_error_text);
eventsContainer = v.findViewById(R.id.calendar_events_container);
eventViews = new CalendarEventItemViewHolder[] {
new CalendarEventItemViewHolder(v.findViewById(R.id.calendar_event_1)),
new CalendarEventItemViewHolder(v.findViewById(R.id.calendar_event_2)),
new CalendarEventItemViewHolder(v.findViewById(R.id.calendar_event_3))
};
}
}
static class FunFactViewHolder extends RecyclerView.ViewHolder {
final TextView funFactText;
FunFactViewHolder(View v) {
super(v);
funFactText = v.findViewById(R.id.fun_fact_text);
}
}
static class HealthViewHolder extends RecyclerView.ViewHolder {
final TextView stepsCount;
final TextView permissionButton;
final TextView errorText;
final LinearLayout contentLayout;
HealthViewHolder(View v) {
super(v);
stepsCount = v.findViewById(R.id.health_steps_count);
permissionButton = v.findViewById(R.id.health_permission_button);
errorText = v.findViewById(R.id.health_error_text);
contentLayout = v.findViewById(R.id.health_content_layout);
}
}
static class FooterViewHolder extends RecyclerView.ViewHolder {
final TextView settingsLink;
FooterViewHolder(View v) {
super(v);
settingsLink = v.findViewById(R.id.settings_link);
}
}
}