Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ff2bc1f2 authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

Fix bulk sharing from list view

parent 096b1ae7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ public class AppendToNoteActivity extends MainActivity {
    @Override
    public void onNoteClick(int position, View v) {
        if (!TextUtils.isEmpty(receivedText)) {
            final LiveData<Note> fullNote$ = mainViewModel.getFullNote(((Note) adapter.getItem(position)).getId());
            final LiveData<Note> fullNote$ = mainViewModel.getFullNote$(((Note) adapter.getItem(position)).getId());
            fullNote$.observe(this, (fullNote) -> {
                fullNote$.removeObservers(this);
                final String oldContent = fullNote.getContent();
+20 −23
Original line number Diff line number Diff line
@@ -528,10 +528,15 @@ public class MainViewModel extends AndroidViewModel {
        return db.addAccount(url, username, accountName, capabilities);
    }

    public LiveData<Note> getFullNote(long id) {
    public LiveData<Note> getFullNote$(long id) {
        return map(getFullNotesWithCategory(Collections.singleton(id)), input -> input.get(0));
    }

    @WorkerThread
    public Note getFullNote(long id) {
        return db.getNoteDao().getNoteById(id);
    }

    public LiveData<List<Note>> getFullNotesWithCategory(@NonNull Collection<Long> ids) {
        return switchMap(getCurrentAccount(), currentAccount -> {
            if (currentAccount == null) {
@@ -579,12 +584,8 @@ public class MainViewModel extends AndroidViewModel {
        return db.getAccountDao().countAccounts$();
    }

    public LiveData<String> collectNoteContents(List<Long> noteIds) {
        return switchMap(getCurrentAccount(), currentAccount -> {
            if (currentAccount != null) {
                Log.v(TAG, "[collectNoteContents] - currentAccount: " + currentAccount.getAccountName());
                final MutableLiveData<String> collectedContent$ = new MutableLiveData<>();
                new Thread(() -> {
    @WorkerThread
    public String collectNoteContents(@NonNull List<Long> noteIds) {
        final StringBuilder noteContents = new StringBuilder();
        for (Long noteId : noteIds) {
            final Note fullNote = db.getNoteDao().getNoteById(noteId);
@@ -596,10 +597,6 @@ public class MainViewModel extends AndroidViewModel {
                noteContents.append(tempFullNote);
            }
        }
                }).start();
                return collectedContent$;
            }
            return new MutableLiveData<>(null);
        });
        return noteContents.toString();
    }
}
 No newline at end of file
+14 −14
Original line number Diff line number Diff line
@@ -137,18 +137,18 @@ public class MultiSelectedActionModeCallback implements Callback {
            for (Long sel : tracker.getSelection()) {
                selection.add(sel);
            }
            // FIXME use title if only one
            final String subject = context.getResources().getQuantityString(R.plurals.share_multiple, selection.size(), selection.size());
//            final String subject = (selection.size() == 1)
//                    ? ((Note) adapter.getItem(adapter.getSelected().get(0))).getTitle()
//                    : context.getResources().getQuantityString(R.plurals.share_multiple, adapter.getSelected().size(), adapter.getSelected().size());

            final LiveData<String> contentCollector = mainViewModel.collectNoteContents(selection);
            contentCollector.observe(lifecycleOwner, (next) -> {
                contentCollector.removeObservers(lifecycleOwner);
                ShareUtil.openShareDialog(context, subject, next);
            tracker.clearSelection();
            });

            new Thread(() -> {
                if (selection.size() == 1) {
                    final Note note = mainViewModel.getFullNote(selection.get(0));
                    ShareUtil.openShareDialog(context, note.getTitle(), note.getContent());
                } else {
                    ShareUtil.openShareDialog(context,
                            context.getResources().getQuantityString(R.plurals.share_multiple, selection.size(), selection.size()),
                            mainViewModel.collectNoteContents(selection));
                }
            }).start();
            return true;
        } else if (itemId == R.id.menu_category) {// TODO detect whether all selected notes do have the same category - in this case preselect it
            final LiveData<Account> accountLiveData = mainViewModel.getCurrentAccount();
+1 −1
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ public class NotesListViewItemTouchHelper extends ItemTouchHelper {
                    case ItemTouchHelper.LEFT:
                        viewHolder.setIsRecyclable(false);
                        final Note dbNoteWithoutContent = (Note) adapter.getItem(viewHolder.getLayoutPosition());
                        final LiveData<Note> dbNoteLiveData = mainViewModel.getFullNote(dbNoteWithoutContent.getId());
                        final LiveData<Note> dbNoteLiveData = mainViewModel.getFullNote$(dbNoteWithoutContent.getId());
                        dbNoteLiveData.observe(lifecycleOwner, (dbNote) -> {
                            dbNoteLiveData.removeObservers(lifecycleOwner);
                            tracker.deselect(dbNote.getId());