-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #407 from poppingmoon/server-list-filter
サーバー一覧を名前やurlで絞り込めるように
- Loading branch information
Showing
5 changed files
with
190 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
lib/state_notifier/common/misskey_server_list_notifier.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'package:collection/collection.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:misskey_dart/misskey_dart.dart'; | ||
|
||
final _queryProvider = StateProvider.autoDispose((ref) { | ||
return ""; | ||
}); | ||
|
||
final _instanceInfosProvider = AsyncNotifierProvider.autoDispose<_InstanceInfos, | ||
List<JoinMisskeyInstanceInfo>>( | ||
_InstanceInfos.new, | ||
); | ||
|
||
class _InstanceInfos | ||
extends AutoDisposeAsyncNotifier<List<JoinMisskeyInstanceInfo>> { | ||
@override | ||
Future<List<JoinMisskeyInstanceInfo>> build() async { | ||
final response = | ||
await JoinMisskey(host: "instanceapp.misskey.page").instances(); | ||
return response.instancesInfos | ||
.sortedByCompare( | ||
(info) => info.nodeInfo?.usage?.users?.total ?? 0, | ||
(a, b) => a.compareTo(b), | ||
) | ||
.reversed | ||
.toList(); | ||
} | ||
} | ||
|
||
class MisskeyServerListNotifier | ||
extends AutoDisposeAsyncNotifier<List<JoinMisskeyInstanceInfo>> { | ||
@override | ||
Future<List<JoinMisskeyInstanceInfo>> build() async { | ||
final query = ref.watch(_queryProvider); | ||
final instances = await ref.watch(_instanceInfosProvider.future); | ||
if (query.isEmpty) { | ||
return instances; | ||
} | ||
final filtered = instances.where( | ||
(e) => e.name.toLowerCase().contains(query) || e.url.contains(query), | ||
); | ||
final grouped = filtered.groupListsBy( | ||
(e) => e.name.toLowerCase().startsWith(query) || e.url.startsWith(query), | ||
); | ||
return [...grouped[true] ?? [], ...grouped[false] ?? []]; | ||
} | ||
|
||
void setQuery(String query) { | ||
ref.read(_queryProvider.notifier).state = query.trim().toLowerCase(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:miria/providers.dart'; | ||
import 'package:miria/view/common/constants.dart'; | ||
import 'package:miria/view/common/error_detail.dart'; | ||
import 'package:misskey_dart/misskey_dart.dart'; | ||
|
||
class MisskeyServerList extends ConsumerWidget { | ||
final bool isDisableUnloginable; | ||
|
||
final void Function(JoinMisskeyInstanceInfo) onTap; | ||
|
||
const MisskeyServerList({ | ||
super.key, | ||
this.isDisableUnloginable = false, | ||
required this.onTap, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context, WidgetRef ref) { | ||
final servers = ref.watch(misskeyServerListNotifierProvider); | ||
return ConstrainedBox( | ||
constraints: const BoxConstraints(maxWidth: 800), | ||
child: Column( | ||
children: [ | ||
Padding( | ||
padding: const EdgeInsets.symmetric(vertical: 10), | ||
child: TextField( | ||
decoration: const InputDecoration( | ||
prefixIcon: Icon(Icons.search), | ||
), | ||
onChanged: | ||
ref.read(misskeyServerListNotifierProvider.notifier).setQuery, | ||
), | ||
), | ||
Expanded( | ||
child: servers.when( | ||
skipLoadingOnReload: true, | ||
data: (servers) => ListView.builder( | ||
itemCount: servers.length, | ||
itemBuilder: (context, index) { | ||
final server = servers[index]; | ||
final description = | ||
server.description?.replaceAll(htmlTagRemove, "") ?? ""; | ||
final available = !isDisableUnloginable || | ||
server.nodeInfo?.software?.name == "misskey" && | ||
availableServerVersion | ||
.allMatches( | ||
server.nodeInfo?.software?.version ?? "", | ||
) | ||
.isNotEmpty; | ||
return Padding( | ||
key: ValueKey(server.url), | ||
padding: const EdgeInsets.only(bottom: 10), | ||
child: InkWell( | ||
onTap: available ? () => onTap.call(server) : null, | ||
child: Container( | ||
padding: const EdgeInsets.all(10), | ||
decoration: BoxDecoration( | ||
color: available ? null : Colors.grey.withAlpha(160), | ||
border: | ||
Border.all(color: Theme.of(context).dividerColor), | ||
), | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
Row( | ||
children: [ | ||
if (server.icon) | ||
Padding( | ||
padding: const EdgeInsets.only(right: 10), | ||
child: SizedBox( | ||
width: (Theme.of(context) | ||
.textTheme | ||
.bodyMedium | ||
?.fontSize ?? | ||
22) * | ||
MediaQuery.of(context) | ||
.textScaleFactor * | ||
2, | ||
child: Image.network( | ||
"https://instanceapp.misskey.page/instance-icons/${server.url}.webp", | ||
), | ||
), | ||
), | ||
Expanded(child: Text(server.name)), | ||
], | ||
), | ||
Text( | ||
description, | ||
style: Theme.of(context).textTheme.bodySmall, | ||
), | ||
const Padding(padding: EdgeInsets.only(top: 10)), | ||
Text( | ||
"${server.nodeInfo?.usage?.users?.total.format()}人が参加中", | ||
style: Theme.of(context).textTheme.bodySmall, | ||
), | ||
const Padding(padding: EdgeInsets.only(top: 10)), | ||
Align( | ||
alignment: Alignment.centerRight, | ||
child: Text( | ||
"${server.nodeInfo?.software?.name ?? ""} ${server.nodeInfo?.software?.version}", | ||
style: Theme.of(context).textTheme.bodySmall, | ||
textAlign: TextAlign.right, | ||
), | ||
), | ||
if (!available) | ||
Align( | ||
alignment: Alignment.centerRight, | ||
child: Text( | ||
"非対応のサーバーです", | ||
style: Theme.of(context).textTheme.bodySmall, | ||
textAlign: TextAlign.right, | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
}, | ||
), | ||
error: (e, st) => ErrorDetail(error: e, stackTrace: st), | ||
loading: () => const Center(child: CircularProgressIndicator()), | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters