✨ ActivityPub service impl basis
This commit is contained in:
2
lib/screens/activitypub/activitypub.dart
Normal file
2
lib/screens/activitypub/activitypub.dart
Normal file
@@ -0,0 +1,2 @@
|
||||
export 'list.dart';
|
||||
export 'search.dart';
|
||||
151
lib/screens/activitypub/list.dart
Normal file
151
lib/screens/activitypub/list.dart
Normal file
@@ -0,0 +1,151 @@
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/models/activitypub.dart';
|
||||
import 'package:island/services/activitypub_service.dart';
|
||||
import 'package:island/widgets/activitypub/user_list_item.dart';
|
||||
import 'package:island/widgets/alert.dart';
|
||||
import 'package:island/widgets/app_scaffold.dart';
|
||||
import 'package:island/widgets/extended_refresh_indicator.dart';
|
||||
import 'package:material_symbols_icons/symbols.dart';
|
||||
|
||||
enum ActivityPubListType { following, followers }
|
||||
|
||||
class ApListScreen extends HookConsumerWidget {
|
||||
final ActivityPubListType type;
|
||||
|
||||
const ApListScreen({super.key, required this.type});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final users = useState<List<SnActivityPubUser>>([]);
|
||||
final isLoading = useState(true);
|
||||
final followingUris = useState<Set<String>>({});
|
||||
final isLoadingAction = useState<String?>(null);
|
||||
|
||||
Future<void> loadUsers() async {
|
||||
isLoading.value = true;
|
||||
try {
|
||||
final service = ref.read(activityPubServiceProvider);
|
||||
final result = type == ActivityPubListType.following
|
||||
? await service.getFollowing()
|
||||
: await service.getFollowers();
|
||||
users.value = result;
|
||||
followingUris.value = result.map((user) => user.actorUri).toSet();
|
||||
} catch (err) {
|
||||
showErrorAlert(err);
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> handleFollow(SnActivityPubUser user) async {
|
||||
isLoadingAction.value = user.actorUri;
|
||||
try {
|
||||
final service = ref.read(activityPubServiceProvider);
|
||||
await service.followRemoteUser(user.actorUri);
|
||||
followingUris.value = {...followingUris.value, user.actorUri};
|
||||
showSnackBar('followedUser'.tr(args: ['@${user.username}']));
|
||||
} catch (err) {
|
||||
showErrorAlert(err);
|
||||
} finally {
|
||||
isLoadingAction.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> handleUnfollow(SnActivityPubUser user) async {
|
||||
isLoadingAction.value = user.actorUri;
|
||||
try {
|
||||
final service = ref.read(activityPubServiceProvider);
|
||||
await service.unfollowRemoteUser(user.actorUri);
|
||||
followingUris.value = followingUris.value
|
||||
.where((uri) => uri != user.actorUri)
|
||||
.toSet();
|
||||
if (type == ActivityPubListType.following) {
|
||||
users.value = users.value
|
||||
.where((u) => u.actorUri != user.actorUri)
|
||||
.toList();
|
||||
}
|
||||
showSnackBar('unfollowedUser'.tr(args: ['@${user.username}']));
|
||||
} catch (err) {
|
||||
showErrorAlert(err);
|
||||
} finally {
|
||||
isLoadingAction.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
final title = type == ActivityPubListType.following
|
||||
? 'following'.tr()
|
||||
: 'followers'.tr();
|
||||
|
||||
return AppScaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(title),
|
||||
elevation: 0,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: loadUsers,
|
||||
tooltip: 'refresh'.tr(),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: isLoading.value
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: users.value.isEmpty
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Symbols.group,
|
||||
size: 64,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
type == ActivityPubListType.following
|
||||
? 'followingEmpty'.tr()
|
||||
: 'followersEmpty'.tr(),
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'followingEmptyHint'.tr(),
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: ExtendedRefreshIndicator(
|
||||
onRefresh: loadUsers,
|
||||
child: ListView.separated(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
itemCount: users.value.length,
|
||||
separatorBuilder: (context, index) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final user = users.value[index];
|
||||
final isFollowing = followingUris.value.contains(
|
||||
user.actorUri,
|
||||
);
|
||||
final isLoadingUser = isLoadingAction.value == user.actorUri;
|
||||
return ActivityPubUserListItem(
|
||||
user: user,
|
||||
isFollowing: isFollowing,
|
||||
isLoading: isLoadingUser,
|
||||
onFollow: type == ActivityPubListType.followers
|
||||
? () => handleFollow(user)
|
||||
: null,
|
||||
onUnfollow: type == ActivityPubListType.following
|
||||
? () => handleUnfollow(user)
|
||||
: null,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
174
lib/screens/activitypub/search.dart
Normal file
174
lib/screens/activitypub/search.dart
Normal file
@@ -0,0 +1,174 @@
|
||||
import 'dart:async';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/models/activitypub.dart';
|
||||
import 'package:island/services/activitypub_service.dart';
|
||||
import 'package:island/widgets/activitypub/actor_list_item.dart';
|
||||
import 'package:island/widgets/alert.dart';
|
||||
import 'package:island/widgets/app_scaffold.dart';
|
||||
import 'package:island/widgets/extended_refresh_indicator.dart';
|
||||
import 'package:material_symbols_icons/symbols.dart';
|
||||
import 'package:styled_widget/styled_widget.dart';
|
||||
|
||||
class ApSearchScreen extends HookConsumerWidget {
|
||||
const ApSearchScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final searchController = useTextEditingController();
|
||||
final debounce = useMemoized(() => const Duration(milliseconds: 500));
|
||||
final debounceTimer = useRef<Timer?>(null);
|
||||
final searchResults = useState<List<SnActivityPubActor>>([]);
|
||||
final isSearching = useState(false);
|
||||
final followingUris = useState<Set<String>>({});
|
||||
|
||||
useEffect(() {
|
||||
return () {
|
||||
searchController.dispose();
|
||||
debounceTimer.value?.cancel();
|
||||
};
|
||||
}, []);
|
||||
|
||||
Future<void> performSearch(String query) async {
|
||||
if (query.trim().isEmpty) {
|
||||
searchResults.value = [];
|
||||
return;
|
||||
}
|
||||
|
||||
isSearching.value = true;
|
||||
try {
|
||||
final service = ref.read(activityPubServiceProvider);
|
||||
final results = await service.searchUsers(query);
|
||||
searchResults.value = results;
|
||||
} catch (err) {
|
||||
showErrorAlert(err);
|
||||
} finally {
|
||||
isSearching.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
void onSearchChanged(String query) {
|
||||
if (debounceTimer.value?.isActive ?? false) {
|
||||
debounceTimer.value!.cancel();
|
||||
}
|
||||
debounceTimer.value = Timer(debounce, () {
|
||||
performSearch(query);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> handleFollow(SnActivityPubActor actor) async {
|
||||
try {
|
||||
followingUris.value = {...followingUris.value, actor.id};
|
||||
final service = ref.read(activityPubServiceProvider);
|
||||
await service.followRemoteUser(actor.id);
|
||||
showSnackBar(
|
||||
'followedUser'.tr(
|
||||
args: [
|
||||
'@${actor.username?.isNotEmpty ?? false ? actor.username : actor.displayName}',
|
||||
],
|
||||
),
|
||||
);
|
||||
} catch (err) {
|
||||
showErrorAlert(err);
|
||||
followingUris.value = followingUris.value
|
||||
.where((uri) => uri != actor.id)
|
||||
.toSet();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> handleUnfollow(SnActivityPubActor actor) async {
|
||||
try {
|
||||
followingUris.value = followingUris.value
|
||||
.where((uri) => uri != actor.id)
|
||||
.toSet();
|
||||
final service = ref.read(activityPubServiceProvider);
|
||||
await service.unfollowRemoteUser(actor.id);
|
||||
showSnackBar(
|
||||
'unfollowedUser'.tr(
|
||||
args: [
|
||||
'@${actor.username?.isNotEmpty ?? false ? actor.username : actor.displayName}',
|
||||
],
|
||||
),
|
||||
);
|
||||
} catch (err) {
|
||||
showErrorAlert(err);
|
||||
followingUris.value = {...followingUris.value, actor.id};
|
||||
}
|
||||
}
|
||||
|
||||
return AppScaffold(
|
||||
appBar: AppBar(title: Text('searchFediverse'.tr()), elevation: 0),
|
||||
body: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: SearchBar(
|
||||
controller: searchController,
|
||||
hintText: 'searchFediverseHint'.tr(
|
||||
args: ['@username@instance.com'],
|
||||
),
|
||||
leading: const Icon(Symbols.search).padding(horizontal: 24),
|
||||
onChanged: onSearchChanged,
|
||||
onSubmitted: (value) {
|
||||
onSearchChanged(value);
|
||||
performSearch(value);
|
||||
},
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: isSearching.value
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: searchResults.value.isEmpty
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Symbols.search,
|
||||
size: 64,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (searchController.text.isEmpty)
|
||||
Text(
|
||||
'searchFediverseEmpty'.tr(),
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
)
|
||||
else
|
||||
Text(
|
||||
'searchFediverseNoResults'.tr(),
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: ExtendedRefreshIndicator(
|
||||
onRefresh: () => performSearch(searchController.text),
|
||||
child: ListView.separated(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
itemCount: searchResults.value.length,
|
||||
separatorBuilder: (context, index) =>
|
||||
const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final actor = searchResults.value[index];
|
||||
final isFollowing = followingUris.value.contains(
|
||||
actor.id,
|
||||
);
|
||||
return ApActorListItem(
|
||||
actor: actor,
|
||||
isFollowing: isFollowing,
|
||||
isLoading: false,
|
||||
onFollow: () => handleFollow(actor),
|
||||
onUnfollow: () => handleUnfollow(actor),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -138,6 +138,18 @@ class ExploreScreen extends HookConsumerWidget {
|
||||
),
|
||||
PopupMenuButton(
|
||||
itemBuilder: (context) => [
|
||||
PopupMenuItem(
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Symbols.linked_services),
|
||||
const Gap(12),
|
||||
Text('searchFediverse').tr(),
|
||||
],
|
||||
),
|
||||
onTap: () {
|
||||
context.pushNamed('activitypubSearch');
|
||||
},
|
||||
),
|
||||
PopupMenuItem(
|
||||
child: Row(
|
||||
children: [
|
||||
|
||||
Reference in New Issue
Block a user