Skip to content

Commit

Permalink
Fix migration functions and add staging
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulPickhardt committed Nov 29, 2023
1 parent 4d948c6 commit 72fc3b8
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 73 deletions.
46 changes: 26 additions & 20 deletions lib/home/services/shortcuts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,35 @@ class Shortcuts with ChangeNotifier {
await storeShortcuts();
} else {
// Init shortcuts.
shortcuts = [];
// Loop through all json Shortcuts and add correct shortcuts to shortcuts.
for (final e in jsonDecode(jsonStr) as List) {
if (e["type"] != null) {
switch (e["type"]) {
case "ShortcutLocation":
shortcuts?.add(ShortcutLocation.fromJson(e));
break;
case "ShortcutRoute":
shortcuts?.add(ShortcutRoute.fromJson(e));
break;
default:
final hint = "Error unknown type ${e["type"]} in loadShortcuts.";
log.e(hint);
}
} else {
// Only for backwards compatibility.
if (e["waypoint"] != null) shortcuts?.add(ShortcutLocation.fromJson(e));
if (e["waypoints"] != null) shortcuts?.add(ShortcutRoute.fromJson(e));
shortcuts = getShortcutsFromJson(jsonStr);
}
notifyListeners();
}

/// Creates a list of shortcut objects from a json string.
List<Shortcut> getShortcutsFromJson(String jsonStr) {
List<Shortcut> shortcuts = [];
// Loop through all json Shortcuts and add correct shortcuts to shortcuts.
for (final e in jsonDecode(jsonStr) as List) {
if (e["type"] != null) {
switch (e["type"]) {
case "ShortcutLocation":
shortcuts.add(ShortcutLocation.fromJson(e));
break;
case "ShortcutRoute":
shortcuts.add(ShortcutRoute.fromJson(e));
break;
default:
final hint = "Error unknown type ${e["type"]} in loadShortcuts.";
log.e(hint);
}
} else {
// Only for backwards compatibility.
if (e["waypoint"] != null) shortcuts.add(ShortcutLocation.fromJson(e));
if (e["waypoints"] != null) shortcuts.add(ShortcutRoute.fromJson(e));
}
}
notifyListeners();
return shortcuts;
}

/// Delete a shortcut.
Expand Down
131 changes: 78 additions & 53 deletions lib/migration/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import 'dart:convert';

import 'package:flutter/material.dart' hide Shortcuts;
import 'package:priobike/home/models/shortcut.dart';
import 'package:priobike/home/models/shortcut_location.dart';
import 'package:priobike/home/models/shortcut_route.dart';
import 'package:priobike/home/services/shortcuts.dart';
import 'package:priobike/main.dart';
import 'package:priobike/settings/models/backend.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand All @@ -12,81 +11,86 @@ class Migration with ChangeNotifier {
/// Load the privacy policy.
Future<void> migrate() async {
// List of things to migrate.
await migrateShortcuts();
await migrateSearchHistory();
await migrateShortcutsProduction();
await migrateShortcutsStaging();
await migrateSearchHistoryProduction();
await migrateSearchHistoryStaging();
}

/// Migrate all shortcuts (Hamburg beta => Hamburg).
Future<void> migrateShortcuts() async {
/// Migrate all shortcuts (production/release => Hamburg).
Future<void> migrateShortcutsProduction() async {
final storage = await SharedPreferences.getInstance();

Shortcuts shortcuts = getIt<Shortcuts>();

// Get the current shortcuts of the currently used backend.
final jsonStrProduction = storage.getString("priobike.home.shortcuts.${Backend.production.name}");
// Return on no old key found.
if (jsonStrProduction == null) return;
final jsonStrRelease = storage.getString("priobike.home.shortcuts.${Backend.release.name}");
// Return on no old key found.
if (jsonStrProduction == null && jsonStrRelease == null) return;
final jsonStrReleaseNew = storage.getString("priobike.home.shortcuts.${Backend.release.regionName}");

List<Shortcut> shortcutsRelease = [];
List<Shortcut> shortcutsProduction = [];
List<Shortcut> shortcutsReleaseNew = [];

if (jsonStrRelease != null) {
// Loop through all json Shortcuts and add correct shortcuts to shortcutsRelease.
for (final e in jsonDecode(jsonStrRelease) as List) {
if (e["type"] != null) {
switch (e["type"]) {
case "ShortcutLocation":
shortcutsRelease.add(ShortcutLocation.fromJson(e));
break;
case "ShortcutRoute":
shortcutsRelease.add(ShortcutRoute.fromJson(e));
break;
default:
final hint = "Error unknown type ${e["type"]} in loadShortcuts.";
log.e(hint);
}
} else {
// Only for backwards compatibility.
if (e["waypoint"] != null) shortcutsRelease.add(ShortcutLocation.fromJson(e));
if (e["waypoints"] != null) shortcutsRelease.add(ShortcutRoute.fromJson(e));
}
}
shortcutsRelease = shortcuts.getShortcutsFromJson(jsonStrRelease);
}

if (jsonStrProduction != null) {
shortcutsProduction = shortcuts.getShortcutsFromJson(jsonStrProduction);
}

// Init shortcuts.
// Loop through all json Shortcuts and add correct shortcuts to shortcutsProduction.
for (final e in jsonDecode(jsonStrProduction) as List) {
if (e["type"] != null) {
switch (e["type"]) {
case "ShortcutLocation":
shortcutsProduction.add(ShortcutLocation.fromJson(e));
break;
case "ShortcutRoute":
shortcutsProduction.add(ShortcutRoute.fromJson(e));
break;
default:
final hint = "Error unknown type ${e["type"]} in loadShortcuts.";
log.e(hint);
}
} else {
// Only for backwards compatibility.
if (e["waypoint"] != null) shortcutsProduction.add(ShortcutLocation.fromJson(e));
if (e["waypoints"] != null) shortcutsProduction.add(ShortcutRoute.fromJson(e));
}
if (jsonStrReleaseNew != null) {
shortcutsReleaseNew = shortcuts.getShortcutsFromJson(jsonStrReleaseNew);
}

// Concat both.
shortcutsRelease.addAll(shortcutsProduction);
// Concat all.
shortcutsReleaseNew.addAll(shortcutsRelease);
shortcutsReleaseNew.addAll(shortcutsProduction);

final jsonStr = jsonEncode(shortcutsRelease.map((e) => e.toJson()).toList());
final jsonStr = jsonEncode(shortcutsReleaseNew.map((e) => e.toJson()).toList());

// Save shortcuts under region name (Hamburg, Dresden) so that production and release use the same shortcuts.
storage.setString("priobike.home.shortcuts.${Backend.release.regionName}", jsonStr);
// Remove the unused shortcuts.
storage.remove("priobike.home.shortcuts.${Backend.production.name}");
}

/// Migrate the search history (Hamburg beta => Hamburg).
Future<void> migrateSearchHistory() async {
/// Migrate all shortcuts (staging => Dresden).
Future<void> migrateShortcutsStaging() async {
final storage = await SharedPreferences.getInstance();

Shortcuts shortcuts = getIt<Shortcuts>();

// Get the current shortcuts of the currently used backend.
final jsonStrStaging = storage.getString("priobike.home.shortcuts.${Backend.staging.name}");
// Return on no old key found.
if (jsonStrStaging == null) return;
final jsonStrStagingNew = storage.getString("priobike.home.shortcuts.${Backend.staging.regionName}");

List<Shortcut> shortcutsStagingNew = [];

List<Shortcut> shortcutsStaging = shortcuts.getShortcutsFromJson(jsonStrStaging);

if (jsonStrStagingNew != null) {
shortcutsStagingNew = shortcuts.getShortcutsFromJson(jsonStrStagingNew);
}

// Concat all.
shortcutsStagingNew.addAll(shortcutsStaging);

final jsonStr = jsonEncode(shortcutsStagingNew.map((e) => e.toJson()).toList());

// Save shortcuts under region name (Hamburg, Dresden) so that production and release use the same shortcuts.
storage.setString("priobike.home.shortcuts.${Backend.staging.regionName}", jsonStr);
// Remove the unused shortcuts.
storage.remove("priobike.home.shortcuts.${Backend.staging.name}");
}

/// Migrate the search history (production/release => Hamburg).
Future<void> migrateSearchHistoryProduction() async {
final preferences = await SharedPreferences.getInstance();

// Load production and release lists.
Expand All @@ -105,4 +109,25 @@ class Migration with ChangeNotifier {
// Remove old list.
await preferences.remove("priobike.routing.searchHistory.${Backend.production.name}");
}

/// Migrate the search history (staging => Dresden).
Future<void> migrateSearchHistoryStaging() async {
final preferences = await SharedPreferences.getInstance();

// Load production and release lists.
List<String>? searchHistoryListStaging =
preferences.getStringList("priobike.routing.searchHistory.${Backend.staging.name}");
// Return on no key found.
if (searchHistoryListStaging == null) return;

List<String> searchHistoryListStagingNew =
preferences.getStringList("priobike.routing.searchHistory.${Backend.staging.regionName}") ?? [];
// Concat both lists.
searchHistoryListStagingNew.addAll(searchHistoryListStaging);
// Store concatenated list.
await preferences.setStringList(
"priobike.routing.searchHistory.${Backend.staging.regionName}", searchHistoryListStagingNew);
// Remove old list.
await preferences.remove("priobike.routing.searchHistory.${Backend.staging.name}");
}
}

0 comments on commit 72fc3b8

Please sign in to comment.