persist history options
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
class DateTimeRangeConverter
|
||||
implements JsonConverter<DateTimeRange?, Map<String, dynamic>?> {
|
||||
const DateTimeRangeConverter();
|
||||
|
||||
@override
|
||||
DateTimeRange? fromJson(Map<String, dynamic>? json) {
|
||||
if (json == null) return null;
|
||||
|
||||
return DateTimeRange(
|
||||
start: DateTime.parse(json['start'] as String),
|
||||
end: DateTime.parse(json['end'] as String),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, dynamic>? toJson(DateTimeRange? dateTimeRange) {
|
||||
if (dateTimeRange == null) return null;
|
||||
|
||||
return {
|
||||
'start': dateTimeRange.start.toIso8601String(),
|
||||
'end': dateTimeRange.end.toIso8601String(),
|
||||
};
|
||||
}
|
||||
}
|
||||
+10
-1
@@ -2,19 +2,28 @@ import 'package:copy_with_extension/copy_with_extension.dart';
|
||||
import 'package:fast_equatable/fast_equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_mozilla_components/flutter_mozilla_components.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:weblibre/data/database/converters/date_time_range.dart';
|
||||
|
||||
part 'history_filter_options.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
@CopyWith()
|
||||
class HistoryFilterOptions with FastEquatable {
|
||||
@DateTimeRangeConverter()
|
||||
final DateTimeRange<DateTime>? dateRange;
|
||||
final Set<VisitType> visitTypes;
|
||||
|
||||
HistoryFilterOptions({required this.dateRange, required this.visitTypes});
|
||||
|
||||
HistoryFilterOptions.withDefaults()
|
||||
: this(dateRange: null, visitTypes: {VisitType.link, VisitType.typed});
|
||||
: this(dateRange: null, visitTypes: {VisitType.link});
|
||||
|
||||
@override
|
||||
List<Object?> get hashParameters => [dateRange, visitTypes];
|
||||
|
||||
factory HistoryFilterOptions.fromJson(Map<String, dynamic> json) =>
|
||||
_$HistoryFilterOptionsFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$HistoryFilterOptionsToJson(this);
|
||||
}
|
||||
|
||||
+34
@@ -73,3 +73,37 @@ extension $HistoryFilterOptionsCopyWith on HistoryFilterOptions {
|
||||
_$HistoryFilterOptionsCWProxy get copyWith =>
|
||||
_$HistoryFilterOptionsCWProxyImpl(this);
|
||||
}
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
HistoryFilterOptions _$HistoryFilterOptionsFromJson(
|
||||
Map<String, dynamic> json,
|
||||
) => HistoryFilterOptions(
|
||||
dateRange: const DateTimeRangeConverter().fromJson(
|
||||
json['dateRange'] as Map<String, dynamic>?,
|
||||
),
|
||||
visitTypes: (json['visitTypes'] as List<dynamic>)
|
||||
.map((e) => $enumDecode(_$VisitTypeEnumMap, e))
|
||||
.toSet(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$HistoryFilterOptionsToJson(
|
||||
HistoryFilterOptions instance,
|
||||
) => <String, dynamic>{
|
||||
'dateRange': const DateTimeRangeConverter().toJson(instance.dateRange),
|
||||
'visitTypes': instance.visitTypes.map((e) => _$VisitTypeEnumMap[e]!).toList(),
|
||||
};
|
||||
|
||||
const _$VisitTypeEnumMap = {
|
||||
VisitType.link: 'link',
|
||||
VisitType.typed: 'typed',
|
||||
VisitType.bookmark: 'bookmark',
|
||||
VisitType.embed: 'embed',
|
||||
VisitType.redirectPermanent: 'redirectPermanent',
|
||||
VisitType.redirectTemporary: 'redirectTemporary',
|
||||
VisitType.download: 'download',
|
||||
VisitType.framedLink: 'framedLink',
|
||||
VisitType.reload: 'reload',
|
||||
};
|
||||
|
||||
@@ -17,10 +17,14 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_mozilla_components/flutter_mozilla_components.dart';
|
||||
import 'package:riverpod/experimental/persist.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:weblibre/features/geckoview/features/history/domain/entities/history_filter_options.dart';
|
||||
import 'package:weblibre/features/user/data/providers.dart';
|
||||
|
||||
part 'providers.g.dart';
|
||||
|
||||
@@ -34,13 +38,28 @@ class HistoryFilter extends _$HistoryFilter {
|
||||
}
|
||||
}
|
||||
|
||||
void reset() {
|
||||
state = HistoryFilterOptions.withDefaults();
|
||||
}
|
||||
|
||||
void setDateRange(DateTimeRange<DateTime>? range) {
|
||||
state = state.copyWith.dateRange(range);
|
||||
}
|
||||
|
||||
@override
|
||||
HistoryFilterOptions build() {
|
||||
return HistoryFilterOptions.withDefaults();
|
||||
final riverpodStorage = ref.watch(riverpodDatabaseStorageProvider);
|
||||
|
||||
persist(
|
||||
riverpodStorage,
|
||||
key: 'HistoryFilterOptions',
|
||||
decode: (encoded) => HistoryFilterOptions.fromJson(
|
||||
jsonDecode(encoded) as Map<String, dynamic>,
|
||||
),
|
||||
encode: (state) => jsonEncode(state.toJson()),
|
||||
);
|
||||
|
||||
return stateOrNull ?? HistoryFilterOptions.withDefaults();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ final class HistoryFilterProvider
|
||||
}
|
||||
}
|
||||
|
||||
String _$historyFilterHash() => r'68a604fe857476f7c15be89352a7b10bc456c3d6';
|
||||
String _$historyFilterHash() => r'a30ed30dac56422144225adb2c5d2c1ba5c740dd';
|
||||
|
||||
abstract class _$HistoryFilter extends $Notifier<HistoryFilterOptions> {
|
||||
HistoryFilterOptions build();
|
||||
|
||||
Reference in New Issue
Block a user