Skip to content

Commit 74856fa

Browse files
committed
feat(sync-all): show notifications
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 1220baa commit 74856fa

4 files changed

Lines changed: 41 additions & 100 deletions

File tree

app/src/main/java/com/nextcloud/client/jobs/folderDownload/FolderDownloadWorker.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class FolderDownloadWorker(
4747
fun isDownloading(id: Long): Boolean = pendingDownloads.contains(id)
4848
}
4949

50-
private val notificationManager = FolderDownloadWorkerNotificationManager(context, viewThemeUtils)
50+
private val notificationManager = FolderDownloadWorkerNotificationManager(context, true, viewThemeUtils)
5151
private val folderDownloadEventBroadcaster = FolderDownloadEventBroadcaster(context, localBroadcastManager)
5252
private lateinit var storageManager: FileDataStorageManager
5353

@@ -112,15 +112,12 @@ class FolderDownloadWorker(
112112
return@withContext Result.failure()
113113
}
114114

115-
withContext(Dispatchers.Main) {
116-
val notification = notificationManager.getProgressNotification(
117-
folder.fileName,
118-
file.fileName,
119-
index,
120-
files.size
121-
)
122-
notificationManager.showNotification(notification)
123-
}
115+
notificationManager.showProgressNotification(
116+
folder.fileName,
117+
file.fileName,
118+
index,
119+
files.size
120+
)
124121

125122
val operation = DownloadFileOperation(user, file, context)
126123
val operationResult = operation.execute(client)

app/src/main/java/com/nextcloud/client/jobs/folderDownload/FolderDownloadWorkerNotificationManager.kt

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.content.Context
1313
import android.content.Intent
1414
import androidx.work.ForegroundInfo
1515
import com.nextcloud.client.jobs.notification.WorkerNotificationManager
16+
import com.nextcloud.utils.extensions.mainThread
1617
import com.owncloud.android.R
1718
import com.owncloud.android.datamodel.OCFile
1819
import com.owncloud.android.ui.notifications.NotificationUtils
@@ -21,7 +22,11 @@ import kotlinx.coroutines.Dispatchers
2122
import kotlinx.coroutines.withContext
2223
import kotlin.random.Random
2324

