6464import java .util .Optional ;
6565
6666import androidx .annotation .NonNull ;
67+ import androidx .core .content .ContextCompat ;
6768import kotlin .Unit ;
68- import kotlin .jvm .functions .Function0 ;
6969
7070/**
7171 * This Adapter populates a ListView with following types of uploads: pending, active, completed. Filtering possible.
@@ -170,37 +170,35 @@ public void onBindHeaderViewHolder(SectionedViewHolder holder, int section, bool
170170
171171 }
172172
173- ViewExtensionsKt .setVisibleIf (headerViewHolder .binding .autoUploadBatterySaverWarningCard .root , powerManagementService .isPowerSavingEnabled ());
173+ ViewExtensionsKt .setVisibleIf (headerViewHolder .binding .autoUploadBatterySaverWarningCard .root ,
174+ powerManagementService .isPowerSavingEnabled ());
174175 viewThemeUtils .material .themeCardView (headerViewHolder .binding .autoUploadBatterySaverWarningCard .root );
175176
176177 headerViewHolder .binding .uploadListAction .setOnClickListener (v -> {
177178 switch (group .type ) {
178179 case CURRENT -> {
179- String accountName = group .items ()[0 ].getAccountName ();
180+ OCUpload [] items = group .items ();
181+ if (items .length == 0 ) return ;
182+
183+ String accountName = items [0 ].getAccountName ();
180184
181185 final int totalUploads = group .items ().length ;
182186 final int [] completedCount = {0 };
183187
184188 for (int i =0 ; i <group .items .length ; i ++) {
185189 OCUpload upload = group .items [i ];
186- uploadHelper .updateUploadStatus (upload .getRemotePath (), accountName , UploadStatus .UPLOAD_CANCELLED , new Function0 <Unit >() {
187- @ Override
188- public Unit invoke () {
189- FileUploadWorker .Companion .cancelUpload (upload .getRemotePath (), accountName , new Function0 <Unit >() {
190- @ Override
191- public Unit invoke () {
192- completedCount [0 ]++;
193- if (completedCount [0 ] == totalUploads ) {
194- Log_OC .d (TAG , "refreshing upload items" );
195-
196- // All uploads finished, refresh UI once
197- loadUploadItemsFromDb (() -> {});
198- }
199- return Unit .INSTANCE ;
200- }
201- });
190+ uploadHelper .updateUploadStatus (upload .getRemotePath (), accountName , UploadStatus .UPLOAD_CANCELLED , () -> {
191+ FileUploadWorker .Companion .cancelUpload (upload .getRemotePath (), accountName , () -> {
192+ completedCount [0 ]++;
193+ if (completedCount [0 ] == totalUploads ) {
194+ Log_OC .d (TAG , "refreshing upload items" );
195+
196+ // All uploads finished, refresh UI once
197+ loadUploadItemsFromDb (() -> {});
198+ }
202199 return Unit .INSTANCE ;
203- }
200+ });
201+ return Unit .INSTANCE ;
204202 });
205203 }
206204 }
@@ -338,7 +336,8 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
338336 }
339337
340338 // upload date
341- boolean showUploadDate = updateTime > 0 && item .getUploadStatus () == UploadStatus .UPLOAD_SUCCEEDED && item .getLastResult () == UploadResult .UPLOADED ;
339+ boolean showUploadDate = (updateTime > 0 && item .getUploadStatus () == UploadStatus .UPLOAD_SUCCEEDED &&
340+ item .getLastResult () == UploadResult .UPLOADED );
342341 itemViewHolder .binding .uploadDate .setVisibility (showUploadDate ? View .VISIBLE : View .GONE );
343342 if (showUploadDate ) {
344343 CharSequence dateString = DisplayUtils .getRelativeDateTimeString (parentActivity ,
@@ -381,8 +380,12 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
381380 // really uploading, so...
382381 // ... unbind the old progress bar, if any; ...
383382 if (uploadProgressListener != null ) {
384- String targetKey = FileUploadHelper .Companion .buildRemoteName (uploadProgressListener .getUpload ().getAccountName (), uploadProgressListener .getUpload ().getRemotePath ());
385- uploadHelper .removeUploadTransferProgressListener (uploadProgressListener , targetKey );
383+ final var upload = uploadProgressListener .getUpload ();
384+ if (upload != null ) {
385+ String targetKey = FileUploadHelper .Companion .buildRemoteName (upload .getAccountName (),
386+ upload .getRemotePath ());
387+ uploadHelper .removeUploadTransferProgressListener (uploadProgressListener , targetKey );
388+ }
386389 }
387390 // ... then, bind the current progress bar to listen for updates
388391 uploadProgressListener = new UploadProgressListener (item , itemViewHolder .binding .uploadProgressBar );
@@ -393,11 +396,13 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
393396 // not really uploading; stop listening progress if view is reused!
394397 if (uploadProgressListener != null &&
395398 uploadProgressListener .isWrapping (itemViewHolder .binding .uploadProgressBar )) {
396-
397- String targetKey = FileUploadHelper .Companion .buildRemoteName (uploadProgressListener .getUpload ().getAccountName (), uploadProgressListener .getUpload ().getRemotePath ());
398-
399- uploadHelper .removeUploadTransferProgressListener (uploadProgressListener , targetKey );
400- uploadProgressListener = null ;
399+ final var upload = uploadProgressListener .getUpload ();
400+ if (upload != null ) {
401+ String targetKey = FileUploadHelper .Companion .buildRemoteName (upload .getAccountName (),
402+ upload .getRemotePath ());
403+ uploadHelper .removeUploadTransferProgressListener (uploadProgressListener , targetKey );
404+ uploadProgressListener = null ;
405+ }
401406 }
402407 }
403408
@@ -439,9 +444,7 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
439444 } else if (item .getUploadStatus () == UploadStatus .UPLOAD_FAILED ) {
440445 if (item .getLastResult () == UploadResult .SYNC_CONFLICT ) {
441446 itemViewHolder .binding .uploadRightButton .setImageResource (R .drawable .ic_dots_vertical );
442- itemViewHolder .binding .uploadRightButton .setOnClickListener (view -> {
443- optionalUser .ifPresent (user -> showItemConflictPopup (user , itemViewHolder , item , status , view ));
444- });
447+ itemViewHolder .binding .uploadRightButton .setOnClickListener (view -> optionalUser .ifPresent (user -> showItemConflictPopup (user , itemViewHolder , item , status , view )));
445448 } else {
446449 // Delete
447450 itemViewHolder .binding .uploadRightButton .setImageResource (R .drawable .ic_action_delete_grey );
@@ -511,7 +514,9 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
511514 && fakeFileToCheatThumbnailsCacheManagerInterface .getRemoteId () != null &&
512515 item .getUploadStatus () == UploadStatus .UPLOAD_SUCCEEDED ) {
513516 // Thumbnail in Cache?
514- Bitmap thumbnail = ThumbnailsCacheManager .getBitmapFromDiskCache (String .valueOf (fakeFileToCheatThumbnailsCacheManagerInterface .getRemoteId ()));
517+
518+ final var cacheKey = String .valueOf (fakeFileToCheatThumbnailsCacheManagerInterface .getRemoteId ());
519+ Bitmap thumbnail = ThumbnailsCacheManager .getBitmapFromDiskCache (cacheKey );
515520
516521 if (thumbnail != null && !fakeFileToCheatThumbnailsCacheManagerInterface .isUpdateThumbnailNeeded ()) {
517522 itemViewHolder .binding .thumbnail .setImageBitmap (thumbnail );
@@ -545,7 +550,8 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
545550 }
546551
547552 if ("image/png" .equals (item .getMimeType ())) {
548- itemViewHolder .binding .thumbnail .setBackgroundColor (parentActivity .getResources ().getColor (R .color .bg_default ));
553+ final var backgroundColor = ContextCompat .getColor (parentActivity , R .color .bg_default );
554+ itemViewHolder .binding .thumbnail .setBackgroundColor (backgroundColor );
549555 }
550556 } else if (MimeTypeUtil .isImage (fakeFileToCheatThumbnailsCacheManagerInterface )) {
551557 File file = new File (item .getLocalPath ());
@@ -566,9 +572,7 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
566572 }
567573
568574 final ThumbnailsCacheManager .AsyncThumbnailDrawable asyncDrawable =
569- new ThumbnailsCacheManager .AsyncThumbnailDrawable (parentActivity .getResources (), thumbnail ,
570- task );
571-
575+ new ThumbnailsCacheManager .AsyncThumbnailDrawable (parentActivity .getResources (), thumbnail , task );
572576 task .execute (new ThumbnailsCacheManager .ThumbnailGenerationTaskObject (file , null ));
573577 task .setListener (new ThumbnailsCacheManager .ThumbnailGenerationTask .Listener () {
574578 @ Override
@@ -586,7 +590,8 @@ public void onError() {
586590 }
587591
588592 if ("image/png" .equalsIgnoreCase (item .getMimeType ())) {
589- itemViewHolder .binding .thumbnail .setBackgroundColor (parentActivity .getResources ().getColor (R .color .bg_default ));
593+ final var backgroundColor = ContextCompat .getColor (parentActivity , R .color .bg_default );
594+ itemViewHolder .binding .thumbnail .setBackgroundColor (backgroundColor );
590595 }
591596 } else {
592597 if (optionalUser .isPresent ()) {
@@ -633,7 +638,8 @@ private boolean checkAndOpenConflictResolutionDialog(User user,
633638 return false ;
634639 }
635640
636- private void refreshFolderAndUpdateUI (ItemViewHolder holder , User user , OCFile folder , String remotePath , OCUpload item , String status ) {
641+ private void refreshFolderAndUpdateUI (ItemViewHolder holder , User user , OCFile folder , String remotePath ,
642+ OCUpload item , String status ) {
637643 Context context = MainApp .getAppContext ();
638644
639645 this .refreshFolder (context , holder , user , folder , (caller , result ) -> {
@@ -885,9 +891,11 @@ private void onUploadedItemClick(OCUpload upload) {
885891 return ;
886892 }
887893
888- if (PreviewImageFragment .canBePreviewed (file )) {
894+ final var optionalUser = parentActivity .getUser ();
895+
896+ if (PreviewImageFragment .canBePreviewed (file ) && optionalUser .isPresent ()) {
889897 //show image preview and stay in uploads tab
890- Intent intent = FileDisplayActivity .openFileIntent (parentActivity , parentActivity . getUser () .get (), file );
898+ Intent intent = FileDisplayActivity .openFileIntent (parentActivity , optionalUser .get (), file );
891899 parentActivity .startActivity (intent );
892900 } else {
893901 Intent intent = new Intent (parentActivity , FileDisplayActivity .class );
0 commit comments