Skip to content

Commit

Permalink
fix: Show proper loading and error for ViewImportOverview.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Oct 16, 2023
1 parent 091674a commit 263c86b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
85 changes: 66 additions & 19 deletions lib/screens/import_task_sheet_widgets/ViewImportOverview.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_logs/flutter_logs.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart'
hide PlatformListTile;
import 'package:locus/screens/locations_overview_screen_widgets/LocationFetchers.dart';
import 'package:locus/constants/values.dart';
import 'package:locus/services/location_fetcher_service/Fetcher.dart';
import 'package:locus/services/location_point_service.dart';
import 'package:locus/services/view_service/index.dart';
import 'package:provider/provider.dart';
import 'package:locus/utils/load_status.dart';

import '../../constants/spacing.dart';
import '../../utils/theme.dart';
Expand All @@ -27,20 +30,16 @@ class ViewImportOverview extends StatefulWidget {

class _ViewImportOverviewState extends State<ViewImportOverview> {
final LocationsMapController _controller = LocationsMapController();
final bool _isError = false;

LoadStatus loadStatus = LoadStatus.loading;

double timeOffset = 0;

@override
void initState() {
super.initState();

final fetchers = context.read<LocationFetchers>();
final lastLocation = fetchers.getLocations(widget.view).lastOrNull;

if (lastLocation != null) {
_controller.add(lastLocation);
}
_fetchLocation().then(_updateMap);
}

@override
Expand All @@ -50,6 +49,48 @@ class _ViewImportOverviewState extends State<ViewImportOverview> {
super.dispose();
}

void _updateMap(final LocationPointService location) {
_controller.add(location);
}

Future<LocationPointService> _fetchLocation() async {
FlutterLogs.logInfo(
LOG_TAG,
"ViewImportOverview",
"Fetching new locations...",
);

try {
final fetcher = Fetcher(widget.view);

await fetcher.fetchPreviewLocations();

FlutterLogs.logInfo(
LOG_TAG,
"ViewImportOverview",
"Fetched locations successfully!",
);

setState(() {
loadStatus = LoadStatus.success;
});

return fetcher.sortedLocations.last;
} catch (error) {
FlutterLogs.logError(
LOG_TAG,
"ViewImportOverview",
"Error while fetching locations: $error",
);

setState(() {
loadStatus = LoadStatus.error;
});

rethrow;
}
}

@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
Expand Down Expand Up @@ -79,7 +120,8 @@ class _ViewImportOverviewState extends State<ViewImportOverview> {
),
],
),
if (_isError)
const SizedBox(height: MEDIUM_SPACE),
if (loadStatus == LoadStatus.error)
Text(
l10n.locationsLoadingError,
style: TextStyle(
Expand All @@ -92,17 +134,22 @@ class _ViewImportOverviewState extends State<ViewImportOverview> {
textAlign: TextAlign.center,
style: getSubTitleTextStyle(context),
),
const SizedBox(height: MEDIUM_SPACE),
SizedBox(
width: double.infinity,
height: 200,
child: ClipRRect(
borderRadius: BorderRadius.circular(MEDIUM_SPACE),
child: LocationsMap(
controller: _controller,
const SizedBox(height: SMALL_SPACE),
if (loadStatus == LoadStatus.loading)
const Center(
child: CircularProgressIndicator(),
)
else
SizedBox(
width: double.infinity,
height: 200,
child: ClipRRect(
borderRadius: BorderRadius.circular(MEDIUM_SPACE),
child: LocationsMap(
controller: _controller,
),
),
),
),
],
const SizedBox(height: MEDIUM_SPACE),
PlatformElevatedButton(
Expand Down
1 change: 0 additions & 1 deletion lib/utils/nostr_fetcher/Socket.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter_logs/flutter_logs.dart';
Expand Down

0 comments on commit 263c86b

Please sign in to comment.