fix box problem with foreground service

This commit is contained in:
otsmr 2025-02-09 23:06:12 +01:00
parent 80e8682357
commit 9485203383
5 changed files with 21 additions and 21 deletions

View file

@ -216,7 +216,7 @@ Future sendImage(
}
}
Future tryDownloadMedia(int messageId, List<int> mediaToken,
Future tryDownloadMedia(int messageId, int fromUserId, List<int> mediaToken,
{bool force = false}) async {
if (globalIsAppInBackground) return;
if (!force) {
@ -237,6 +237,7 @@ Future tryDownloadMedia(int messageId, List<int> mediaToken,
}
globalCallBackOnDownloadChange(mediaToken, true);
box.put("${mediaToken}_messageId", messageId);
box.put("${mediaToken}_fromUserId", fromUserId);
apiProvider.triggerDownload(mediaToken, offset);
}
@ -255,7 +256,7 @@ Future userOpenedOtherMessage(int fromUserId, int messageOtherId) async {
}
Future<Uint8List?> getDownloadedMedia(
List<int> mediaToken, int messageOtherId) async {
List<int> mediaToken, int messageOtherId, int otherUserId) async {
final box = await getMediaStorage();
Uint8List? media;
try {
@ -265,13 +266,11 @@ Future<Uint8List?> getDownloadedMedia(
}
if (media == null) return null;
int fromUserId = box.get("${mediaToken}_fromUserId");
await userOpenedOtherMessage(fromUserId, messageOtherId);
await userOpenedOtherMessage(otherUserId, messageOtherId);
box.delete(mediaToken.toString());
box.put("${mediaToken}_downloaded", "deleted");
box.delete("${mediaToken}_fromUserId");
box.delete("${mediaToken}_messageId");
box.delete("${mediaToken}_fromUserId");
return media;
}

View file

@ -2,7 +2,6 @@ import 'dart:convert';
import 'dart:typed_data';
import 'package:fixnum/fixnum.dart';
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import 'package:libsignal_protocol_dart/libsignal_protocol_dart.dart';
import 'package:logging/logging.dart';
import 'package:twonly/globals.dart';
@ -50,6 +49,10 @@ Future handleServerMessage(server.ServerToClient msg) async {
}
Future<client.Response> handleDownloadData(DownloadData data) async {
if (globalIsAppInBackground) {
// download should only be done when the app is open
return client.Response()..error = ErrorCode.InternalError;
}
debugPrint("Downloading: ${data.uploadToken} ${data.fin}");
final box = await getMediaStorage();
@ -61,7 +64,6 @@ Future<client.Response> handleDownloadData(DownloadData data) async {
if (messageId != null) {
int? fromUserId = await DbMessages.deleteMessageById(messageId);
box.delete(boxId);
// int? fromUserId = box.get("${data.uploadToken}_fromUserId");
if (fromUserId != null) {
globalCallBackOnMessageChange(fromUserId);
}
@ -181,16 +183,12 @@ Future<client.Response> handleNewMessage(
message.kind == MessageKind.image) {
await DbContacts.checkAndUpdateFlames(fromUserId.toInt(),
timestamp: message.timestamp);
if (!globalIsAppInBackground) {
final content = message.content;
if (content is MediaMessageContent) {
List<int> downloadToken = content.downloadToken;
Box box = await getMediaStorage();
box.put("${downloadToken}_fromUserId", fromUserId.toInt());
if (box.get("${downloadToken}_fromUserId") == null) {
debugPrint("BOX IS NOT WORKING");
tryDownloadMedia(messageId, fromUserId.toInt(), downloadToken);
}
tryDownloadMedia(messageId, downloadToken);
}
}
localPushNotificationNewMessage(

View file

@ -80,7 +80,8 @@ class ChatListEntry extends StatelessWidget {
}),
);
} else {
tryDownloadMedia(message.messageId, token, force: true);
tryDownloadMedia(message.messageId, message.otherUserId, token,
force: true);
}
}
},

View file

@ -208,7 +208,9 @@ class _UserListItem extends State<UserListItem> {
onTap: () {
if (isDownloading) return;
if (!widget.lastMessage.isDownloaded) {
tryDownloadMedia(widget.lastMessage.messageId, token, force: true);
tryDownloadMedia(widget.lastMessage.messageId,
widget.lastMessage.otherUserId, token,
force: true);
return;
}
if (state == MessageSendState.received &&

View file

@ -73,8 +73,8 @@ class _MediaViewerViewState extends State<MediaViewerView> {
flutterLocalNotificationsPlugin.cancel(widget.message.messageId);
List<int> token = content.downloadToken;
_imageByte =
await getDownloadedMedia(token, widget.message.messageOtherId!);
_imageByte = await getDownloadedMedia(
token, widget.message.messageOtherId!, widget.message.otherUserId);
if (_imageByte == null) {
// image already deleted
if (context.mounted) {