Skip to content

Commit

Permalink
Hotfix/#882 fix dashboard reorganisation (#1023)
Browse files Browse the repository at this point in the history
* Remove broadcast card from reorderable list

* Fix some tests

* Create migration

* Fix migration with removed item

* Remove broadcastCard from shared prefs

* Remove broadcastChange from shared prefs

* Fix tests

* Last changes before review

* [BOT] Applying version.

* Fix TODO version

* [BOT] Applying format.

* Fix analyze

* [BOT] Update golden files

---------

Co-authored-by: XavierPaquet-Rapold <[email protected]>
Co-authored-by: clubapplets-server <[email protected]>
  • Loading branch information
3 people authored Jul 21, 2024
1 parent b2bd88b commit f7ffba7
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 176 deletions.
2 changes: 0 additions & 2 deletions lib/constants/preferences_flags.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ enum PreferencesFlag {
discoveryMore,

// Dashboard flags
broadcastCard,
aboutUsCard,
scheduleCard,
progressBarCard,
gradesCard,
progressBarText,
broadcastChange,

// Rating flag
ratingTimer,
Expand Down
75 changes: 39 additions & 36 deletions lib/features/dashboard/dashboard_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class _DashboardViewState extends State<DashboardView>
data: Theme.of(context)
.copyWith(canvasColor: Colors.transparent),
child: ReorderableListView(
header:
model.remoteConfigService.dashboardMessageActive
? _buildMessageBroadcastCard(model)
: null,
onReorder: (oldIndex, newIndex) =>
onReorder(model, oldIndex, newIndex),
padding: const EdgeInsets.fromLTRB(0, 4, 0, 24),
Expand All @@ -96,16 +100,11 @@ class _DashboardViewState extends State<DashboardView>

List<Widget> _buildCards(DashboardViewModel model) {
final List<Widget> cards = List.empty(growable: true);

// always try to build broadcast cart so the user doesn't miss out on
// important info if they dismissed it previously

for (final PreferencesFlag element in model.cardsToDisplay ?? []) {
switch (element) {
case PreferencesFlag.broadcastCard:
if (model.remoteConfigService.dashboardMessageActive) {
cards.add(_buildMessageBroadcastCard(model, element));
}
case PreferencesFlag.aboutUsCard:
cards.add(_buildAboutUsCard(model, element));
case PreferencesFlag.scheduleCard:
Expand All @@ -114,7 +113,6 @@ class _DashboardViewState extends State<DashboardView>
cards.add(_buildProgressBarCard(model, element));
case PreferencesFlag.gradesCard:
cards.add(_buildGradesCards(model, element));

default:
}

Expand Down Expand Up @@ -459,43 +457,48 @@ class _DashboardViewState extends State<DashboardView>
);
}

Widget _buildMessageBroadcastCard(
DashboardViewModel model, PreferencesFlag flag) {
Widget _buildMessageBroadcastCard(DashboardViewModel model) {
if (model.broadcastMessage == "" ||
model.broadcastColor == "" ||
model.broadcastTitle == "") {
return const SizedBox.shrink();
}
final broadcastMsgColor = Color(int.parse(model.broadcastColor));
final broadcastMsgType = model.broadcastType;
final broadcastMsgUrl = model.broadcastUrl;
return DismissibleCard(
return Card(
key: UniqueKey(),
onDismissed: (DismissDirection direction) {
dismissCard(model, flag);
},
isBusy: model.busy(model.broadcastMessage),
cardColor: broadcastMsgColor,
color: broadcastMsgColor,
child: Padding(
padding: const EdgeInsets.fromLTRB(17, 10, 15, 20),
child: Column(mainAxisSize: MainAxisSize.min, children: [
// title row
Row(
children: [
Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: Text(model.broadcastTitle,
style: Theme.of(context).primaryTextTheme.titleLarge),
),
),
Align(
alignment: Alignment.centerRight,
child: InkWell(
child: getBroadcastIcon(broadcastMsgType, broadcastMsgUrl),
child: model.busy(model.broadcastMessage)
? const Center(child: CircularProgressIndicator())
: Column(mainAxisSize: MainAxisSize.min, children: [
// title row
Row(
children: [
Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: Text(model.broadcastTitle,
style: Theme.of(context)
.primaryTextTheme
.titleLarge),
),
),
Align(
alignment: Alignment.centerRight,
child: InkWell(
child: getBroadcastIcon(
broadcastMsgType, broadcastMsgUrl),
),
),
],
),
),
],
),
// main text
AutoSizeText(model.broadcastMessage,
style: Theme.of(context).primaryTextTheme.bodyMedium)
]),
// main text
AutoSizeText(model.broadcastMessage,
style: Theme.of(context).primaryTextTheme.bodyMedium)
]),
));
}

Expand Down
47 changes: 24 additions & 23 deletions lib/features/dashboard/dashboard_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
import 'package:feature_discovery/feature_discovery.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:stacked/stacked.dart';

// Project imports:
Expand Down Expand Up @@ -36,7 +37,6 @@ class DashboardViewModel extends FutureViewModel<Map<PreferencesFlag, int>> {
static const String tag = "DashboardViewModel";

final SettingsManager _settingsManager = locator<SettingsManager>();
final PreferencesService _preferencesService = locator<PreferencesService>();
final CourseRepository _courseRepository = locator<CourseRepository>();
final AnalyticsService _analyticsService = locator<AnalyticsService>();
final RemoteConfigService remoteConfigService =
Expand Down Expand Up @@ -197,17 +197,35 @@ class DashboardViewModel extends FutureViewModel<Map<PreferencesFlag, int>> {
Future<Map<PreferencesFlag, int>> futureToRun() async {
final dashboard = await _settingsManager.getDashboard();

_cards = dashboard;
//TODO: remove when all users are on 4.50.1 or more
final sharedPreferences = await SharedPreferences.getInstance();
if (sharedPreferences.containsKey("PreferencesFlag.broadcastChange")) {
sharedPreferences.remove("PreferencesFlag.broadcastChange");
}
if (sharedPreferences.containsKey("PreferencesFlag.broadcastCard")) {
sharedPreferences.remove("PreferencesFlag.broadcastCard");
}
final sortedList = dashboard.entries.toList()
..sort((a, b) => a.value.compareTo(b.value));
final sortedDashboard = LinkedHashMap.fromEntries(sortedList);
int index = 0;
for (final element in sortedDashboard.entries) {
if (element.value >= 0) {
sortedDashboard.update(element.key, (value) => index);
index++;
}
}
//TODO: end remove when all users are on 4.50.1 or more

await checkForBroadcastChange();
_cards = sortedDashboard;

getCardsToDisplay();

// load data for both grade cards & grades home screen widget
// (moved from getCardsToDisplay())
await loadDataAndUpdateWidget();

return dashboard;
return sortedDashboard;
}

Future loadDataAndUpdateWidget() async {
Expand Down Expand Up @@ -254,13 +272,13 @@ class DashboardViewModel extends FutureViewModel<Map<PreferencesFlag, int>> {
void setAllCardsVisible() {
_cards?.updateAll((key, value) {
_settingsManager
.setInt(key, key.index - PreferencesFlag.broadcastCard.index)
.setInt(key, key.index - PreferencesFlag.aboutUsCard.index)
.then((value) {
if (!value) {
Fluttertoast.showToast(msg: _appIntl.error);
}
});
return key.index - PreferencesFlag.broadcastCard.index;
return key.index - PreferencesFlag.aboutUsCard.index;
});

getCardsToDisplay();
Expand Down Expand Up @@ -288,23 +306,6 @@ class DashboardViewModel extends FutureViewModel<Map<PreferencesFlag, int>> {
_analyticsService.logEvent(tag, "Restoring cards");
}

Future<void> checkForBroadcastChange() async {
final broadcastChange =
await _preferencesService.getString(PreferencesFlag.broadcastChange) ??
"";
if (broadcastChange != remoteConfigService.dashboardMessageEn) {
// Update pref
_preferencesService.setString(PreferencesFlag.broadcastChange,
remoteConfigService.dashboardMessageEn);
if (_cards != null && _cards![PreferencesFlag.broadcastCard]! < 0) {
_cards?.updateAll((key, value) {
return value >= 0 ? value + 1 : value;
});
_cards![PreferencesFlag.broadcastCard] = 0;
}
}
}

Future<List<Session>> futureToRunSessionProgressBar() async {
try {
final progressBarText =
Expand Down
13 changes: 2 additions & 11 deletions lib/features/more/settings/settings_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,6 @@ class SettingsManager with ChangeNotifier {
/// Get Dashboard
Future<Map<PreferencesFlag, int>> getDashboard() async {
final Map<PreferencesFlag, int> dashboard = {};

final broadcastCardIndex =
await _preferencesService.getInt(PreferencesFlag.broadcastCard) ??
getDefaultCardIndex(PreferencesFlag.broadcastCard);

dashboard.putIfAbsent(
PreferencesFlag.broadcastCard, () => broadcastCardIndex);

final aboutUsIndex =
await _preferencesService.getInt(PreferencesFlag.aboutUsCard) ??
getDefaultCardIndex(PreferencesFlag.aboutUsCard);
Expand Down Expand Up @@ -277,9 +269,8 @@ class SettingsManager with ChangeNotifier {
}

/// Get the default index of each card
int getDefaultCardIndex(PreferencesFlag flag) {
return flag.index - PreferencesFlag.broadcastCard.index;
}
int getDefaultCardIndex(PreferencesFlag flag) =>
flag.index - PreferencesFlag.aboutUsCard.index;

bool get calendarViewSetting => _remoteConfigService.scheduleListViewDefault;
}
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.2.1"
skeletonizer:
dependency: "direct main"
description:
name: skeletonizer
sha256: "9dcaec747d200a299bc457ffe51f135b9856b7f9097e851e277c4c95f563893b"
url: "https://pub.dev"
source: hosted
version: "1.4.1+1"
sky_engine:
dependency: transitive
description: flutter
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: The 4th generation of ÉTSMobile, the main gateway between the Éco
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 4.50.0+1
version: 4.50.1+1

environment:
sdk: '>=3.3.0 <4.0.0'
Expand Down
34 changes: 12 additions & 22 deletions test/managers/settings_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -418,20 +418,17 @@ void main() {

// Cards
final Map<PreferencesFlag, int> expected = {
PreferencesFlag.broadcastCard: 0,
PreferencesFlag.aboutUsCard: 1,
PreferencesFlag.scheduleCard: 2,
PreferencesFlag.progressBarCard: 3,
PreferencesFlag.gradesCard: 4
PreferencesFlag.aboutUsCard: 0,
PreferencesFlag.scheduleCard: 1,
PreferencesFlag.progressBarCard: 2,
PreferencesFlag.gradesCard: 3
};

expect(
await manager.getDashboard(),
expected,
);

verify(preferencesServiceMock.getInt(PreferencesFlag.broadcastCard))
.called(1);
verify(preferencesServiceMock.getInt(PreferencesFlag.aboutUsCard))
.called(1);
verify(preferencesServiceMock.getInt(PreferencesFlag.scheduleCard))
Expand All @@ -447,38 +444,31 @@ void main() {

test("validate the loading of the cards", () async {
PreferencesServiceMock.stubGetInt(
preferencesServiceMock, PreferencesFlag.broadcastCard,
toReturn: 0);
PreferencesServiceMock.stubGetInt(
preferencesServiceMock, PreferencesFlag.aboutUsCard,
toReturn: 2);
preferencesServiceMock, PreferencesFlag.aboutUsCard);
PreferencesServiceMock.stubGetInt(
preferencesServiceMock, PreferencesFlag.scheduleCard,
toReturn: 3);
toReturn: 2);
PreferencesServiceMock.stubGetInt(
preferencesServiceMock, PreferencesFlag.progressBarCard,
// ignore: avoid_redundant_argument_values
toReturn: 1);
toReturn: 0);
PreferencesServiceMock.stubGetInt(
preferencesServiceMock, PreferencesFlag.gradesCard,
toReturn: 4);
toReturn: 3);

// Cards
final Map<PreferencesFlag, int> expected = {
PreferencesFlag.broadcastCard: 0,
PreferencesFlag.aboutUsCard: 2,
PreferencesFlag.scheduleCard: 3,
PreferencesFlag.progressBarCard: 1,
PreferencesFlag.gradesCard: 4
PreferencesFlag.aboutUsCard: 1,
PreferencesFlag.scheduleCard: 2,
PreferencesFlag.progressBarCard: 0,
PreferencesFlag.gradesCard: 3
};

expect(
await manager.getDashboard(),
expected,
);

verify(preferencesServiceMock.getInt(PreferencesFlag.broadcastCard))
.called(1);
verify(preferencesServiceMock.getInt(PreferencesFlag.aboutUsCard))
.called(1);
verify(preferencesServiceMock.getInt(PreferencesFlag.scheduleCard))
Expand Down
Loading

0 comments on commit f7ffba7

Please sign in to comment.