Skip to content

Commit 15a7137

Browse files
Merge pull request #2714 from nextcloud/bugfix/set-as-favorite
BugFix - Set As Favorite
2 parents 9a1d344 + 8700279 commit 15a7137

File tree

9 files changed

+64
-17
lines changed

9 files changed

+64
-17
lines changed

app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
248248
return true;
249249
} else if (itemId == R.id.menu_favorite) {
250250
note.setFavorite(!note.getFavorite());
251-
repo.toggleFavoriteAndSync(localAccount, note.getId());
251+
repo.toggleFavoriteAndSync(localAccount, note);
252252
listener.onNoteUpdated(note);
253253
prepareFavoriteOption(item);
254254
return true;

app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,15 @@ public void onNoteClick(int position, View v) {
786786

787787
@Override
788788
public void onNoteFavoriteClick(int position, View view) {
789-
final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(((Note) adapter.getItem(position)).getId());
790-
toggleLiveData.observe(this, (next) -> toggleLiveData.removeObservers(this));
789+
if (!(adapter.getItem(position) instanceof Note note)) {
790+
return;
791+
}
792+
793+
final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(note);
794+
toggleLiveData.observe(this, (next) -> {{
795+
toggleLiveData.removeObservers(this);
796+
adapter.notifyItemChanged(position);
797+
}});
791798
}
792799

793800
@Override

app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -513,16 +513,15 @@ public LiveData<Note> moveNoteToAnotherAccount(Account account, long noteId) {
513513
});
514514
}
515515

