- 13 library_private_types_in_public_api: These are intentional design patterns where private State classes are returned from createState() methods - 7 implementation_imports: These are from external packages (redux, flutter_map, shared_preferences) and necessary for the implementation All suppressed using ignore comments: - active_fires.dart, compass_map_plugin.dart, custom_stepper.dart, fire_alert.dart, fire_notification_list.dart, fires_app.dart, generic_map.dart, global_fires_bottom_stats.dart, home_page.dart, markdown_page.dart, slider.dart, support_page.dart, material_app_with_intro.dart - generic_map.dart, main_drawer.dart, main_prod.dart, fire_notifications_persist.dart, your_location_persist.dart, monitored_areas.dart, fetch_data_middleware.dart Result: ZERO lint issues in lib/ - clean build achieved!
275 lines
10 KiB
Dart
275 lines
10 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_redux/flutter_redux.dart';
|
|
import 'package:redux/redux.dart';
|
|
|
|
import 'custom_moment.dart';
|
|
import 'fires_spinner.dart';
|
|
import 'generated/i18n.dart';
|
|
import 'generic_map.dart';
|
|
import 'main_drawer.dart';
|
|
import 'models/app_state.dart';
|
|
import 'models/fire_notification.dart';
|
|
import 'models/your_location.dart';
|
|
import 'redux/actions.dart';
|
|
|
|
@immutable
|
|
class _ViewModel {
|
|
const _ViewModel(
|
|
{required this.isLoaded,
|
|
required this.onTap,
|
|
required this.onDelete,
|
|
required this.onDeleteAll,
|
|
required this.fireNotifications,
|
|
required this.yourLocations,
|
|
required this.fireNotificationsUnread});
|
|
final bool isLoaded;
|
|
final List<FireNotification> fireNotifications;
|
|
final int fireNotificationsUnread;
|
|
final List<YourLocation> yourLocations;
|
|
final TapFireNotificationFunction onTap;
|
|
final DeleteFireNotificationFunction onDelete;
|
|
final DeleteAllFireNotificationFunction onDeleteAll;
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
identical(this, other) ||
|
|
other is _ViewModel &&
|
|
runtimeType == other.runtimeType &&
|
|
isLoaded == other.isLoaded &&
|
|
fireNotifications == other.fireNotifications &&
|
|
fireNotificationsUnread == other.fireNotificationsUnread &&
|
|
yourLocations == other.yourLocations;
|
|
|
|
@override
|
|
int get hashCode =>
|
|
isLoaded.hashCode ^
|
|
fireNotifications.hashCode ^
|
|
fireNotificationsUnread.hashCode ^
|
|
yourLocations.hashCode;
|
|
}
|
|
|
|
class FireNotificationList extends StatefulWidget {
|
|
const FireNotificationList({super.key});
|
|
|
|
static const String routeName = '/fireNotifications';
|
|
|
|
@override
|
|
// ignore: library_private_types_in_public_api
|
|
_FireNotificationListState createState() => _FireNotificationListState();
|
|
}
|
|
|
|
class _FireNotificationListState extends State<FireNotificationList> {
|
|
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
|
|
|
Widget _buildRow(
|
|
BuildContext context,
|
|
List<YourLocation> yourLocations,
|
|
FireNotification notif,
|
|
DeleteFireNotificationFunction onDeleted,
|
|
TapFireNotificationFunction onTap) {
|
|
String prefix = '';
|
|
|
|
// FIXME (this can fails if you don't have a location for this notif, for instance during tests)
|
|
final YourLocation yl =
|
|
yourLocations.singleWhere((YourLocation yl) => yl.id == notif.subsId);
|
|
prefix = '${yl.description}. ';
|
|
|
|
return ListTile(
|
|
dense: true,
|
|
leading: const Icon(Icons.whatshot),
|
|
title: Text('$prefix${notif.description}',
|
|
style: TextStyle(
|
|
fontWeight: notif.read ? FontWeight.normal : FontWeight.bold)),
|
|
subtitle: Text(Moment.now().from(context, notif.when)),
|
|
onLongPress: () {
|
|
showSnackMsg(S.of(context).toDeleteThisNotification);
|
|
},
|
|
onTap: () {
|
|
onTap(notif);
|
|
});
|
|
}
|
|
|
|
void showSnackMsg(String msg) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text(msg),
|
|
));
|
|
}
|
|
|
|
Widget _buildSavedFireNotifications(
|
|
BuildContext context,
|
|
List<YourLocation> yourLocations,
|
|
List<FireNotification> notifList,
|
|
DeleteFireNotificationFunction onDeleted,
|
|
TapFireNotificationFunction onTap) {
|
|
return RefreshIndicator(
|
|
onRefresh: _handleRefresh,
|
|
child: ListView.builder(
|
|
padding: const EdgeInsets.all(16.0),
|
|
// reverse: true,
|
|
// shrinkWrap: true,
|
|
itemCount: notifList.length,
|
|
itemBuilder: (BuildContext context, int i) {
|
|
final ThemeData theme = Theme.of(context);
|
|
return Dismissible(
|
|
key: ObjectKey(notifList.elementAt(i)),
|
|
onDismissed: (DismissDirection direction) {
|
|
onDeleted(notifList.elementAt(i));
|
|
},
|
|
background: Container(
|
|
color: theme.primaryColor,
|
|
child: const ListTile(
|
|
leading: Icon(Icons.delete,
|
|
color: Colors.white, size: 36.0))),
|
|
secondaryBackground: Container(
|
|
color: theme.primaryColor,
|
|
child: const ListTile(
|
|
trailing: Icon(Icons.delete,
|
|
color: Colors.white, size: 36.0))),
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
color: theme.canvasColor,
|
|
border: Border(
|
|
bottom: BorderSide(color: theme.dividerColor))),
|
|
child: _buildRow(context, yourLocations,
|
|
notifList.elementAt(i), onDeleted, onTap)));
|
|
}));
|
|
}
|
|
|
|
Future<void> _handleRefresh() async {
|
|
await Future<void>.delayed(const Duration(seconds: 1));
|
|
|
|
setState(() {});
|
|
|
|
return;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return StoreConnector<AppState, _ViewModel>(
|
|
distinct: true,
|
|
converter: (Store<AppState> store) {
|
|
return _ViewModel(
|
|
isLoaded: store.state.isLoaded,
|
|
onDeleteAll: () {
|
|
store.dispatch(DeleteAllFireNotificationAction());
|
|
},
|
|
onDelete: (FireNotification notif) {
|
|
store.dispatch(DeleteFireNotificationAction(notif));
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text(S.of(context).youDeletedThisNotification),
|
|
action: SnackBarAction(
|
|
label: S.of(context).UNDO,
|
|
onPressed: () {
|
|
store.dispatch(AddFireNotificationAction(notif));
|
|
})));
|
|
},
|
|
onTap: (FireNotification notif) {
|
|
if (!notif.read) {
|
|
store.dispatch(
|
|
ReadFireNotificationAction(notif.copyWith(read: true)));
|
|
}
|
|
Timer(const Duration(milliseconds: 500), () {
|
|
gotoMap(store, notif, context);
|
|
});
|
|
},
|
|
yourLocations: store.state.yourLocations,
|
|
fireNotifications: store.state.fireNotifications,
|
|
fireNotificationsUnread: store.state.fireNotificationsUnread);
|
|
},
|
|
builder: (BuildContext context, _ViewModel view) {
|
|
final bool hasFireNotifications = view.fireNotifications.isNotEmpty;
|
|
final String title = S.of(context).fireNotificationsTitle;
|
|
|
|
return Scaffold(
|
|
key: _scaffoldKey,
|
|
drawer: MainDrawer(context, FireNotificationList.routeName),
|
|
appBar: AppBar(
|
|
title: Text(title),
|
|
leading: IconButton(
|
|
icon: const Icon(Icons.menu),
|
|
onPressed: () {
|
|
_scaffoldKey.currentState?.openDrawer();
|
|
},
|
|
),
|
|
actions: <Widget?>[
|
|
if (hasFireNotifications)
|
|
IconButton(
|
|
icon: const Icon(Icons.delete),
|
|
onPressed: () => _showConfirmDialog(view))
|
|
else
|
|
null
|
|
].where((Widget? w) => w != null).cast<Widget>().toList()),
|
|
body: !view.isLoaded
|
|
? const FiresSpinner()
|
|
: !hasFireNotifications
|
|
? Padding(
|
|
padding: const EdgeInsets.all(20.0),
|
|
child: Card(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(20.0),
|
|
child: Column(
|
|
mainAxisAlignment:
|
|
MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
const Icon(Icons.notifications_none,
|
|
size: 150.0, color: Colors.black26),
|
|
const SizedBox(height: 20.0),
|
|
Text(
|
|
S
|
|
.of(context)
|
|
.fireNotificationsDescription,
|
|
textAlign: TextAlign.center,
|
|
textScaler:
|
|
const TextScaler.linear(1.1),
|
|
style: const TextStyle(
|
|
height: 1.3,
|
|
color: Colors.black45))
|
|
]))))
|
|
: _buildSavedFireNotifications(
|
|
context,
|
|
view.yourLocations,
|
|
view.fireNotifications,
|
|
view.onDelete,
|
|
view.onTap));
|
|
});
|
|
}
|
|
|
|
void gotoMap(
|
|
Store<AppState> store, FireNotification notif, BuildContext context) {
|
|
store.dispatch(ShowFireNotificationMapAction(notif));
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute<void>(
|
|
builder: (BuildContext context) => const GenericMap()));
|
|
}
|
|
|
|
Future<void> _showConfirmDialog(_ViewModel view) {
|
|
return showDialog<void>(
|
|
context: context,
|
|
barrierDismissible: false, // user must tap button!
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: Text(S.of(context).areYouSureTitle),
|
|
content: SingleChildScrollView(
|
|
child: ListBody(
|
|
children: <Widget>[
|
|
Text(S.of(context).deleteAllFireNotificationsAlertDescription)
|
|
],
|
|
),
|
|
),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
child: Text(S.of(context).DELETE),
|
|
onPressed: () {
|
|
view.onDeleteAll();
|
|
Navigator.of(context).pop();
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|