88 lines
2.9 KiB
Dart
88 lines
2.9 KiB
Dart
|
|
import 'package:async/async.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../api/errors/error_mapper.dart';
|
|
import '../../../api/marianumcloud/autocomplete/autocompleteApi.dart';
|
|
import '../../../api/marianumcloud/autocomplete/autocompleteResponse.dart';
|
|
import '../../../model/endpoint_data.dart';
|
|
import '../../../widget/app_progress_indicator.dart';
|
|
import '../../../widget/placeholder_view.dart';
|
|
|
|
class JoinChat extends SearchDelegate<String> {
|
|
CancelableOperation<AutocompleteResponse>? future;
|
|
|
|
@override
|
|
List<Widget>? buildActions(BuildContext context) => [
|
|
if(future != null && query.isNotEmpty) FutureBuilder(
|
|
future: future!.value,
|
|
builder: (context, snapshot) {
|
|
if(snapshot.connectionState != ConnectionState.done) {
|
|
return const Padding(
|
|
padding: EdgeInsets.all(10),
|
|
child: Center(child: AppProgressIndicator.medium()),
|
|
);
|
|
}
|
|
return const SizedBox.shrink();
|
|
},
|
|
),
|
|
if(query.isNotEmpty) IconButton(onPressed: () => query = '', icon: const Icon(Icons.delete)),
|
|
];
|
|
|
|
@override
|
|
Widget? buildLeading(BuildContext context) => null;
|
|
|
|
@override
|
|
Widget buildResults(BuildContext context) {
|
|
if(future != null) future!.cancel();
|
|
|
|
if(query.isEmpty) {
|
|
return const PlaceholderView(
|
|
text: 'Suche nach benutzern',
|
|
icon: Icons.person_search_outlined,
|
|
);
|
|
}
|
|
|
|
future = CancelableOperation.fromFuture(AutocompleteApi().find(query));
|
|
return FutureBuilder<AutocompleteResponse>(
|
|
future: future!.value,
|
|
builder: (context, snapshot) {
|
|
if(snapshot.hasData) {
|
|
return ListView.builder(
|
|
itemCount: snapshot.data!.data.length,
|
|
itemBuilder: (context, index) {
|
|
var object = snapshot.data!.data[index];
|
|
var circleAvatar = CircleAvatar(
|
|
foregroundImage: Image.network('https://${EndpointData().nextcloud().full()}/avatar/${object.id}/128').image,
|
|
backgroundColor: Theme.of(context).primaryColor,
|
|
foregroundColor: Colors.white,
|
|
child: const Icon(Icons.person),
|
|
);
|
|
return ListTile(
|
|
leading: circleAvatar,
|
|
title: Text(object.label),
|
|
subtitle: Text(object.id),
|
|
trailing: const Icon(Icons.arrow_right),
|
|
onTap: () {
|
|
close(context, object.id);
|
|
},
|
|
);
|
|
}
|
|
);
|
|
} else if(snapshot.hasError) {
|
|
return PlaceholderView(
|
|
icon: Icons.search_off,
|
|
text: errorToUserMessage(snapshot.error),
|
|
);
|
|
}
|
|
|
|
return const Center(child: AppProgressIndicator.large());
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget buildSuggestions(BuildContext context) => buildResults(context);
|
|
|
|
}
|