516-
public LiveData<Void> toggleFavoriteAndSync(long noteId) {
517-
return switchMap(getCurrentAccount(), currentAccount -> {
518-
if (currentAccount == null) {
519-
return new MutableLiveData<>(null);
520-
} else {
521-
Log.v(TAG, "[toggleFavoriteAndSync] - currentAccount: " + currentAccount.getAccountName());
522-
repo.toggleFavoriteAndSync(currentAccount, noteId);
523-
return new MutableLiveData<>(null);
524-
}
525-
});
516+
public LiveData<Void> toggleFavoriteAndSync(Note note) {
517+
final var currentAccount = getCurrentAccount().getValue();
518+
519+
if (currentAccount != null) {
520+
Log.v(TAG, "[toggleFavoriteAndSync] - currentAccount: " + currentAccount.getAccountName());
521+
repo.toggleFavoriteAndSync(currentAccount, note);
522+
}
523+
524+
return new MutableLiveData<>(null);
526525
}
527526

528527
public LiveData<Void> deleteNoteAndSync(long id) {

app/src/main/java/it/niedermann/owncloud/notes/main/items/list/NotesListViewItemTouchHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction)
101101
case ItemTouchHelper.RIGHT -> {
102102
viewHolder.setIsRecyclable(false);
103103
final var adapterNote = (Note) adapter.getItem(viewHolder.getLayoutPosition());
104-
final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(adapterNote.getId());
104+
final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(adapterNote);
105105
toggleLiveData.observe(lifecycleOwner, (next) -> toggleLiveData.removeObservers(lifecycleOwner));
106106
}
107107
default -> {

app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static androidx.lifecycle.Transformations.map;
1313
import static java.util.stream.Collectors.toMap;
1414
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT;
15+
import static it.niedermann.owncloud.notes.shared.util.ApiVersionUtil.getPreferredApiVersion;
1516
import static it.niedermann.owncloud.notes.shared.util.NoteUtil.generateNoteExcerpt;
1617
import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets;
1718
import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.updateSingleNoteWidgets;
@@ -47,6 +48,7 @@
4748
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
4849
import com.nextcloud.android.sso.helper.SingleAccountHelper;
4950
import com.nextcloud.android.sso.model.SingleSignOnAccount;
51+
import com.owncloud.android.lib.common.utils.Log_OC;
5052

5153
import java.util.ArrayList;
5254
import java.util.Calendar;
@@ -539,10 +541,23 @@ public Map<Long, Long> getIdMap(long accountId) {
539541
}
540542

541543
@AnyThread
542-
public void toggleFavoriteAndSync(Account account, long noteId) {
544+
public void toggleFavoriteAndSync(Account account, Note note) {
543545
executor.submit(() -> {
544-
db.getNoteDao().toggleFavorite(noteId);
545-
scheduleSync(account, true);
546+
try {
547+
final var ssoAccount = AccountImporter.getSingleSignOnAccount(context, account.getAccountName());
548+
final var notesAPI = apiProvider.getNotesAPI(context, ssoAccount, getPreferredApiVersion(account.getApiVersion()));
549+
note.setFavorite(!note.getFavorite());
550+
final var result = notesAPI.updateNote(note);
551+
final var response = result.execute();
552+
if (response.isSuccessful()) {
553+
final var updatedNote = response.body();
554+
if (updatedNote != null) {
555+
scheduleSync(account, false);
556+
}
557+
}
558+
} catch (Exception e) {
559+
Log_OC.e(TAG, "toggleFavoriteAndSync: " + e);
560+
}
546561
});
547562
}
548563

app/src/main/java/it/niedermann/owncloud/notes/persistence/sync/NotesAPI.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import com.nextcloud.android.sso.api.ParsedResponse;
1818

1919
import java.util.Calendar;
20+
import java.util.HashMap;
2021
import java.util.List;
22+
import java.util.Map;
2123
import java.util.stream.Collectors;
2224

2325
import io.reactivex.Observable;
@@ -106,6 +108,21 @@ public Call<Note> createNote(Note note) {
106108
}
107109
}
108110

111+
public Call<Note> updateNote(@NonNull Note note) {
112+
final Long remoteId = note.getRemoteId();
113+
if (remoteId == null) {
114+
return null;
115+
}
116+
117+
if (ApiVersion.API_VERSION_1_0.equals(usedApiVersion)) {
118+
return notesAPI_1_0.updateNote(remoteId, note);
119+
} else if (ApiVersion.API_VERSION_0_2.equals(usedApiVersion)) {
120+
return notesAPI_0_2.updateNote(remoteId, new Note_0_2(note));
121+
} else {
122+
return null;
123+
}
124+
}
125+
109126
public Call<Note> editNote(@NonNull Note note) {
110127
final Long remoteId = note.getRemoteId();
111128
if (remoteId == null) {

app/src/main/java/it/niedermann/owncloud/notes/persistence/sync/NotesAPI_0_2.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.nextcloud.android.sso.api.ParsedResponse;
1111

1212
import java.util.List;
13+
import java.util.Map;
1314

1415
import io.reactivex.Observable;
1516
import it.niedermann.owncloud.notes.persistence.entity.Note;
@@ -46,4 +47,7 @@ public interface NotesAPI_0_2 {
4647

4748
@DELETE("notes/{remoteId}")
4849
Call<EmptyResponse> deleteNote(@Path("remoteId") long noteId);
50+
51+
@PUT("notes/{id}")
52+
Call<Note> updateNote(@Path("id") long id, @Body NotesAPI.Note_0_2 note);
4953
}

app/src/main/java/it/niedermann/owncloud/notes/persistence/sync/NotesAPI_1_0.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.nextcloud.android.sso.api.ParsedResponse;
1111

1212
import java.util.List;
13+
import java.util.Map;
1314

1415
import io.reactivex.Observable;
1516
import it.niedermann.owncloud.notes.persistence.entity.Note;
@@ -52,4 +53,7 @@ public interface NotesAPI_1_0 {
5253

5354
@PUT("settings")
5455
Call<NotesSettings> putSettings(@Body NotesSettings settings);
56+
57+
@PUT("notes/{id}")
58+
Call<Note> updateNote(@Path("id") long id, @Body Note note);
5559
}

gradle/verification-metadata.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<trust group="com.android.tools.build" name="aapt2" version="8.4.1-11315950" reason="ships OS specific artifacts (win/linux) - temp global trust"/>
88
<trust file=".*-javadoc[.]jar" regex="true" reason="Android Studio downloads javadoc jars but doesn't add checksums - fixes building in AS"/>
99
<trust file=".*-sources[.]jar" regex="true" reason="Android Studio downloads source jars but doesn't add checksums - fixes building in AS"/>
10+
<trust file=".*android-library-2.21.0.module" regex="true" reason="CodeQL fails to resolve this and write it to the metadata"/>
1011
</trusted-artifacts>
1112
<trusted-keys>
1213
<trusted-key id="04543577D6A9CC626239C50C7ECBD740FF06AEB5">

0 commit comments

Comments
 (0)