mirror of
https://github.com/twonlyapp/twonly-app.git
synced 2026-01-15 13:08:42 +00:00
fixes flame counter
This commit is contained in:
parent
c563a09f43
commit
909b482af7
8 changed files with 91 additions and 127 deletions
|
|
@ -44,9 +44,9 @@ int getFlameCounterFromContact(Contact contact) {
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
final startOfToday = DateTime(now.year, now.month, now.day);
|
final startOfToday = DateTime(now.year, now.month, now.day);
|
||||||
final twoDaysAgo = startOfToday.subtract(Duration(days: 2));
|
final twoDaysAgo = startOfToday.subtract(Duration(days: 2));
|
||||||
if (contact.lastMessageSend!.isBefore(twoDaysAgo) &&
|
if (contact.lastMessageSend!.isAfter(twoDaysAgo) &&
|
||||||
contact.lastMessageReceived!.isBefore(twoDaysAgo)) {
|
contact.lastMessageReceived!.isAfter(twoDaysAgo)) {
|
||||||
return contact.flameCounter;
|
return contact.flameCounter + 1;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -135,17 +135,79 @@ class TwonlyDatabase extends _$TwonlyDatabase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future incTotalMediaCounter(int contactId) async {
|
Future incFlameCounter(
|
||||||
return (update(contacts)..where((t) => t.userId.equals(contactId)))
|
int contactId, bool received, DateTime timestamp) async {
|
||||||
.write(ContactsCompanion(
|
Contact contact = (await (select(contacts)
|
||||||
totalMediaCounter: Value(
|
..where((t) => t.userId.equals(contactId)))
|
||||||
(await (select(contacts)..where((t) => t.userId.equals(contactId)))
|
.get())
|
||||||
.get())
|
.first;
|
||||||
.first
|
|
||||||
.totalMediaCounter +
|
int totalMediaCounter = contact.totalMediaCounter + 1;
|
||||||
1,
|
int flameCounter = contact.flameCounter;
|
||||||
|
|
||||||
|
if (contact.lastMessageReceived != null &&
|
||||||
|
contact.lastMessageSend != null) {
|
||||||
|
final now = DateTime.now();
|
||||||
|
final startOfToday = DateTime(now.year, now.month, now.day);
|
||||||
|
final twoDaysAgo = startOfToday.subtract(Duration(days: 2));
|
||||||
|
if (contact.lastMessageSend!.isBefore(twoDaysAgo) ||
|
||||||
|
contact.lastMessageReceived!.isBefore(twoDaysAgo)) {
|
||||||
|
flameCounter = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Value<DateTime?> lastMessageSend = Value.absent();
|
||||||
|
Value<DateTime?> lastMessageReceived = Value.absent();
|
||||||
|
Value<DateTime?> lastMessage = Value.absent();
|
||||||
|
|
||||||
|
if (contact.lastMessage != null) {
|
||||||
|
final now = DateTime.now();
|
||||||
|
final startOfToday = DateTime(now.year, now.month, now.day);
|
||||||
|
|
||||||
|
if (contact.lastMessage!.isBefore(startOfToday)) {
|
||||||
|
// last flame update was yesterday. check if it can be updated.
|
||||||
|
bool updateFlame = false;
|
||||||
|
if (received) {
|
||||||
|
if (contact.lastMessageSend!.isAfter(startOfToday)) {
|
||||||
|
// today a message was already send -> update flame
|
||||||
|
updateFlame = true;
|
||||||
|
}
|
||||||
|
} else if (contact.lastMessageReceived!.isAfter(startOfToday)) {
|
||||||
|
// today a message was already received -> update flame
|
||||||
|
updateFlame = true;
|
||||||
|
}
|
||||||
|
if (updateFlame) {
|
||||||
|
flameCounter += 1;
|
||||||
|
lastMessage = Value(timestamp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// There where no message until no...
|
||||||
|
lastMessage = Value(timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (received) {
|
||||||
|
lastMessageReceived = Value(timestamp);
|
||||||
|
} else {
|
||||||
|
lastMessageSend = Value(timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (update(contacts)..where((t) => t.userId.equals(contactId))).write(
|
||||||
|
ContactsCompanion(
|
||||||
|
totalMediaCounter: Value(totalMediaCounter),
|
||||||
|
lastMessage: lastMessage,
|
||||||
|
lastMessageReceived: lastMessageReceived,
|
||||||
|
lastMessageSend: lastMessageSend,
|
||||||
|
flameCounter: Value(flameCounter),
|
||||||
),
|
),
|
||||||
));
|
);
|
||||||
|
|
||||||
|
// twonlyDatabase.updateContact(
|
||||||
|
// fromUserId,
|
||||||
|
// ContactsCompanion(
|
||||||
|
// lastMessageReceived: Value(message.timestamp),
|
||||||
|
// ),
|
||||||
|
// );
|
||||||
}
|
}
|
||||||
|
|
||||||
SingleOrNullSelectable<Contact> getContactByUserId(int userId) {
|
SingleOrNullSelectable<Contact> getContactByUserId(int userId) {
|
||||||
|
|
@ -183,7 +245,7 @@ class TwonlyDatabase extends _$TwonlyDatabase {
|
||||||
Stream<List<Contact>> watchContactsForChatList() {
|
Stream<List<Contact>> watchContactsForChatList() {
|
||||||
return (select(contacts)
|
return (select(contacts)
|
||||||
..where((t) => t.accepted.equals(true) & t.blocked.equals(false))
|
..where((t) => t.accepted.equals(true) & t.blocked.equals(false))
|
||||||
..orderBy([(t) => OrderingTerm.asc(t.lastMessage)]))
|
..orderBy([(t) => OrderingTerm.desc(t.lastMessage)]))
|
||||||
.watch();
|
.watch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -163,12 +163,10 @@ class ImageUploader {
|
||||||
|
|
||||||
final downloadToken = uploadState.downloadTokens.removeLast();
|
final downloadToken = uploadState.downloadTokens.removeLast();
|
||||||
|
|
||||||
twonlyDatabase.incTotalMediaCounter(targetUserId);
|
twonlyDatabase.incFlameCounter(
|
||||||
twonlyDatabase.updateContact(
|
|
||||||
targetUserId,
|
targetUserId,
|
||||||
ContactsCompanion(
|
false,
|
||||||
lastMessageReceived: Value(metadata.messageSendAt),
|
metadata.messageSendAt,
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Ensures the retransmit of the message
|
// Ensures the retransmit of the message
|
||||||
|
|
|
||||||
|
|
@ -223,7 +223,7 @@ Future<client.Response> handleNewMessage(int fromUserId, Uint8List body) async {
|
||||||
downloadState: Value(message.kind == MessageKind.media
|
downloadState: Value(message.kind == MessageKind.media
|
||||||
? DownloadState.pending
|
? DownloadState.pending
|
||||||
: DownloadState.downloaded),
|
: DownloadState.downloaded),
|
||||||
sendAt: Value(message.timestamp.toUtc()),
|
sendAt: Value(message.timestamp),
|
||||||
);
|
);
|
||||||
|
|
||||||
final messageId = await twonlyDatabase.insertMessage(
|
final messageId = await twonlyDatabase.insertMessage(
|
||||||
|
|
@ -246,15 +246,12 @@ Future<client.Response> handleNewMessage(int fromUserId, Uint8List body) async {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (message.kind == MessageKind.media) {
|
if (message.kind == MessageKind.media) {
|
||||||
twonlyDatabase.updateContact(
|
twonlyDatabase.incFlameCounter(
|
||||||
fromUserId,
|
fromUserId,
|
||||||
ContactsCompanion(
|
true,
|
||||||
lastMessageReceived: Value(message.timestamp),
|
message.timestamp,
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
twonlyDatabase.incTotalMediaCounter(fromUserId);
|
|
||||||
|
|
||||||
if (!globalIsAppInBackground) {
|
if (!globalIsAppInBackground) {
|
||||||
final content = message.content;
|
final content = message.content;
|
||||||
if (content is MediaMessageContent) {
|
if (content is MediaMessageContent) {
|
||||||
|
|
|
||||||
|
|
@ -162,62 +162,6 @@ Future<Uint8List?> getCompressedImage(Uint8List imageBytes) async {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// int getFlameCounter(List<DateTime> dates) {
|
|
||||||
// if (dates.isEmpty) return 0;
|
|
||||||
|
|
||||||
// int flamesCount = 0;
|
|
||||||
// DateTime lastFlameCount = DateTime.now();
|
|
||||||
|
|
||||||
// if (calculateTimeDifference(dates[0], lastFlameCount).inDays == 0) {
|
|
||||||
// flamesCount = 1;
|
|
||||||
// lastFlameCount = dates[0];
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // print(dates[0]);
|
|
||||||
// for (int i = 1; i < dates.length; i++) {
|
|
||||||
// // print(
|
|
||||||
// // "${dates[i]} ${dates[i].difference(dates[i - 1]).inDays} ${dates[i].difference(lastFlameCount).inDays}");
|
|
||||||
// if (calculateTimeDifference(dates[i], dates[i - 1]).inDays == 0) {
|
|
||||||
// if (lastFlameCount.difference(dates[i]).inDays == 1) {
|
|
||||||
// flamesCount++;
|
|
||||||
// lastFlameCount = dates[i];
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// break; // Stop counting if there's a break in the sequence
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return flamesCount;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Future<int> getFlamesForOtherUser(int otherUserId) async {
|
|
||||||
// List<(DateTime, int?)> dates = await DbMessages.getMessageDates(otherUserId);
|
|
||||||
// // print("Dates ${dates.length}");
|
|
||||||
// if (dates.isEmpty) return 0;
|
|
||||||
|
|
||||||
// List<DateTime> received =
|
|
||||||
// dates.where((x) => x.$2 != null).map((x) => x.$1).toList();
|
|
||||||
// List<DateTime> send =
|
|
||||||
// dates.where((x) => x.$2 == null).map((x) => x.$1).toList();
|
|
||||||
|
|
||||||
// int a = getFlameCounter(received);
|
|
||||||
// int b = getFlameCounter(send);
|
|
||||||
// // print("Received $a and send $b");
|
|
||||||
// return min(a, b);
|
|
||||||
// }
|
|
||||||
|
|
||||||
Duration calculateTimeDifference(DateTime now, DateTime startTime) {
|
|
||||||
// Get the timezone offsets
|
|
||||||
Duration nowOffset = now.timeZoneOffset;
|
|
||||||
Duration startTimeOffset = startTime.timeZoneOffset;
|
|
||||||
|
|
||||||
// Convert both DateTime objects to UTC
|
|
||||||
DateTime nowInUTC = now.subtract(nowOffset);
|
|
||||||
DateTime startTimeInUTC = startTime.subtract(startTimeOffset);
|
|
||||||
|
|
||||||
// Calculate the difference
|
|
||||||
return nowInUTC.difference(startTimeInUTC);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool> authenticateUser(String localizedReason,
|
Future<bool> authenticateUser(String localizedReason,
|
||||||
{bool force = true}) async {
|
{bool force = true}) async {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -248,14 +248,6 @@ class _ChatItemDetailsViewState extends State<ChatItemDetailsView> {
|
||||||
(messages[i - 1].messageOtherId != null &&
|
(messages[i - 1].messageOtherId != null &&
|
||||||
messages[i].messageOtherId != null);
|
messages[i].messageOtherId != null);
|
||||||
}
|
}
|
||||||
// if (messages[i].openedAt != null) {
|
|
||||||
// if (calculateTimeDifference(
|
|
||||||
// DateTime.now(), messages[i].openedAt!)
|
|
||||||
// .inHours >=
|
|
||||||
// 24) {
|
|
||||||
// return Container();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
return ChatListEntry(
|
return ChatListEntry(
|
||||||
messages[i],
|
messages[i],
|
||||||
widget.userid,
|
widget.userid,
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,6 @@ class ChatListView extends StatefulWidget {
|
||||||
class _ChatListViewState extends State<ChatListView> {
|
class _ChatListViewState extends State<ChatListView> {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
Stream<List<Contact>> contacts = twonlyDatabase.watchContactsForChatList();
|
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: GestureDetector(
|
title: GestureDetector(
|
||||||
|
|
@ -88,7 +86,7 @@ class _ChatListViewState extends State<ChatListView> {
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
body: StreamBuilder(
|
body: StreamBuilder(
|
||||||
stream: contacts,
|
stream: twonlyDatabase.watchContactsForChatList(),
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (!snapshot.hasData || snapshot.data == null) {
|
if (!snapshot.hasData || snapshot.data == null) {
|
||||||
return Container();
|
return Container();
|
||||||
|
|
@ -168,8 +166,10 @@ class _UserListItem extends State<UserListItem> {
|
||||||
setState(() {
|
setState(() {
|
||||||
if (currentMessage != null) {
|
if (currentMessage != null) {
|
||||||
lastMessageInSeconds =
|
lastMessageInSeconds =
|
||||||
calculateTimeDifference(DateTime.now(), currentMessage!.sendAt)
|
(DateTime.now().difference(currentMessage!.sendAt)).inSeconds;
|
||||||
.inSeconds;
|
if (lastMessageInSeconds < 0) {
|
||||||
|
lastMessageInSeconds = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -260,7 +260,9 @@ class _UserListItem extends State<UserListItem> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Message msg = currentMessage!;
|
Message msg = currentMessage!;
|
||||||
if (msg.kind == MessageKind.media && msg.messageOtherId != null) {
|
if (msg.kind == MessageKind.media &&
|
||||||
|
msg.messageOtherId != null &&
|
||||||
|
msg.openedAt == null) {
|
||||||
switch (msg.downloadState) {
|
switch (msg.downloadState) {
|
||||||
case DownloadState.pending:
|
case DownloadState.pending:
|
||||||
MediaMessageContent content =
|
MediaMessageContent content =
|
||||||
|
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
// Function to calculate time difference
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:twonly/src/utils/misc.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
test('Time difference calculation between different time zones', () {
|
|
||||||
// Test case 1: Current time is in UTC and start time is in UTC+2
|
|
||||||
DateTime now = DateTime.parse('2023-10-01T10:00:00Z'); // 10:00 UTC
|
|
||||||
DateTime startTime =
|
|
||||||
DateTime.parse('2023-10-01T12:00:00+02:00'); // 12:00 UTC+2
|
|
||||||
|
|
||||||
Duration difference = calculateTimeDifference(now, startTime);
|
|
||||||
expect(difference.inHours, equals(0)); // 10:00 UTC - 12:00 UTC+2 = 0 hours
|
|
||||||
|
|
||||||
// Test case 2: Current time is in UTC-1 and start time is in UTC+1
|
|
||||||
now = DateTime.parse('2023-10-01T09:00:00-01:00'); // 09:00 UTC-1
|
|
||||||
startTime = DateTime.parse('2023-10-01T11:00:00+01:00'); // 11:00 UTC+1
|
|
||||||
|
|
||||||
difference = calculateTimeDifference(now, startTime);
|
|
||||||
expect(
|
|
||||||
difference.inHours, equals(0)); // 09:00 UTC-1 - 11:00 UTC+1 = 0 hours
|
|
||||||
|
|
||||||
// Test case 3: Current time is in UTC+3 and start time is in UTC-1
|
|
||||||
now = DateTime.parse('2023-10-01T15:00:00+03:00'); // 15:00 UTC+3
|
|
||||||
startTime = DateTime.parse('2023-10-01T13:00:00-01:00'); // 13:00 UTC-1
|
|
||||||
|
|
||||||
difference = calculateTimeDifference(now, startTime);
|
|
||||||
expect(
|
|
||||||
difference.inHours, equals(-2)); // 15:00 UTC+3 - 13:00 UTC-1 = -2 hours
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue