Skip to content

Commit

Permalink
wip migration;
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianPix committed Oct 24, 2023
1 parent ff01e4e commit 3631a10
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 24 deletions.
20 changes: 18 additions & 2 deletions src/flutter_app/lib/components/common/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ class _SettingsState extends State<Settings>{
label: const Text("print cache verbose"),
)
])),
Card(child: Column(children: [
const Padding(
padding: EdgeInsets.only(top: 5),
child: Text("Migrate", style: MyTextStyles.title)
),
ElevatedButton.icon(
onPressed: () {adminService.migrateMedia();},
icon: const Icon(
Icons.update_rounded,
color: Colors.black,
size: 30.0,
semanticLabel: 'migrate images',
),
label: const Text("migrate images"),
)
])),
Card(child: Column(children: [
const Padding(
padding: EdgeInsets.only(top: 5),
Expand All @@ -146,9 +162,9 @@ class _SettingsState extends State<Settings>{
Icons.delete,
color: Colors.black,
size: 30.0,
semanticLabel: 'delete all data incl. images',
semanticLabel: 'delete all data',
),
label: const Text("delete all data incl. images"),
label: const Text("delete all data"),
)
])),
],
Expand Down
38 changes: 26 additions & 12 deletions src/flutter_app/lib/services/admin_service.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'package:climbing_diary/components/common/my_notifications.dart';
import 'package:climbing_diary/services/cache_service.dart';
import 'package:climbing_diary/services/error_service.dart';
import '../config/environment.dart';
import 'package:dio/dio.dart';

import '../data/network/dio_client.dart';
import '../data/sharedprefs/shared_preference_helper.dart';
import 'locator.dart';
import 'package:http/http.dart' as http;

