|
17 | 17 | import android.view.ViewGroup; |
18 | 18 | import android.view.inputmethod.EditorInfo; |
19 | 19 |
|
| 20 | +import androidx.activity.OnBackPressedCallback; |
20 | 21 | import androidx.activity.result.ActivityResultLauncher; |
21 | 22 | import androidx.activity.result.contract.ActivityResultContracts; |
22 | 23 | import androidx.annotation.NonNull; |
|
36 | 37 | import org.jetbrains.annotations.NotNull; |
37 | 38 |
|
38 | 39 | import java.util.ArrayList; |
| 40 | +import java.util.Collections; |
39 | 41 | import java.util.List; |
40 | 42 | import java.util.concurrent.Executors; |
41 | 43 | import java.util.concurrent.Future; |
@@ -89,24 +91,51 @@ public class NoteShareActivity extends BrandedActivity implements ShareeListAdap |
89 | 91 | private Account account; |
90 | 92 | private ShareRepository repository; |
91 | 93 | private Capabilities capabilities; |
92 | | - private final List<OCShare> shares = new ArrayList<>(); |
| 94 | + private ActivityResultLauncher<Intent> resultLauncher; |
| 95 | + private final List<OCShare> shares = Collections.synchronizedList(new ArrayList<>()); |
93 | 96 |
|
94 | 97 | public void onCreate(@Nullable Bundle savedInstanceState) { |
95 | 98 | super.onCreate(savedInstanceState); |
96 | 99 |
|
97 | 100 | executorService = Executors.newSingleThreadScheduledExecutor(); |
98 | 101 | binding = ActivityNoteShareBinding.inflate(getLayoutInflater()); |
99 | 102 | setContentView(binding.getRoot()); |
| 103 | + registerResultLauncher(); |
100 | 104 | initializeArguments(); |
| 105 | + initializeOnBackPressedDispatcher(); |
| 106 | + } |
| 107 | + |
| 108 | + private void registerResultLauncher() { |
| 109 | + resultLauncher = registerForActivityResult( |
| 110 | + new ActivityResultContracts.StartActivityForResult(), |
| 111 | + result -> { |
| 112 | + if (result.getResultCode() == RESULT_OK && result.getData() != null) { |
| 113 | + recreate(); |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + private void initializeOnBackPressedDispatcher() { |
| 119 | + getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) { |
| 120 | + @Override |
| 121 | + public void handleOnBackPressed() { |
| 122 | + Intent intent = new Intent(NoteShareActivity.this, MainActivity.class); |
| 123 | + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); |
| 124 | + startActivity(intent); |
| 125 | + finish(); |
| 126 | + } |
| 127 | + }); |
101 | 128 | } |
102 | 129 |
|
103 | 130 | private void initializeArguments() { |
104 | 131 | Bundle bundler = getIntent().getExtras(); |
105 | 132 | note = BundleExtensionsKt.getSerializableArgument(bundler, ARG_NOTE, Note.class); |
106 | 133 | account = BundleExtensionsKt.getSerializableArgument(bundler, ARG_ACCOUNT, Account.class); |
| 134 | + |
107 | 135 | if (note == null) { |
108 | 136 | throw new IllegalArgumentException("Note cannot be null"); |
109 | 137 | } |
| 138 | + |
110 | 139 | if (account == null) { |
111 | 140 | throw new IllegalArgumentException("Account cannot be null"); |
112 | 141 | } |
@@ -280,7 +309,7 @@ private void navigateNoteShareDetail(String shareWith, int shareType) { |
280 | 309 |
|
281 | 310 | Intent intent = new Intent(this, NoteShareDetailActivity.class); |
282 | 311 | intent.putExtras(bundle); |
283 | | - startActivity(intent); |
| 312 | + resultLauncher.launch(intent); |
284 | 313 | } |
285 | 314 |
|
286 | 315 | private boolean accountOwnsFile() { |
@@ -428,56 +457,77 @@ public void requestPasswordForShare(OCShare share, boolean askForPassword) { |
428 | 457 | public void showProfileBottomSheet(Account account, String shareWith) { |
429 | 458 | } |
430 | 459 |
|
| 460 | + private boolean isInitializingShares = false; |
| 461 | + |
431 | 462 | public void updateShareeListAdapter() { |
| 463 | + if (isInitializingShares) { |
| 464 | + Log_OC.d(TAG, "Shares initialization already in progress"); |
| 465 | + return; |
| 466 | + } |
| 467 | + |
| 468 | + isInitializingShares = true; |
| 469 | + |
432 | 470 | executorService.submit(() -> { |
433 | 471 | try { |
434 | 472 | ShareeListAdapter adapter = (ShareeListAdapter) binding.sharesList.getAdapter(); |
435 | 473 |
|
436 | 474 | if (adapter == null) { |
437 | 475 | runOnUiThread(() -> DisplayUtils.showSnackMessage(NoteShareActivity.this, getString(R.string.could_not_retrieve_shares))); |
| 476 | + isInitializingShares = false; |
438 | 477 | return; |
439 | 478 | } |
440 | 479 |
|
441 | | - // clear adapter |
442 | | - adapter.removeAll(); |
443 | | - shares.clear(); |
| 480 | + List<OCShare> tempShares = new ArrayList<>(); |
444 | 481 |
|
445 | 482 | // to show share with users/groups info |
446 | 483 | if (note != null) { |
447 | 484 | // get shares from local DB |
448 | | - final var shareEntities = repository.getShareEntitiesForSpecificNote(note); |
449 | | - shareEntities.forEach(entity -> { |
450 | | - if (entity.getId() != null) { |
451 | | - addShares(entity.getId()); |
452 | | - } |
453 | | - }); |
454 | | - |
455 | | - // get shares from remote |
456 | | - final var shares = repository.getShareFromNote(note); |
457 | | - if (shares != null) { |
458 | | - shares.forEach(entity -> addShares(entity.getId())); |
459 | | - } |
| 485 | + populateSharesList(tempShares); |
460 | 486 | } |
461 | 487 |
|
462 | 488 | runOnUiThread(() -> { |
| 489 | + shares.clear(); |
| 490 | + shares.addAll(tempShares); |
| 491 | + |
| 492 | + adapter.removeAll(); |
463 | 493 | adapter.addShares(shares); |
464 | 494 | addPublicShares(adapter); |
465 | 495 | setShareWithYou(); |
| 496 | + |
| 497 | + isInitializingShares = false; |
466 | 498 | }); |
467 | 499 | } catch (Exception e) { |
468 | 500 | Log_OC.d(TAG, "Exception while updateShareeListAdapter: " + e); |
469 | 501 | } |
470 | 502 | }); |
471 | 503 | } |
472 | 504 |
|
473 | | - private void addShares(long id) { |
| 505 | + private void populateSharesList(List<OCShare> targetList) { |
| 506 | + // Get shares from local DB |
| 507 | + final var shareEntities = repository.getShareEntitiesForSpecificNote(note); |
| 508 | + for (var entity : shareEntities) { |
| 509 | + if (entity.getId() != null) { |
| 510 | + addSharesToList(entity.getId(), targetList); |
| 511 | + } |
| 512 | + } |
| 513 | + |
| 514 | + // Get shares from remote |
| 515 | + final var remoteShares = repository.getShareFromNote(note); |
| 516 | + if (remoteShares != null) { |
| 517 | + for (var entity : remoteShares) { |
| 518 | + addSharesToList(entity.getId(), targetList); |
| 519 | + } |
| 520 | + } |
| 521 | + } |
| 522 | + |
| 523 | + private void addSharesToList(long id, List<OCShare> targetList) { |
474 | 524 | final var result = repository.getShares(id); |
475 | 525 | if (result != null) { |
476 | | - result.forEach(ocShare -> { |
477 | | - if (!shares.contains(ocShare)) { |
478 | | - shares.add(ocShare); |
| 526 | + for (OCShare ocShare : result) { |
| 527 | + if (!targetList.contains(ocShare)) { |
| 528 | + targetList.add(ocShare); |
479 | 529 | } |
480 | | - }); |
| 530 | + } |
481 | 531 | } |
482 | 532 | } |
483 | 533 |
|
@@ -629,7 +679,7 @@ private void modifyExistingShare(OCShare share, int screenTypePermission) { |
629 | 679 |
|
630 | 680 | Intent intent = new Intent(this, NoteShareDetailActivity.class); |
631 | 681 | intent.putExtras(bundle); |
632 | | - startActivity(intent); |
| 682 | + resultLauncher.launch(intent); |
633 | 683 | } |
634 | 684 |
|
635 | 685 | private boolean getExpDateShown() { |
|
0 commit comments