Skip to content

Commit

Permalink
fix bug where user data would not get deleted from device after logout
Browse files Browse the repository at this point in the history
  • Loading branch information
NeXTormer committed Mar 7, 2024
1 parent 6a26f7d commit d25cb76
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 50 deletions.
1 change: 1 addition & 0 deletions lib/backend/authentication/frederic_auth_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class FredericSignOutEvent extends FredericAuthEvent {

@override
Future<FredericUser> process(FredericUserManager userManager) async {
FredericBackend.instance.deleteEverythingFromDisk();
FredericBackend.instance.dispose();
await userManager.authInterface.logOut();
return FredericUser.noAuth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ class FirestoreCachingDataInterface<T extends FredericDataObject>

@override
Future<List<T>> reload() async {
if (name == "Goals") {
print("G");
}
if (_box == null) {
_box = await Hive.openBox(name);
} else {
Expand Down Expand Up @@ -109,4 +106,9 @@ class FirestoreCachingDataInterface<T extends FredericDataObject>
_box!.putAll(entries);
return data;
}

@override
Future<void> deleteFromDisk() {
return Hive.deleteBoxFromDisk(name);
}
}
6 changes: 6 additions & 0 deletions lib/backend/database/frederic_data_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ abstract class FredericDataInterface<T extends FredericDataObject> {
/// Reloads data from remote DB
///
Future<List<T>> reload();

Future<T> update(T object);

Future<void> delete(T object);

Future<T> create(T object);

Future<T> createFromMap(Map<String, dynamic> data);

Future<void> deleteFromDisk();
}
7 changes: 7 additions & 0 deletions lib/backend/frederic_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ class FredericBackend implements FredericMessageProcessor {
void dispose() {
_purchaseManager.dispose();
}

Future<void> deleteEverythingFromDisk() async {
await activityManager.dataInterface.deleteFromDisk();
await workoutManager.dataInterface.deleteFromDisk();
await setManager.dataInterface.deleteFromDisk();
await goalManager.dataInterface.deleteFromDisk();
}
}

class FredericDefaults {
Expand Down
15 changes: 7 additions & 8 deletions lib/backend/goals/frederic_goal_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class FredericGoalManager
on<FredericGoalEvent>(_onEvent);
}

late final FredericDataInterface<FredericGoal> _dataInterface;
late final FredericDataInterface<FredericGoal> dataInterface;

void setDataInterface(FredericDataInterface<FredericGoal> interface) =>
_dataInterface = interface;
dataInterface = interface;

HashMap<String, FredericGoal> _goals = HashMap<String, FredericGoal>();

Expand All @@ -30,9 +30,8 @@ class FredericGoalManager
Future<void> reload([bool fullReloadFromDB = true]) async {
List<String> changed = <String>[];
_goals.clear();
List<FredericGoal> list = await (fullReloadFromDB
? _dataInterface.reload()
: _dataInterface.get());
List<FredericGoal> list =
await (fullReloadFromDB ? dataInterface.reload() : dataInterface.get());
for (FredericGoal goal in list) {
_goals[goal.id] = goal;
changed.add(goal.id);
Expand All @@ -52,16 +51,16 @@ class FredericGoalManager
if (_goals.containsKey(event.updatedGoal.id)) {
_goals[event.updatedGoal.id] = event.updatedGoal;
}
await _dataInterface.update(event.updatedGoal);
await dataInterface.update(event.updatedGoal);
emit(FredericGoalListData(event.changed, _goals));
} else if (event is FredericGoalCreateEvent) {
final newGoalFromDatabase = await _dataInterface.create(event.newGoal);
final newGoalFromDatabase = await dataInterface.create(event.newGoal);
_goals[event.newGoal.id] = newGoalFromDatabase;
emit(FredericGoalListData(event.changed, _goals));
FredericBackend.instance.analytics.logGoalCreated();
} else if (event is FredericGoalDeleteEvent) {
_goals.remove(event.goal.id);
await _dataInterface.delete(event.goal);
await dataInterface.delete(event.goal);
emit(FredericGoalListData(event.changed, _goals));
FredericBackend.instance.analytics.logGoalDeleted();
} else {
Expand Down
5 changes: 0 additions & 5 deletions lib/backend/sets/frederic_set_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ class FredericSetManager extends Bloc<FredericSetEvent, FredericSetListData> {

bool _canFullReload = true;

FredericSetList operator [](String value) {
if (!_sets.containsKey(value)) _sets[value] = FredericSetList.create(value);
return _sets[value]!;
}

void setDataInterface(FredericDataInterface<FredericSetDocument> interface) =>
dataInterface = interface;

Expand Down
39 changes: 7 additions & 32 deletions lib/screens/activity_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,13 @@ class ActivityListScreen extends StatelessWidget {
child: FredericScaffold(
floatingActionButton: FloatingActionButton(
key: ValueKey<String>('fab_activitylistscreen'),
heroTag: 'fab_activitylistscreen',
onPressed: () {
// if (CupertinoScaffold.of(context) == null) {
if (false) {
showModalBottomSheet(
enableDrag: true,
isScrollControlled: true,
isDismissible: true,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
context: context,
builder: (newContext) {
return ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
height: MediaQuery.of(context).size.height * 0.93,
child: EditActivityScreen(FredericActivity.create(
FredericBackend.instance.userManager.state.id)),
),
);
});
} else {
showFredericBottomSheet(
enableDrag: true,
context: context,
builder: (newContext) {
return EditActivityScreen(FredericActivity.create(
FredericBackend.instance.userManager.state.id));
});
}
},
onPressed: () => showFredericBottomSheet(
enableDrag: true,
context: context,
builder: (newContext) {
return EditActivityScreen(FredericActivity.create(
FredericBackend.instance.userManager.state.id));
}),
child: Icon(
Icons.post_add_outlined,
color: Colors.white,
Expand Down
4 changes: 3 additions & 1 deletion lib/screens/edit_workout_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class _EditWorkoutScreenState extends State<EditWorkoutScreen> {
return FredericScaffold(
floatingActionButton: workout.canEdit
? FloatingActionButton(
key: UniqueKey(),
// This seems to have fixed itself without a key and hero tag
// key: UniqueKey(),
// heroTag: UniqueKey(),
// because the FAB disappears after resulting sheet is closed,
// the unique key combined with the setState after the sheet is closed makes it reappear
backgroundColor: theme.mainColor,
Expand Down
3 changes: 2 additions & 1 deletion lib/widgets/settings_screen/account_deleter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ class _AccountDeleterState extends State<AccountDeleter> {
await FredericBackend.instance.userManager.authInterface
.deleteAccount(user);
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.clear();
await prefs.clear();
await FredericBackend.instance.deleteEverythingFromDisk();
FredericBase.forceFullRestart(context);
},
mainColor: confirmed ? theme.negativeColor : theme.greyColor,
Expand Down
7 changes: 7 additions & 0 deletions test/mock_database/test_activity_data_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class TestActivityDataInterface
HashMap<String, FredericActivity> database =
HashMap<String, FredericActivity>();
int index = 0;

void initData() {
addActivity('Werner', FredericActivityType.Weighted,
[FredericActivityMuscleGroup.Arms]);
Expand Down Expand Up @@ -68,4 +69,10 @@ class TestActivityDataInterface
// TODO: implement reload
throw UnimplementedError();
}

@override
Future<void> deleteFromDisk() {
// TODO: implement deleteFromDisk
throw UnimplementedError();
}
}

0 comments on commit d25cb76

Please sign in to comment.