-
Notifications
You must be signed in to change notification settings - Fork 0
/
earthquake_query.dart
130 lines (113 loc) · 3.66 KB
/
earthquake_query.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
122
123
124
125
126
127
128
129
130
// Copyright (c) 2020-2023 Navibyte (https://navibyte.com). All rights reserved.
// Use of this source code is governed by a “BSD-3-Clause”-style license that is
// specified in the LICENSE file.
//
// Docs: https://github.com/navibyte/geospatial_demos
import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'earthquake_model.dart';
/// A minimum earthquake magnitude used by an earthquake filter.
enum Magnitude {
significant,
m45plus,
m25plus,
m10plus,
all;
/// An identifier used by the USGS service (not equals with the enum [name]).
String get code {
switch (this) {
case Magnitude.significant:
return 'significant';
case Magnitude.m45plus:
return '4.5';
case Magnitude.m25plus:
return '2.5';
case Magnitude.m10plus:
return '1.0';
case Magnitude.all:
return 'all';
}
}
}
/// A time period ("past from now") used by an earthquake filter.
enum Past {
hour,
day,
week,
month;
/// An identifier used by the USGS service (equals with the enum [name]).
String get code => name;
}
/// A query to filter earthquakes when requesting data from a data source.
///
/// This model class extends `Equatable` that implements `==` and `hashCode`.
class EarthquakeQuery extends Equatable {
/// The selection of the producer for earthquake data.
final EarthquakeProducer producer;
/// A minimum earthquake magnitude.
///
/// This is a required parameter at least for the USGS producer.
final Magnitude magnitude;
/// A time period ("past from now").
///
/// This is a required parameter at least for the USGS producer.
final Past past;
/// A query to filter earthquakes when requesting data from a data source.
const EarthquakeQuery({
required this.producer,
required this.magnitude,
required this.past,
});
/// Copies this query as a new instance with optional parameter values.
EarthquakeQuery copyWith({
EarthquakeProducer? producer,
Magnitude? magnitude,
Past? past,
}) =>
EarthquakeQuery(
producer: producer ?? this.producer,
magnitude: magnitude ?? this.magnitude,
past: past ?? this.past,
);
@override
List<Object?> get props => [producer, magnitude, past];
}
/// The state notifier provider for an query used to filter earthquakes.
///
/// This is used `earthquakeMarkers` to get a filter for earthquakes. Filter
/// parameters can be customized on the settings view.
final earthquakeFilter =
StateNotifierProvider<EarthquakeFilterNotifier, EarthquakeQuery>(
(ref) => EarthquakeFilterNotifier(),
);
/// The state notifier class for an query used to filter earthquakes.
class EarthquakeFilterNotifier extends StateNotifier<EarthquakeQuery> {
EarthquakeFilterNotifier()
: super(
// The default query filters earthquakes with a magnitude above 4.5
// for the past week.
const EarthquakeQuery(
producer: EarthquakeProducer.usgs,
magnitude: Magnitude.m45plus,
past: Past.week,
),
);
/// Updates the [producer] selection on this filter.
void updateProducer(EarthquakeProducer producer) {
if (producer != state.producer) {
state = state.copyWith(producer: producer);
}
}
/// Updates the minimum [magnitude] selection on this filter.
void updateMagnitude(Magnitude magnitude) {
if (magnitude != state.magnitude) {
state = state.copyWith(magnitude: magnitude);
}
}
/// Updates the time period or [past] selection on this filter.
void updatePast(Past past) {
if (past != state.past) {
state = state.copyWith(past: past);
}
}
}