kopia lustrzana https://gitlab.com/mysocialportal/relatica
Convert HashtagService to Riverpod controller
rodzic
1e360c6a57
commit
4cb45cd894
|
@ -1,12 +1,12 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../globals.dart';
|
||||
import '../../riverpod_controllers/hashtag_service.dart';
|
||||
import '../../services/entry_manager_service.dart';
|
||||
import '../../services/hashtag_service.dart';
|
||||
import '../../utils/active_profile_selector.dart';
|
||||
|
||||
class HashtagAutocompleteOptions extends StatelessWidget {
|
||||
class HashtagAutocompleteOptions extends ConsumerWidget {
|
||||
const HashtagAutocompleteOptions({
|
||||
super.key,
|
||||
required this.id,
|
||||
|
@ -19,7 +19,7 @@ class HashtagAutocompleteOptions extends StatelessWidget {
|
|||
final ValueSetter<String> onHashtagTap;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final manager = context
|
||||
.read<ActiveProfileSelector<EntryManagerService>>()
|
||||
.activeEntry
|
||||
|
@ -27,7 +27,7 @@ class HashtagAutocompleteOptions extends StatelessWidget {
|
|||
final postTreeHashtags =
|
||||
manager.getPostTreeHashtags(id).getValueOrElse(() => [])..sort();
|
||||
final hashtagsFromService =
|
||||
getIt<HashtagService>().getMatchingHashTags(query);
|
||||
ref.watch(hashtagServiceProvider(searchString: query));
|
||||
final hashtags = [...postTreeHashtags, ...hashtagsFromService];
|
||||
|
||||
if (hashtags.isEmpty) return const SizedBox.shrink();
|
||||
|
|
|
@ -15,6 +15,7 @@ import 'friendica_client/friendica_client.dart';
|
|||
import 'globals.dart';
|
||||
import 'models/auth/profile.dart';
|
||||
import 'models/instance_info.dart';
|
||||
import 'riverpod_controllers/hashtag_service.dart';
|
||||
import 'services/auth_service.dart';
|
||||
import 'services/blocks_manager.dart';
|
||||
import 'services/connections_manager.dart';
|
||||
|
@ -24,7 +25,6 @@ import 'services/feature_version_checker.dart';
|
|||
import 'services/fediverse_server_validator.dart';
|
||||
import 'services/follow_requests_manager.dart';
|
||||
import 'services/gallery_service.dart';
|
||||
import 'services/hashtag_service.dart';
|
||||
import 'services/interactions_manager.dart';
|
||||
import 'services/network_status_service.dart';
|
||||
import 'services/notifications_manager.dart';
|
||||
|
@ -66,7 +66,6 @@ Future<void> dependencyInjectionInitialization() async {
|
|||
final objectBoxCache = await ObjectBoxCache.create();
|
||||
getIt.registerSingleton<ObjectBoxCache>(objectBoxCache);
|
||||
getIt.registerSingleton<IHashtagRepo>(ObjectBoxHashtagRepo());
|
||||
getIt.registerSingleton<HashtagService>(HashtagService());
|
||||
getIt.registerSingleton<NetworkStatusService>(NetworkStatusService());
|
||||
getIt.registerSingleton<FediverseServiceValidator>(
|
||||
FediverseServiceValidator());
|
||||
|
@ -196,7 +195,9 @@ void clearCaches() {
|
|||
_logger.severe('Error clearing GalleryService Repo: $error'),
|
||||
);
|
||||
|
||||
getIt<HashtagService>().clear();
|
||||
rootRiverpodProviderContainer
|
||||
?.read(hashtagServiceProvider().notifier)
|
||||
.clear();
|
||||
|
||||
getIt<ActiveProfileSelector<NotificationsManager>>().activeEntry.match(
|
||||
onSuccess: (manager) => manager.clear(),
|
||||
|
|
|
@ -2,6 +2,7 @@ import 'dart:io';
|
|||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
|
@ -32,6 +33,8 @@ const processingSleep = Duration(milliseconds: 1);
|
|||
const apiCallTimeout = Duration(seconds: 60);
|
||||
const oauthTimeout = Duration(seconds: 15);
|
||||
|
||||
ProviderContainer? rootRiverpodProviderContainer;
|
||||
|
||||
Future<bool?> showConfirmDialog(BuildContext context, String caption) {
|
||||
return showDialog<bool>(
|
||||
context: context,
|
||||
|
|
|
@ -19,7 +19,6 @@ import 'services/direct_message_service.dart';
|
|||
import 'services/entry_manager_service.dart';
|
||||
import 'services/follow_requests_manager.dart';
|
||||
import 'services/gallery_service.dart';
|
||||
import 'services/hashtag_service.dart';
|
||||
import 'services/interactions_manager.dart';
|
||||
import 'services/notifications_manager.dart';
|
||||
import 'services/setting_service.dart';
|
||||
|
@ -30,7 +29,6 @@ import 'utils/active_profile_selector.dart';
|
|||
import 'utils/app_scrolling_behavior.dart';
|
||||
import 'utils/old_android_letsencrypte_cert.dart';
|
||||
|
||||
fr.ProviderContainer? rootRiverpodProviderContainer;
|
||||
final preInitializationLogEventQueue = <LogRecord>[];
|
||||
|
||||
void main() async {
|
||||
|
@ -147,10 +145,6 @@ class _AppState extends fr.ConsumerState<App> {
|
|||
create: (_) => getIt<ActiveProfileSelector<GalleryService>>(),
|
||||
lazy: true,
|
||||
),
|
||||
ChangeNotifierProvider<HashtagService>(
|
||||
create: (_) => getIt<HashtagService>(),
|
||||
lazy: true,
|
||||
),
|
||||
ChangeNotifierProvider<ActiveProfileSelector<TimelineManager>>(
|
||||
create: (_) => getIt<ActiveProfileSelector<TimelineManager>>(),
|
||||
),
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
import 'package:relatica/data/interfaces/hashtag_repo_intf.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../data/objectbox/objectbox_hashtag_repo.dart';
|
||||
import '../models/hashtag.dart';
|
||||
|
||||
part 'hashtag_service.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
IHashtagRepo _hashtagRepo(_HashtagRepoRef ref) {
|
||||
return ObjectBoxHashtagRepo();
|
||||
}
|
||||
|
||||
@riverpod
|
||||
class HashtagService extends _$HashtagService {
|
||||
@override
|
||||
List<String> build({String searchString = ''}) {
|
||||
final repo = ref.watch(_hashtagRepoProvider);
|
||||
return repo.getMatchingHashTags(searchString);
|
||||
}
|
||||
|
||||
Future<void> clear() async {
|
||||
await ref.read(_hashtagRepoProvider).clear();
|
||||
ref.invalidateSelf();
|
||||
}
|
||||
|
||||
void add(Hashtag tag) {
|
||||
ref.read(_hashtagRepoProvider).add(tag);
|
||||
ref.invalidateSelf();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,189 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'hashtag_service.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$hashtagRepoHash() => r'8ee41e763536af28769739a977568f5fdcec7b41';
|
||||
|
||||
/// See also [_hashtagRepo].
|
||||
@ProviderFor(_hashtagRepo)
|
||||
final _hashtagRepoProvider = Provider<IHashtagRepo>.internal(
|
||||
_hashtagRepo,
|
||||
name: r'_hashtagRepoProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$hashtagRepoHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _HashtagRepoRef = ProviderRef<IHashtagRepo>;
|
||||
String _$hashtagServiceHash() => r'ca0480f1f691248f0dabf233f0e4d190bf3610fc';
|
||||
|
||||
/// Copied from Dart SDK
|
||||
class _SystemHash {
|
||||
_SystemHash._();
|
||||
|
||||
static int combine(int hash, int value) {
|
||||
// ignore: parameter_assignments
|
||||
hash = 0x1fffffff & (hash + value);
|
||||
// ignore: parameter_assignments
|
||||
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
|
||||
return hash ^ (hash >> 6);
|
||||
}
|
||||
|
||||
static int finish(int hash) {
|
||||
// ignore: parameter_assignments
|
||||
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
|
||||
// ignore: parameter_assignments
|
||||
hash = hash ^ (hash >> 11);
|
||||
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _$HashtagService
|
||||
extends BuildlessAutoDisposeNotifier<List<String>> {
|
||||
late final String searchString;
|
||||
|
||||
List<String> build({
|
||||
String searchString = '',
|
||||
});
|
||||
}
|
||||
|
||||
/// See also [HashtagService].
|
||||
@ProviderFor(HashtagService)
|
||||
const hashtagServiceProvider = HashtagServiceFamily();
|
||||
|
||||
/// See also [HashtagService].
|
||||
class HashtagServiceFamily extends Family<List<String>> {
|
||||
/// See also [HashtagService].
|
||||
const HashtagServiceFamily();
|
||||
|
||||
/// See also [HashtagService].
|
||||
HashtagServiceProvider call({
|
||||
String searchString = '',
|
||||
}) {
|
||||
return HashtagServiceProvider(
|
||||
searchString: searchString,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
HashtagServiceProvider getProviderOverride(
|
||||
covariant HashtagServiceProvider provider,
|
||||
) {
|
||||
return call(
|
||||
searchString: provider.searchString,
|
||||
);
|
||||
}
|
||||
|
||||
static const Iterable<ProviderOrFamily>? _dependencies = null;
|
||||
|
||||
@override
|
||||
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
|
||||
|
||||
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
|
||||
|
||||
@override
|
||||
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
|
||||
_allTransitiveDependencies;
|
||||
|
||||
@override
|
||||
String? get name => r'hashtagServiceProvider';
|
||||
}
|
||||
|
||||
/// See also [HashtagService].
|
||||
class HashtagServiceProvider
|
||||
extends AutoDisposeNotifierProviderImpl<HashtagService, List<String>> {
|
||||
/// See also [HashtagService].
|
||||
HashtagServiceProvider({
|
||||
String searchString = '',
|
||||
}) : this._internal(
|
||||
() => HashtagService()..searchString = searchString,
|
||||
from: hashtagServiceProvider,
|
||||
name: r'hashtagServiceProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$hashtagServiceHash,
|
||||
dependencies: HashtagServiceFamily._dependencies,
|
||||
allTransitiveDependencies:
|
||||
HashtagServiceFamily._allTransitiveDependencies,
|
||||
searchString: searchString,
|
||||
);
|
||||
|
||||
HashtagServiceProvider._internal(
|
||||
super._createNotifier, {
|
||||
required super.name,
|
||||
required super.dependencies,
|
||||
required super.allTransitiveDependencies,
|
||||
required super.debugGetCreateSourceHash,
|
||||
required super.from,
|
||||
required this.searchString,
|
||||
}) : super.internal();
|
||||
|
||||
final String searchString;
|
||||
|
||||
@override
|
||||
List<String> runNotifierBuild(
|
||||
covariant HashtagService notifier,
|
||||
) {
|
||||
return notifier.build(
|
||||
searchString: searchString,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Override overrideWith(HashtagService Function() create) {
|
||||
return ProviderOverride(
|
||||
origin: this,
|
||||
override: HashtagServiceProvider._internal(
|
||||
() => create()..searchString = searchString,
|
||||
from: from,
|
||||
name: null,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
debugGetCreateSourceHash: null,
|
||||
searchString: searchString,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
AutoDisposeNotifierProviderElement<HashtagService, List<String>>
|
||||
createElement() {
|
||||
return _HashtagServiceProviderElement(this);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is HashtagServiceProvider &&
|
||||
other.searchString == searchString;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
var hash = _SystemHash.combine(0, runtimeType.hashCode);
|
||||
hash = _SystemHash.combine(hash, searchString.hashCode);
|
||||
|
||||
return _SystemHash.finish(hash);
|
||||
}
|
||||
}
|
||||
|
||||
mixin HashtagServiceRef on AutoDisposeNotifierProviderRef<List<String>> {
|
||||
/// The parameter `searchString` of this provider.
|
||||
String get searchString;
|
||||
}
|
||||
|
||||
class _HashtagServiceProviderElement
|
||||
extends AutoDisposeNotifierProviderElement<HashtagService, List<String>>
|
||||
with HashtagServiceRef {
|
||||
_HashtagServiceProviderElement(super.provider);
|
||||
|
||||
@override
|
||||
String get searchString => (origin as HashtagServiceProvider).searchString;
|
||||
}
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
|
@ -7,9 +7,9 @@ import '../../models/link_data.dart';
|
|||
import '../../models/location_data.dart';
|
||||
import '../../models/timeline_entry.dart';
|
||||
import '../../models/visibility.dart';
|
||||
import '../../riverpod_controllers/hashtag_service.dart';
|
||||
import '../../services/auth_service.dart';
|
||||
import '../../services/connections_manager.dart';
|
||||
import '../../services/hashtag_service.dart';
|
||||
import '../../services/reshared_via_service.dart';
|
||||
import '../../utils/active_profile_selector.dart';
|
||||
import '../../utils/dateutils.dart';
|
||||
|
@ -133,11 +133,13 @@ extension TimelineEntryMastodonExtensions on TimelineEntry {
|
|||
final List<dynamic>? tagsJson = json['tags'];
|
||||
final tags = <String>[];
|
||||
if (tagsJson?.isNotEmpty ?? false) {
|
||||
final tagManager = getIt<HashtagService>();
|
||||
final tagManager = rootRiverpodProviderContainer
|
||||
?.read(hashtagServiceProvider().notifier);
|
||||
|
||||
for (final tagJson in tagsJson!) {
|
||||
final tag = HashtagMastodonExtensions.fromJson(tagJson);
|
||||
tags.add(tag.tag);
|
||||
tagManager.add(tag);
|
||||
tagManager?.add(tag);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../data/interfaces/hashtag_repo_intf.dart';
|
||||
import '../globals.dart';
|
||||
import '../models/hashtag.dart';
|
||||
|
||||
class HashtagService extends ChangeNotifier {
|
||||
late final IHashtagRepo repo;
|
||||
|
||||
HashtagService() {
|
||||
repo = getIt<IHashtagRepo>();
|
||||
}
|
||||
|
||||
Future<void> clear() async {
|
||||
await repo.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void add(Hashtag tag) {
|
||||
repo.add(tag);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
List<String> getMatchingHashTags(String searchString) {
|
||||
return repo.getMatchingHashTags(searchString);
|
||||
}
|
||||
}
|
Ładowanie…
Reference in New Issue