setup method
Loads conversations.
Should be called in initState.
Implementation
Future<void> setup() async {
// Reversing to show the latest threads first on the UI
var ids = (await getSavedThreadIds()).reversed;
_conversationIds = ids.toList();
if (ids.isEmpty) {
debugPrint('No conversations save found.');
}
conversations.addAll(ids.map((x) => Conversation(id: x)));
var titles = await getThreadsTitle(ids);
for (var conversation in conversations) {
var id = conversation.id!;
var title = titles[id];
if (title == null) continue;
conversation.title = title;
}
notifyListeners();
try {
var stream = streamAll();
stream.listen((conversation) {
debugPrint('Loaded conversations ${conversation.map((x) => x.id)}');
}, onDone: () {
_sort();
});
// for (var id in ids) {
// _loadConversation(
// id,
// titles: titles,
// );
// }
} on Exception catch (e) {
Widget adaptiveAction(
{required BuildContext context,
required VoidCallback onPressed,
required Widget child}) {
ThemeData theme = Theme.of(context);
switch (theme.platform) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
return TextButton(onPressed: onPressed, child: child);
case TargetPlatform.iOS:
case TargetPlatform.macOS:
return CupertinoDialogAction(onPressed: onPressed, child: child);
}
}
await showAdaptiveDialog(
context: context,
builder: (context) {
return AlertDialog.adaptive(
title: const Text('Falha ao carregar conversas'),
content: Column(
children: [
const Text(
'Verifique sua conexão com a internet e certifique-se que a token de API está válida.'),
Text('Logs:${getErrorMessage(e) ?? '...'}'),
],
),
actions: [
adaptiveAction(
context: context,
onPressed: () => Navigator.pop(context, 'OK'),
child: const Text('Ok'),
),
],
);
},
);
} finally {
_sort();
}
}