Skip to content

Commit

Permalink
add migration test in internal settings
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulPickhardt committed Nov 30, 2023
1 parent a5db8c7 commit d89cb57
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 11 deletions.
100 changes: 90 additions & 10 deletions lib/migration/services.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
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/routing/models/waypoint.dart';
import 'package:priobike/settings/models/backend.dart';
import 'package:shared_preferences/shared_preferences.dart';

Expand Down Expand Up @@ -90,43 +94,119 @@ class Migration {

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

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

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

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

// Load production and release lists.
List<String>? searchHistoryListStaging =
preferences.getStringList("priobike.routing.searchHistory.${Backend.staging.name}");
storage.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}") ?? [];
storage.getStringList("priobike.routing.searchHistory.${Backend.staging.regionName}") ?? [];
// Concat both lists.
searchHistoryListStagingNew.addAll(searchHistoryListStaging);
// Store concatenated list.
await preferences.setStringList(
await storage.setStringList(
"priobike.routing.searchHistory.${Backend.staging.regionName}", searchHistoryListStagingNew);
// Remove old list.
await preferences.remove("priobike.routing.searchHistory.${Backend.staging.name}");
await storage.remove("priobike.routing.searchHistory.${Backend.staging.name}");
}

/// Adds test migration data for all backends.
Future<void> addTestMigrationData() async {
final storage = await SharedPreferences.getInstance();

// Create old data for Staging.
List<Shortcut> stagingList = [
ShortcutLocation(
id: UniqueKey().toString(),
name: "Staging-Location-Test",
waypoint: Waypoint(51.038294, 13.703280, address: "Clara-Viebig-Straße 9"),
),
ShortcutRoute(
id: UniqueKey().toString(),
name: "Staging-Route-Test",
waypoints: [
Waypoint(51.038294, 13.703280, address: "Clara-Viebig-Straße 9"),
Waypoint(50.979067, 13.882596, address: "Elberadweg Heidenau"),
],
),
];

final jsonStrStaging = jsonEncode(stagingList.map((e) => e.toJson()).toList());

storage.setString("priobike.home.shortcuts.${Backend.staging.name}", jsonStrStaging);

// Create old data for Production.
List<Shortcut> productionList = [
ShortcutLocation(
id: UniqueKey().toString(),
name: "Production-Location-Test",
waypoint: Waypoint(53.5415701077766, 9.984275605794686, address: "Staging-test"),
),
ShortcutRoute(
id: UniqueKey().toString(),
name: "Production-Route-Test",
waypoints: [
Waypoint(53.560863, 9.990909, address: "Theodor-Heuss-Platz, Hamburg"),
Waypoint(53.564378, 9.978001, address: "Rentzelstraße 55, 20146 Hamburg"),
],
),
];

final jsonStrProduction = jsonEncode(productionList.map((e) => e.toJson()).toList());

storage.setString("priobike.home.shortcuts.${Backend.production.name}", jsonStrProduction);

// Create old data for Release.
List<Shortcut> releaseList = [
ShortcutLocation(
id: UniqueKey().toString(),
name: "Release-Location-Test",
waypoint: Waypoint(53.5415701077766, 9.984275605794686, address: "Staging-test"),
),
ShortcutRoute(
id: UniqueKey().toString(),
name: "Release-Route-Test",
waypoints: [
Waypoint(53.560863, 9.990909, address: "Theodor-Heuss-Platz, Hamburg"),
Waypoint(53.564378, 9.978001, address: "Rentzelstraße 55, 20146 Hamburg"),
],
),
];

final jsonStrRelease = jsonEncode(releaseList.map((e) => e.toJson()).toList());

storage.setString("priobike.home.shortcuts.${Backend.release.name}", jsonStrRelease);

// Create old search history data.
await storage.setStringList("priobike.routing.searchHistory.${Backend.staging.name}",
[json.encode(Waypoint(51.038294, 13.703280, address: "Clara-Viebig-Straße 9").toJSON())]);
await storage.setStringList("priobike.routing.searchHistory.${Backend.production.name}",
[json.encode(Waypoint(53.560863, 9.990909, address: "Theodor-Heuss-Platz, Hamburg").toJSON())]);
await storage.setStringList("priobike.routing.searchHistory.${Backend.release.name}",
[json.encode(Waypoint(53.560863, 9.990909, address: "Theodor-Heuss-Platz, Hamburg").toJSON())]);
}
}
2 changes: 1 addition & 1 deletion lib/migration/user_transfer_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class UserTransferViewState extends State<UserTransferView> {
@override
Widget build(BuildContext context) {
// Display when backend ist not release and user did not seen this view yet.
if ((settings.didViewUserTransfer == true || settings.backend == Backend.release) &&
if ((settings.didViewUserTransfer == true || settings.backend != Backend.production) &&
(!isUserTransferring) &&
(widget.child != null)) {
return widget.child!;
Expand Down
23 changes: 23 additions & 0 deletions lib/settings/views/internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:priobike/gamification/common/services/user_service.dart';
import 'package:priobike/gamification/goals/services/goals_service.dart';
import 'package:priobike/home/services/shortcuts.dart';
import 'package:priobike/main.dart';
import 'package:priobike/migration/services.dart';
import 'package:priobike/news/services/news.dart';
import 'package:priobike/positioning/services/positioning.dart';
import 'package:priobike/privacy/services.dart';
Expand Down Expand Up @@ -162,6 +163,11 @@ class InternalSettingsViewState extends State<InternalSettingsView> {
if (mounted) Navigator.pop(context);
}

/// A callback that adds test migration data for testing.
void addTestMigrationData() {
Migration().addTestMigrationData();
}

@override
Widget build(BuildContext context) {
return AnnotatedRegionWrapper(
Expand Down Expand Up @@ -361,6 +367,23 @@ class InternalSettingsViewState extends State<InternalSettingsView> {
),
),
const SmallVSpace(),
Padding(
padding: const EdgeInsets.only(left: 34, top: 8, bottom: 8, right: 24),
child: Small(
text:
"Durch Migration testen werden jeweils ein alter Shortcut und eine alte Suchanfrage angelegt (staging, production, release). Diese müssten nach einem Neustart der App jeweils mit angezeigt werden.",
context: context,
),
),
Padding(
padding: const EdgeInsets.only(top: 8),
child: SettingsElement(
title: "Migration testen",
icon: Icons.start,
callback: addTestMigrationData,
),
),
const SmallVSpace(),
],
),
),
Expand Down

0 comments on commit d89cb57

Please sign in to comment.