add sorting for convos
This commit is contained in:
parent
a73614b5cc
commit
d264fc4504
|
@ -5,7 +5,7 @@ import 'package:loris/business_logic/timeline/timeline.dart';
|
|||
import 'package:loris/global.dart' as global;
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class ConversationModel {
|
||||
class ConversationModel implements Comparable {
|
||||
final String id;
|
||||
final List<AccountModel> accounts;
|
||||
final bool unread;
|
||||
|
@ -35,22 +35,29 @@ class ConversationModel {
|
|||
unread: json["unread"],
|
||||
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 {
|
||||
// http status code
|
||||
final int statusCode;
|
||||
|
||||
// list of 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
|
||||
*/
|
||||
Future<ConversationModelResult> getConversationModels(
|
||||
Future<ConversationModelResult> _getConversationModels(
|
||||
String identityName,
|
||||
String? maxId,
|
||||
) async {
|
||||
|
@ -73,14 +80,35 @@ Future<ConversationModelResult> getConversationModels(
|
|||
|
||||
final result = await http.get(uri, headers: headers);
|
||||
if (result.statusCode != 200) {
|
||||
return ConversationModelResult(result.statusCode, []);
|
||||
return ConversationModelResult([]);
|
||||
}
|
||||
|
||||
return ConversationModelResult(
|
||||
result.statusCode,
|
||||
jsonDecode(result.body)
|
||||
.map<ConversationModel>(
|
||||
(e) => ConversationModel.fromJson(e, identityName),
|
||||
)
|
||||
.toList());
|
||||
return ConversationModelResult(jsonDecode(result.body)
|
||||
.map<ConversationModel>(
|
||||
(e) => ConversationModel.fromJson(e, identityName),
|
||||
)
|
||||
.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);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:localization/localization.dart';
|
||||
import 'package:loris/business_logic/chat/chat.dart';
|
||||
import 'package:loris/themes/themes.dart' as themes;
|
||||
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<String, String?> maxIds = {};
|
||||
|
||||
Future<void> fetchForIdentity(String identityName) async {
|
||||
final c = await getConversationModels(identityName, maxIds[identityName]);
|
||||
Future<void> updateConversations() async {
|
||||
final models = await getAllConversationModels(maxIds);
|
||||
print(models.maxIds);
|
||||
setState(() {
|
||||
conversations.addAll(c.models);
|
||||
});
|
||||
}
|
||||
|
||||
void updateConversations() {
|
||||
global.settings!.identities.forEach((key, value) {
|
||||
fetchForIdentity(key);
|
||||
conversations.addAll(models.models);
|
||||
maxIds = models.maxIds;
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -80,7 +77,9 @@ class ConversationButton extends StatelessWidget {
|
|||
child: Column(
|
||||
children: [
|
||||
Wrap(
|
||||
children: [SelectableText(model.identity)],
|
||||
children: [
|
||||
SelectableText("${"you-are".i18n()} ${model.identity}")
|
||||
],
|
||||
)
|
||||
],
|
||||
),
|
||||
|
|
Loading…
Reference in New Issue