add sorting for convos

This commit is contained in:
zoe 2022-09-28 00:00:12 +02:00
parent a73614b5cc
commit d264fc4504
2 changed files with 51 additions and 24 deletions

View File

@ -5,7 +5,7 @@ import 'package:loris/business_logic/timeline/timeline.dart';
import 'package:loris/global.dart' as global; import 'package:loris/global.dart' as global;
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
class ConversationModel { class ConversationModel implements Comparable {
final String id; final String id;
final List<AccountModel> accounts; final List<AccountModel> accounts;
final bool unread; final bool unread;
@ -35,22 +35,29 @@ class ConversationModel {
unread: json["unread"], unread: json["unread"],
lastStatus: PostModel.fromJson(json["last_status"], identity)); lastStatus: PostModel.fromJson(json["last_status"], identity));
} }
@override
int compareTo(other) {
if (lastStatus == null && other.lastStatus == null) return 0;
;
if (lastStatus == null) return -1;
return lastStatus!.createdAt.compareTo(other.lastStatus!.createdAt);
}
} }
class ConversationModelResult { class ConversationModelResult {
// http status code
final int statusCode;
// list of models // list of models
final List<ConversationModel> models; final List<ConversationModel> models;
ConversationModelResult(this.statusCode, this.models); final Map<String, String?> maxIds;
ConversationModelResult(this.models, {this.maxIds = const {}});
} }
/* /*
loads conversation models from timeline loads conversation models from timeline
*/ */
Future<ConversationModelResult> getConversationModels( Future<ConversationModelResult> _getConversationModels(
String identityName, String identityName,
String? maxId, String? maxId,
) async { ) async {
@ -73,14 +80,35 @@ Future<ConversationModelResult> getConversationModels(
final result = await http.get(uri, headers: headers); final result = await http.get(uri, headers: headers);
if (result.statusCode != 200) { if (result.statusCode != 200) {
return ConversationModelResult(result.statusCode, []); return ConversationModelResult([]);
} }
return ConversationModelResult( return ConversationModelResult(jsonDecode(result.body)
result.statusCode, .map<ConversationModel>(
jsonDecode(result.body) (e) => ConversationModel.fromJson(e, identityName),
.map<ConversationModel>( )
(e) => ConversationModel.fromJson(e, identityName), .toList());
) }
.toList());
Future<ConversationModelResult> getAllConversationModels(
Map<String, String?> maxIds) async {
List<Future> futureResults = [];
global.settings!.identities.forEach((key, value) {
futureResults.add(_getConversationModels(key, maxIds[key]));
});
List<ConversationModel> models = [];
for (var element in futureResults) {
models.addAll((await element).models);
}
models.sort();
models = models.reversed.toList().sublist(0, global.settings!.batchSize);
Map<String, String?> newMaxIds = {};
for (var element in models) {
if (newMaxIds[element.identity] == null) {
newMaxIds.addAll({element.identity: element.id});
} else if (element.id.compareTo(newMaxIds[element.identity]!) < 0) {
newMaxIds.addAll({element.identity: element.id});
}
}
return ConversationModelResult(models, maxIds: newMaxIds);
} }

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:localization/localization.dart';
import 'package:loris/business_logic/chat/chat.dart'; import 'package:loris/business_logic/chat/chat.dart';
import 'package:loris/themes/themes.dart' as themes; import 'package:loris/themes/themes.dart' as themes;
import 'package:loris/global.dart' as global; import 'package:loris/global.dart' as global;
@ -15,16 +16,12 @@ class _ChatState extends State<Chat> {
// map that stores max ids for each identity // map that stores max ids for each identity
Map<String, String?> maxIds = {}; Map<String, String?> maxIds = {};
Future<void> fetchForIdentity(String identityName) async { Future<void> updateConversations() async {
final c = await getConversationModels(identityName, maxIds[identityName]); final models = await getAllConversationModels(maxIds);
print(models.maxIds);
setState(() { setState(() {
conversations.addAll(c.models); conversations.addAll(models.models);
}); maxIds = models.maxIds;
}
void updateConversations() {
global.settings!.identities.forEach((key, value) {
fetchForIdentity(key);
}); });
} }
@ -80,7 +77,9 @@ class ConversationButton extends StatelessWidget {
child: Column( child: Column(
children: [ children: [
Wrap( Wrap(
children: [SelectableText(model.identity)], children: [
SelectableText("${"you-are".i18n()} ${model.identity}")
],
) )
], ],
), ),