class AdminService {
final netWorkLocator = getIt.get<DioClient>();
Expand All @@ -15,24 +18,35 @@ class AdminService {
Future<void> deleteAll() async {
try {
final Response dataResponse = await netWorkLocator.dio.delete('$climbingApiHost/admin/');
if (dataResponse.statusCode != 200) {
throw Exception('Failed to delete all data');
}
if (dataResponse.statusCode != 200) throw Exception('Failed to delete all data');
await CacheService.clearCache();
MyNotifications.showPositiveNotification('All your data was deleted');
final Response mediaResponse = await netWorkLocator.dio.delete('$mediaApiHost/media');
if (mediaResponse.statusCode != 204) {
throw Exception('Failed to delete all images');
}
// TODO delete from cache
MyNotifications.showPositiveNotification('All your images were deleted');
} catch (e) {
if (e is DioError) {
if (e.error.toString().contains('OS Error: No address associated with hostname, errno = 7')){
// TODO this means we are offline so queue this and delete later
}
}
} finally {
// TODO delete from cache;
}
}

Future<void> migrateMedia() async {
try {
final Response mediaResponse = await netWorkLocator.dio.get('$mediaApiHost/media');
if (mediaResponse.statusCode != 200) throw Exception('Failed to get media');
for (final medium in mediaResponse.data!){
final Response mediumResponse = await netWorkLocator.dio.get('$mediaApiHost/media/${medium["id"]}/access-url');
if (mediumResponse.statusCode != 200) throw Exception('Failed to get medium');
final idUrl = mediumResponse.data!;
String url = idUrl['url'];
// download image
var uri = Uri.https(url);
var response = await http.get(uri);
print('Response status: ${response.statusCode}');
// convert to UInt8List
// create medium
}
} catch (e) {
ErrorService.handleConnectionErrors(e);
}
}
}
1 change: 1 addition & 0 deletions src/flutter_app/lib/services/archive_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class ArchiveService {
MyNotifications.showPositiveNotification("exported to $directoryPath");
}

/// import data from a user picked directory
Future<void> import() async{
if (! await _requestPermission(Permission.storage)) return;
// pick a directory to export to
Expand Down
12 changes: 8 additions & 4 deletions src/flutter_app/lib/services/ascent_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ class AscentService {
Box pitchBox = Hive.box(Pitch.boxName);
Map pitchMap = pitchBox.get(pitchId);
Pitch pitch = Pitch.fromCache(pitchMap);
pitch.ascentIds.add(createAscent.id);
await pitchBox.put(pitch.id, pitch.toJson());
if (!pitch.ascentIds.contains(createAscent.id)){
pitch.ascentIds.add(createAscent.id);
await pitchBox.put(pitch.id, pitch.toJson());
}
if (online == null || !online) return createAscent;
// try to upload and update cache if successful
Map data = createAscent.toJson();
Expand Down Expand Up @@ -134,8 +136,10 @@ class AscentService {
Box singlePitchRouteBox = Hive.box(SinglePitchRoute.boxName);
Map singlePitchRouteMap = singlePitchRouteBox.get(singlePitchRouteId);
SinglePitchRoute singlePitchRoute = SinglePitchRoute.fromCache(singlePitchRouteMap);
singlePitchRoute.ascentIds.add(createAscent.id);
await singlePitchRouteBox.put(singlePitchRoute.id, singlePitchRoute.toJson());
if (!singlePitchRoute.ascentIds.contains(createAscent.id)){
singlePitchRoute.ascentIds.add(createAscent.id);
await singlePitchRouteBox.put(singlePitchRoute.id, singlePitchRoute.toJson());
}
if (online == null || !online) return createAscent;
// try to upload and update cache if successful
Map data = createAscent.toJson();
Expand Down
6 changes: 4 additions & 2 deletions src/flutter_app/lib/services/multi_pitch_route_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ class MultiPitchRouteService {
Box spotBox = Hive.box(Spot.boxName);
Map spotMap = spotBox.get(spotId);
Spot spot = Spot.fromCache(spotMap);
spot.multiPitchRouteIds.add(multiPitchRoute.id);
await spotBox.put(spotId, spot.toJson());
if (!spot.multiPitchRouteIds.contains(multiPitchRoute.id)) {
spot.multiPitchRouteIds.add(multiPitchRoute.id);
await spotBox.put(spotId, spot.toJson());
}
if (online == null || !online) return multiPitchRoute;
// try to upload and update cache if successful
Map data = multiPitchRoute.toJson();
Expand Down
6 changes: 4 additions & 2 deletions src/flutter_app/lib/services/pitch_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ class PitchService {
Box multiPitchRouteBox = Hive.box(MultiPitchRoute.boxName);
Map multiPitchRouteMap = multiPitchRouteBox.get(routeId);
MultiPitchRoute multiPitchRoute = MultiPitchRoute.fromCache(multiPitchRouteMap);
multiPitchRoute.pitchIds.add(createPitch.id);
await multiPitchRouteBox.put(multiPitchRoute.id, multiPitchRoute.toJson());
if (!multiPitchRoute.pitchIds.contains(createPitch.id)) {
multiPitchRoute.pitchIds.add(createPitch.id);
await multiPitchRouteBox.put(multiPitchRoute.id, multiPitchRoute.toJson());
}
if (online == null || !online) return createPitch;
// try to upload and update cache if successful
Map data = createPitch.toJson();
Expand Down
6 changes: 4 additions & 2 deletions src/flutter_app/lib/services/single_pitch_route_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ class SinglePitchRouteService {
Box spotBox = Hive.box(Spot.boxName);
Map spotMap = spotBox.get(spotId);
Spot spot = Spot.fromCache(spotMap);
spot.singlePitchRouteIds.add(createRoute.id);
await spotBox.put(spotId, spot.toJson());
if (!spot.singlePitchRouteIds.contains(createRoute.id)) {
spot.singlePitchRouteIds.add(createRoute.id);
await spotBox.put(spotId, spot.toJson());
}
if (online == null || !online) return createRoute;
// try to upload and update cache if successful
Map data = createRoute.toJson();
Expand Down

0 comments on commit 3631a10

Please sign in to comment.