24-
class FolderDownloadWorkerNotificationManager(private val context: Context, viewThemeUtils: ViewThemeUtils) :
25+
class FolderDownloadWorkerNotificationManager(
26+
private val context: Context,
27+
private val showCancel: Boolean = true,
28+
viewThemeUtils: ViewThemeUtils?
29+
) :
2530
WorkerNotificationManager(
2631
id = NOTIFICATION_ID,
2732
context,
@@ -42,7 +47,7 @@ class FolderDownloadWorkerNotificationManager(private val context: Context, view
4247
clearActions()
4348
setContentText(description)
4449

45-
if (progress != null) {
50+
if (progress != null && showCancel) {
4651
setProgress(MAX_PROGRESS, progress, false)
4752
addAction(
4853
R.drawable.ic_cancel,
@@ -68,29 +73,35 @@ class FolderDownloadWorkerNotificationManager(private val context: Context, view
6873
}
6974

7075
@Suppress("MagicNumber")
71-
fun getProgressNotification(
76+
fun showProgressNotification(
7277
folderName: String,
7378
filename: String,
7479
currentIndex: Int,
7580
totalFileSize: Int
76-
): Notification {
77-
val currentFileIndex = (currentIndex + 1)
78-
val description = context.getString(R.string.folder_download_counter, currentFileIndex, totalFileSize, filename)
79-
val progress = (currentFileIndex * MAX_PROGRESS) / totalFileSize
80-
return getNotification(folderName, description, progress)
81+
) {
82+
mainThread(delay = 0) {
83+
val currentFileIndex = (currentIndex + 1)
84+
val description =
85+
context.getString(R.string.folder_download_counter, currentFileIndex, totalFileSize, filename)
86+
val progress = (currentFileIndex * MAX_PROGRESS) / totalFileSize
87+
val notification = getNotification(folderName, description, progress)
88+
showNotification(notification)
89+
}
8190
}
8291

83-
suspend fun showCompletionNotification(folderName: String, success: Boolean) = withContext(Dispatchers.Main) {
84-
val titleId = if (success) {
85-
R.string.folder_download_success_notification_title
86-
} else {
87-
R.string.folder_download_error_notification_title
88-
}
92+
fun showCompletionNotification(folderName: String, success: Boolean) {
93+
mainThread(delay = 0) {
94+
val titleId = if (success) {
95+
R.string.folder_download_success_notification_title
96+
} else {
97+
R.string.folder_download_error_notification_title
98+
}
8999

90-
val title = context.getString(titleId, folderName)
100+
val title = context.getString(titleId, folderName)
91101

92-
val notification = getNotification(title)
93-
notificationManager.notify(NOTIFICATION_ID, notification)
102+
val notification = getNotification(title)
103+
notificationManager.notify(NOTIFICATION_ID, notification)
104+
}
94105
}
95106

96107
suspend fun showNotAvailableDiskSpace() = withContext(Dispatchers.Main) {

app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.nextcloud.client.account.User;
1818
import com.nextcloud.client.jobs.download.FileDownloadHelper;
19+
import com.nextcloud.client.jobs.folderDownload.FolderDownloadWorkerNotificationManager;
1920
import com.owncloud.android.datamodel.FileDataStorageManager;
2021
import com.owncloud.android.datamodel.OCFile;
2122
import com.owncloud.android.datamodel.e2e.v1.decrypted.DecryptedFolderMetadataFileV1;
@@ -29,7 +30,6 @@
2930
import com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation;
3031
import com.owncloud.android.lib.resources.files.model.RemoteFile;
3132
import com.owncloud.android.operations.common.SyncOperation;
32-
import com.owncloud.android.operations.manager.SynchronizeFileNotificationManager;
3333
import com.owncloud.android.services.OperationsService;
3434
import com.owncloud.android.utils.FileStorageUtils;
3535
import com.owncloud.android.utils.MimeTypeUtil;
@@ -91,7 +91,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
9191

9292
private final boolean syncAll;
9393

94-
final SynchronizeFileNotificationManager notificationManager;
94+
final FolderDownloadWorkerNotificationManager notificationManager;
9595

9696
/**
9797
* Creates a new instance of {@link SynchronizeFolderOperation}.
@@ -117,7 +117,7 @@ public SynchronizeFolderOperation(Context context,
117117
mCancellationRequested = new AtomicBoolean(false);
118118
this.useWorkerWithNotification = useWorkerWithNotification;
119119
this.syncAll = syncAll;
120-
notificationManager = new SynchronizeFileNotificationManager(context, null);
120+
notificationManager = new FolderDownloadWorkerNotificationManager(context, false,null);
121121
}
122122

123123

@@ -525,6 +525,7 @@ private void startContentSynchronizations(List<SynchronizeFileOperation> filesTo
525525
int total = filesToSyncContents.size();
526526
int current = 0;
527527
boolean success = true;
528+
String folderName = mLocalFolder.getFileName();
528529

529530
for (SynchronizeFileOperation op: filesToSyncContents) {
530531
if (mCancellationRequested.get()) {
@@ -534,10 +535,9 @@ private void startContentSynchronizations(List<SynchronizeFileOperation> filesTo
534535
contentsResult = op.execute(mContext);
535536
current++;
536537

537-
int progress = (int) ((current * 100.0f) / total);
538538
final var file = op.getLocalFile();
539539
if (file != null) {
540-
notificationManager.showProgress(file.getFileName(), progress);
540+
notificationManager.showProgressNotification(folderName, file.getFileName(), current, total);
541541
}
542542

543543
if (!contentsResult.isSuccess()) {
@@ -558,7 +558,7 @@ private void startContentSynchronizations(List<SynchronizeFileOperation> filesTo
558558
}
559559

560560
// FIXME: NOT DISPLAYING
561-
notificationManager.showCompletion(mLocalFolder.getFileName(), success);
561+
notificationManager.showCompletionNotification(folderName, success);
562562
}
563563

564564
/**

app/src/main/java/com/owncloud/android/operations/manager/SynchronizeFileNotificationManager.kt

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)