map_list_view.dart 3.71 KB
Newer Older
CLOUARD Regis's avatar
CLOUARD Regis committed
1
2
3
4
5
6
7
8
9
10
11
12
13
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:provider/provider.dart';
import 'package:vikazimut/map_choice/map_choice_presenter.dart';
import 'package:vikazimut/map_choice/map_proxy.dart';
import 'package:vikazimut/map_choice/title_presenter.dart';
import 'package:vikazimut/utils/alert_dialog.dart';
import 'package:vikazimut/utils/translations.dart';

import 'directory_cleaner.dart';
import 'map_proxy_list_view.dart';
import 'memory_info_presenter.dart';

CLOUARD Regis's avatar
CLOUARD Regis committed
14
class MapListView extends StatefulWidget {
CLOUARD Regis's avatar
CLOUARD Regis committed
15
16
17
18
19
  final String searchQuery;
  final MapChoicePresenter presenter;

  MapListView({required this.presenter, required this.searchQuery});

CLOUARD Regis's avatar
CLOUARD Regis committed
20
21
22
23
24
25
26
27
28
29
30
31
32
  @override
  _MapListViewState createState() => _MapListViewState();
}

class _MapListViewState extends State<MapListView> {
  Future<List<MapProxy>>? _mapProxyList;

  @override
  void initState() {
    super.initState();
    _mapProxyList = widget.presenter.fetchMapList(http.Client());
  }

CLOUARD Regis's avatar
CLOUARD Regis committed
33
34
35
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<MapProxy>>(
CLOUARD Regis's avatar
CLOUARD Regis committed
36
      future: _mapProxyList,
CLOUARD Regis's avatar
CLOUARD Regis committed
37
38
      builder: (context, snapshot) {
        if (snapshot.hasError) {
CLOUARD Regis's avatar
CLOUARD Regis committed
39
          return _ErrorWidget();
CLOUARD Regis's avatar
CLOUARD Regis committed
40
        } else if (snapshot.hasData) {
CLOUARD Regis's avatar
CLOUARD Regis committed
41
          widget.presenter.markMapsAsDownloaded(snapshot.data!);
CLOUARD Regis's avatar
CLOUARD Regis committed
42
43
44
45
46
47
48
          cleanObsoleteMaps(snapshot.data!);
          WidgetsBinding.instance!.addPostFrameCallback((_) {
            Provider.of<MemoryInfoPresenter>(context, listen: false).updateMemorySize();
            Provider.of<TitlePresenter>(context, listen: false).numberOfMaps = snapshot.data!.length;
            if (snapshot.data!.isEmpty) {
              showErrorDialog(
                context,
CLOUARD Regis's avatar
CLOUARD Regis committed
49
                title: Translations.getString('error_title'),
CLOUARD Regis's avatar
CLOUARD Regis committed
50
51
                message: Translations.getString("no_network_and_no_stored_map_error_message"),
              );
CLOUARD Regis's avatar
CLOUARD Regis committed
52
            } else if (widget.presenter.isLocalMapList) {
CLOUARD Regis's avatar
CLOUARD Regis committed
53
54
55
56
57
              final snackBar = SnackBar(
                content: Text(
                  Translations.getString("server_connection_error_use_memorized_map"),
                  textAlign: TextAlign.center,
                ),
CLOUARD Regis's avatar
CLOUARD Regis committed
58
                duration: const Duration(seconds: 4),
CLOUARD Regis's avatar
CLOUARD Regis committed
59
60
61
62
              );
              ScaffoldMessenger.of(context).showSnackBar(snackBar);
            }
          });
Clouard Regis's avatar
Clouard Regis committed
63
          return MapProxyListView(presenter: widget.presenter, mapProxies: snapshot.data!, searchQuery: widget.searchQuery);
CLOUARD Regis's avatar
CLOUARD Regis committed
64
        } else {
CLOUARD Regis's avatar
CLOUARD Regis committed
65
          return _WaitingWidget();
CLOUARD Regis's avatar
CLOUARD Regis committed
66
67
68
69
70
        }
      },
    );
  }
}
CLOUARD Regis's avatar
CLOUARD Regis committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

class _ErrorWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
        child: Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(
          Translations.getString('error_title'),
          style: const TextStyle(
            fontSize: 24.0,
            fontWeight: FontWeight.w700,
          ),
        ),
        Text(
          Translations.getString("no_network_and_no_stored_map_error_message"),
          textAlign: TextAlign.center,
          style: const TextStyle(
            fontSize: 16.0,
          ),
        ),
      ],
    ));
  }
}

class _WaitingWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Padding(
        padding: const EdgeInsets.only(
          left: 8.0,
          right: 8.0,
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              Translations.getString("map_list_downloading_message"),
              textAlign: TextAlign.center,
            ),
            CircularProgressIndicator(),
          ],
        ),
      ),
    );
  }
}