Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
} else if (itemId == R.id.menu_favorite) {
note.setFavorite(!note.getFavorite());
repo.toggleFavoriteAndSync(localAccount, note.getId());
repo.toggleFavoriteAndSync(localAccount, note);
listener.onNoteUpdated(note);
prepareFavoriteOption(item);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,15 @@ public void onNoteClick(int position, View v) {

@Override
public void onNoteFavoriteClick(int position, View view) {
final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(((Note) adapter.getItem(position)).getId());
toggleLiveData.observe(this, (next) -> toggleLiveData.removeObservers(this));
if (!(adapter.getItem(position) instanceof Note note)) {
return;
}

final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(note);
toggleLiveData.observe(this, (next) -> {{
toggleLiveData.removeObservers(this);
adapter.notifyItemChanged(position);
}});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,16 +513,15 @@ public LiveData<Note> moveNoteToAnotherAccount(Account account, long noteId) {
});
}

public LiveData<Void> toggleFavoriteAndSync(long noteId) {
return switchMap(getCurrentAccount(), currentAccount -> {
if (currentAccount == null) {
return new MutableLiveData<>(null);
} else {
Log.v(TAG, "[toggleFavoriteAndSync] - currentAccount: " + currentAccount.getAccountName());
repo.toggleFavoriteAndSync(currentAccount, noteId);
return new MutableLiveData<>(null);
}
});
public LiveData<Void> toggleFavoriteAndSync(Note note) {
final var currentAccount = getCurrentAccount().getValue();

if (currentAccount != null) {
Log.v(TAG, "[toggleFavoriteAndSync] - currentAccount: " + currentAccount.getAccountName());
repo.toggleFavoriteAndSync(currentAccount, note);
}

return new MutableLiveData<>(null);
}

public LiveData<Void> deleteNoteAndSync(long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction)
case ItemTouchHelper.RIGHT -> {
viewHolder.setIsRecyclable(false);
final var adapterNote = (Note) adapter.getItem(viewHolder.getLayoutPosition());
final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(adapterNote.getId());
final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(adapterNote);
toggleLiveData.observe(lifecycleOwner, (next) -> toggleLiveData.removeObservers(lifecycleOwner));
}
default -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static androidx.lifecycle.Transformations.map;
import static java.util.stream.Collectors.toMap;
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT;
import static it.niedermann.owncloud.notes.shared.util.ApiVersionUtil.getPreferredApiVersion;
import static it.niedermann.owncloud.notes.shared.util.NoteUtil.generateNoteExcerpt;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets;
import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.updateSingleNoteWidgets;
Expand Down Expand Up @@ -47,6 +48,7 @@
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import com.owncloud.android.lib.common.utils.Log_OC;

import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -539,10 +541,23 @@ public Map<Long, Long> getIdMap(long accountId) {
}

@AnyThread
public void toggleFavoriteAndSync(Account account, long noteId) {
public void toggleFavoriteAndSync(Account account, Note note) {
executor.submit(() -> {
db.getNoteDao().toggleFavorite(noteId);
scheduleSync(account, true);
try {
final var ssoAccount = AccountImporter.getSingleSignOnAccount(context, account.getAccountName());
final var notesAPI = apiProvider.getNotesAPI(context, ssoAccount, getPreferredApiVersion(account.getApiVersion()));
note.setFavorite(!note.getFavorite());
final var result = notesAPI.updateNote(note);
final var response = result.execute();
if (response.isSuccessful()) {
final var updatedNote = response.body();
if (updatedNote != null) {
scheduleSync(account, false);
}
}
} catch (Exception e) {
Log_OC.e(TAG, "toggleFavoriteAndSync: " + e);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import com.nextcloud.android.sso.api.ParsedResponse;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.reactivex.Observable;
Expand Down Expand Up @@ -106,6 +108,21 @@ public Call<Note> createNote(Note note) {
}
}

public Call<Note> updateNote(@NonNull Note note) {
final Long remoteId = note.getRemoteId();
if (remoteId == null) {
return null;
}

if (ApiVersion.API_VERSION_1_0.equals(usedApiVersion)) {
return notesAPI_1_0.updateNote(remoteId, note);
} else if (ApiVersion.API_VERSION_0_2.equals(usedApiVersion)) {
return notesAPI_0_2.updateNote(remoteId, new Note_0_2(note));
} else {
return null;
}
}

public Call<Note> editNote(@NonNull Note note) {
final Long remoteId = note.getRemoteId();
if (remoteId == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.nextcloud.android.sso.api.ParsedResponse;

import java.util.List;
import java.util.Map;

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

@DELETE("notes/{remoteId}")
Call<EmptyResponse> deleteNote(@Path("remoteId") long noteId);

@PUT("notes/{id}")
Call<Note> updateNote(@Path("id") long id, @Body NotesAPI.Note_0_2 note);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.nextcloud.android.sso.api.ParsedResponse;

import java.util.List;
import java.util.Map;

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

@PUT("settings")
Call<NotesSettings> putSettings(@Body NotesSettings settings);

@PUT("notes/{id}")
Call<Note> updateNote(@Path("id") long id, @Body Note note);
}
1 change: 1 addition & 0 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<trust group="com.android.tools.build" name="aapt2" version="8.4.1-11315950" reason="ships OS specific artifacts (win/linux) - temp global trust"/>
<trust file=".*-javadoc[.]jar" regex="true" reason="Android Studio downloads javadoc jars but doesn't add checksums - fixes building in AS"/>
<trust file=".*-sources[.]jar" regex="true" reason="Android Studio downloads source jars but doesn't add checksums - fixes building in AS"/>
<trust file=".*android-library-2.21.0.module" regex="true" reason="CodeQL fails to resolve this and write it to the metadata"/>
</trusted-artifacts>
<trusted-keys>
<trusted-key id="04543577D6A9CC626239C50C7ECBD740FF06AEB5">
Expand Down
Loading