Skip to content

Commit fa020d5

Browse files
committed
use remote id
Signed-off-by: alperozturk <[email protected]>
1 parent a4c6f0f commit fa020d5

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
114114
}
115115
isNew = false;
116116
note = originalNote = repo.getNoteById(id);
117+
if (note == null) {
118+
Log_OC.d(TAG, "remoteNoteId will be used to get note");
119+
note = repo.getNoteByRemoteId(id);
120+
}
117121
requireActivity().runOnUiThread(() -> onNoteLoaded(note));
118122
requireActivity().invalidateOptionsMenu();
119123
} else {
@@ -278,21 +282,35 @@ public boolean onOptionsItemSelected(MenuItem item) {
278282
shareNote();
279283
return false;
280284
} else if (itemId == MENU_ID_PIN) {
281-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
282-
final var context = requireContext();
283-
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
284-
final var pinShortcutInfo = new ShortcutInfoCompat.Builder(context, String.valueOf(note.getId()))
285-
.setShortLabel(note.getTitle())
286-
.setIcon(IconCompat.createWithResource(context.getApplicationContext(), TRUE.equals(note.getFavorite()) ? R.drawable.ic_star_yellow_24dp : R.drawable.ic_star_border_grey_ccc_24dp))
287-
.setIntent(new Intent(getActivity(), EditNoteActivity.class).putExtra(EditNoteActivity.PARAM_NOTE_ID, note.getId()).setAction(ACTION_SHORTCUT))
288-
.build();
289-
290-
ShortcutManagerCompat.requestPinShortcut(context, pinShortcutInfo, PendingIntent.getBroadcast(context, 0, ShortcutManagerCompat.createShortcutResultIntent(context, pinShortcutInfo), pendingIntentFlagCompat(0)).getIntentSender());
291-
} else {
292-
Log.i(TAG, "RequestPinShortcut is not supported");
293-
}
285+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
286+
return true;
294287
}
295288

289+
if (!ShortcutManagerCompat.isRequestPinShortcutSupported(requireContext())) {
290+
Log.i(TAG, "RequestPinShortcut is not supported");
291+
return true;
292+
}
293+
294+
final var iconId = note.getFavorite() ? R.drawable.ic_star_yellow_24dp : R.drawable.ic_star_border_grey_ccc_24dp;
295+
final var icon = IconCompat.createWithResource(requireContext().getApplicationContext(), iconId);
296+
final var intent = new Intent(getActivity(), EditNoteActivity.class)
297+
.putExtra(EditNoteActivity.PARAM_NOTE_ID, note.getRemoteId())
298+
.setAction(ACTION_SHORTCUT);
299+
final var noteId = String.valueOf(note.getRemoteId());
300+
301+
final var pinShortcutInfo = new ShortcutInfoCompat.Builder(requireContext(), noteId)
302+
.setShortLabel(note.getTitle())
303+
.setIcon(icon)
304+
.setIntent(intent)
305+
.build();
306+
307+
final var broadcastIntent = ShortcutManagerCompat.createShortcutResultIntent(requireContext(), pinShortcutInfo);
308+
final var intentFlag = pendingIntentFlagCompat(0);
309+
final var intentSender = PendingIntent
310+
.getBroadcast(requireContext(), 0, broadcastIntent, intentFlag)
311+
.getIntentSender();
312+
ShortcutManagerCompat.requestPinShortcut(requireContext(), pinShortcutInfo, intentSender);
313+
296314
return true;
297315
}
298316
return super.onOptionsItemSelected(item);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ public Note getNoteById(long id) {
355355
return db.getNoteDao().getNoteById(id);
356356
}
357357

358+
public Note getNoteByRemoteId(long id) {
359+
return db.getNoteDao().getNoteByRemoteId(id);
360+
}
361+
358362
public LiveData<Integer> count$(long accountId) {
359363
return db.getNoteDao().count$(accountId);
360364
}

app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public interface NoteDao {
5353
@Query(getNoteById)
5454
Note getNoteById(long id);
5555

56+
@Query("SELECT * FROM NOTE WHERE remoteId = :id")
57+
Note getNoteByRemoteId(long id);
58+
5659
@Query("SELECT remoteId FROM NOTE WHERE id = :id")
5760
Long getRemoteId(long id);
5861

0 commit comments

Comments
 (0)