Skip to content

Commit

Permalink
add isSharedViaLink
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Feb 6, 2025
1 parent 9787fe9 commit 065b9e2
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
if (content == null) {
throw new IllegalArgumentException(PARAM_NOTE_ID + " is not given, argument " + PARAM_NEWNOTE + " is missing and " + PARAM_CONTENT + " is missing.");
} else {
note = new Note(-1, null, Calendar.getInstance(), NoteUtil.generateNoteTitle(content), content, getString(R.string.category_readonly), false, null, DBStatus.VOID, -1, "", 0);
note = new Note(-1, null, Calendar.getInstance(), NoteUtil.generateNoteTitle(content), content, getString(R.string.category_readonly), false, null, DBStatus.VOID, -1, "", 0, false);
requireActivity().runOnUiThread(() -> onNoteLoaded(note));
requireActivity().invalidateOptionsMenu();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private void launchNewNote() {
if (content == null) {
content = "";
}
final var newNote = new Note(null, Calendar.getInstance(), NoteUtil.generateNonEmptyNoteTitle(content, this), content, categoryTitle, favorite, null);
final var newNote = new Note(null, Calendar.getInstance(), NoteUtil.generateNonEmptyNoteTitle(content, this), content, categoryTitle, favorite, null, false);
fragment = getNewNoteFragment(newNote);
replaceFragment();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public NotesListWidgetData getNoteListWidgetData(int appWidgetId) {
@NonNull
@MainThread
public LiveData<Note> addNoteAndSync(Account account, Note note) {
final var entity = new Note(0, null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), note.getETag(), DBStatus.LOCAL_EDITED, account.getId(), generateNoteExcerpt(note.getContent(), note.getTitle()), 0);
final var entity = new Note(0, null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), note.getETag(), DBStatus.LOCAL_EDITED, account.getId(), generateNoteExcerpt(note.getContent(), note.getTitle()), 0, note.isSharedViaLink());
final var ret = new MutableLiveData<Note>();
executor.submit(() -> ret.postValue(addNote(account.getId(), entity)));
return map(ret, newNote -> {
Expand Down Expand Up @@ -508,7 +508,7 @@ public Note addNote(long accountId, @NonNull Note note) {

@MainThread
public LiveData<Note> moveNoteToAnotherAccount(Account account, @NonNull Note note) {
final var fullNote = new Note(null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), null);
final var fullNote = new Note(null, note.getModified(), note.getTitle(), note.getContent(), note.getCategory(), note.getFavorite(), null, note.isSharedViaLink());
fullNote.setStatus(DBStatus.LOCAL_EDITED);
deleteNoteAndSync(account, note.getId());
return addNoteAndSync(account, fullNote);
Expand Down Expand Up @@ -570,7 +570,7 @@ public Note updateNoteAndSync(@NonNull Account localAccount, @NonNull Note oldNo
// https://github.com/nextcloud/notes-android/issues/1198
@Nullable final Long remoteId = db.getNoteDao().getRemoteId(oldNote.getId());
if (newContent == null) {
newNote = new Note(oldNote.getId(), remoteId, oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), oldNote.getExcerpt(), oldNote.getScrollY());
newNote = new Note(oldNote.getId(), remoteId, oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), oldNote.getExcerpt(), oldNote.getScrollY(), oldNote.isSharedViaLink());
} else {
final String title;
if (newTitle != null) {
Expand All @@ -584,7 +584,7 @@ public Note updateNoteAndSync(@NonNull Account localAccount, @NonNull Note oldNo
title = oldNote.getTitle();
}
}
newNote = new Note(oldNote.getId(), remoteId, Calendar.getInstance(), title, newContent, oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), generateNoteExcerpt(newContent, title), oldNote.getScrollY());
newNote = new Note(oldNote.getId(), remoteId, Calendar.getInstance(), title, newContent, oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), generateNoteExcerpt(newContent, title), oldNote.getScrollY(), oldNote.isSharedViaLink());
}
int rows = db.getNoteDao().updateNote(newNote);
// if data was changed, set new status and schedule sync (with callback); otherwise invoke callback directly.
Expand Down Expand Up @@ -974,4 +974,8 @@ public void addShareEntities(List<ShareEntity> entities) {
public List<ShareEntity> getShareEntities(String path) {
return db.getShareDao().getShareEntities(path);
}

public void updateNote(Note note) {
db.getNoteDao().updateNote(note);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public interface NoteDao {
String getNoteById = "SELECT * FROM NOTE WHERE id = :id";
String count = "SELECT COUNT(*) FROM NOTE WHERE status != 'LOCAL_DELETED' AND accountId = :accountId";
String countFavorites = "SELECT COUNT(*) FROM NOTE WHERE status != 'LOCAL_DELETED' AND accountId = :accountId AND favorite = 1";
String searchRecentByModified = "SELECT id, remoteId, accountId, title, favorite, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) ORDER BY favorite DESC, modified DESC";
String searchRecentLexicographically = "SELECT id, remoteId, accountId, title, favorite, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) ORDER BY favorite DESC, title COLLATE LOCALIZED ASC";
String searchFavoritesByModified = "SELECT id, remoteId, accountId, title, favorite, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND favorite = 1 ORDER BY modified DESC";
String searchFavoritesLexicographically = "SELECT id, remoteId, accountId, title, favorite, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND favorite = 1 ORDER BY title COLLATE LOCALIZED ASC";
String searchUncategorizedByModified = "SELECT id, remoteId, accountId, title, favorite, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND category = '' ORDER BY favorite DESC, modified DESC";
String searchUncategorizedLexicographically = "SELECT id, remoteId, accountId, title, favorite, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND category = '' ORDER BY favorite DESC, title COLLATE LOCALIZED ASC";
String searchCategoryByModified = "SELECT id, remoteId, accountId, title, favorite, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND (category = :category OR category LIKE :category || '/%') ORDER BY category, favorite DESC, modified DESC";
String searchCategoryLexicographically = "SELECT id, remoteId, accountId, title, favorite, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND (category = :category OR category LIKE :category || '/%') ORDER BY category, favorite DESC, title COLLATE LOCALIZED ASC";
String searchRecentByModified = "SELECT id, remoteId, accountId, title, favorite, share_by_link, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) ORDER BY favorite DESC, modified DESC";
String searchRecentLexicographically = "SELECT id, remoteId, accountId, title, favorite, share_by_link, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) ORDER BY favorite DESC, title COLLATE LOCALIZED ASC";
String searchFavoritesByModified = "SELECT id, remoteId, accountId, title, favorite, share_by_link, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND favorite = 1 ORDER BY modified DESC";
String searchFavoritesLexicographically = "SELECT id, remoteId, accountId, title, favorite, share_by_link, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND favorite = 1 ORDER BY title COLLATE LOCALIZED ASC";
String searchUncategorizedByModified = "SELECT id, remoteId, accountId, title, favorite, share_by_link, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND category = '' ORDER BY favorite DESC, modified DESC";
String searchUncategorizedLexicographically = "SELECT id, remoteId, accountId, title, favorite, share_by_link, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND category = '' ORDER BY favorite DESC, title COLLATE LOCALIZED ASC";
String searchCategoryByModified = "SELECT id, remoteId, accountId, title, favorite, share_by_link, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND (category = :category OR category LIKE :category || '/%') ORDER BY category, favorite DESC, modified DESC";
String searchCategoryLexicographically = "SELECT id, remoteId, accountId, title, favorite, share_by_link, excerpt, modified, category, status, '' as eTag, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND (title LIKE :query OR content LIKE :query) AND (category = :category OR category LIKE :category || '/%') ORDER BY category, favorite DESC, title COLLATE LOCALIZED ASC";

@Query(getNoteById)
LiveData<Note> getNoteById$(long id);
Expand Down Expand Up @@ -141,7 +141,7 @@ public interface NoteDao {
* Gets a list of {@link Note} objects with filled {@link Note#id} and {@link Note#remoteId},
* where {@link Note#remoteId} is not <code>null</code>
*/
@Query("SELECT id, remoteId, 0 as accountId, '' as title, 0 as favorite, '' as excerpt, 0 as modified, '' as eTag, 0 as status, '' as category, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND remoteId IS NOT NULL")
@Query("SELECT id, remoteId, 0 as accountId, '' as title, 0 as favorite, 0 as share_by_link, '' as excerpt, 0 as modified, '' as eTag, 0 as status, '' as category, '' as content, 0 as scrollY FROM NOTE WHERE accountId = :accountId AND status != 'LOCAL_DELETED' AND remoteId IS NOT NULL")
List<Note> getRemoteIdAndId(long accountId);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
@Index(name = "IDX_NOTE_ACCOUNTID", value = "accountId"),
@Index(name = "IDX_NOTE_CATEGORY", value = "category"),
@Index(name = "IDX_NOTE_FAVORITE", value = "favorite"),
@Index(name = "IDX_NOTE_IS_SHARED_VIA_LINK", value = "share_by_link"),
@Index(name = "IDX_NOTE_MODIFIED", value = "modified"),
@Index(name = "IDX_NOTE_REMOTEID", value = "remoteId"),
@Index(name = "IDX_NOTE_STATUS", value = "status")
Expand Down Expand Up @@ -81,6 +82,11 @@ public class Note implements Serializable, Item {
@ColumnInfo(defaultValue = "0")
private boolean favorite = false;

@Expose
@ColumnInfo(defaultValue = "0", name = "share_by_link")
@SerializedName("share_by_link")
private boolean isSharedViaLink = false;

@Expose
@Nullable
@SerializedName("etag")
Expand All @@ -98,19 +104,20 @@ public Note() {
}

@Ignore
public Note(@Nullable Long remoteId, @Nullable Calendar modified, @NonNull String title, @NonNull String content, @NonNull String category, boolean favorite, @Nullable String eTag) {
public Note(@Nullable Long remoteId, @Nullable Calendar modified, @NonNull String title, @NonNull String content, @NonNull String category, boolean favorite, @Nullable String eTag, boolean isSharedViaLink) {
this.remoteId = remoteId;
this.title = title;
this.modified = modified;
this.content = content;
this.favorite = favorite;
this.category = category;
this.eTag = eTag;
this.isSharedViaLink = isSharedViaLink;
}

@Ignore
public Note(long id, @Nullable Long remoteId, @Nullable Calendar modified, @NonNull String title, @NonNull String content, @NonNull String category, boolean favorite, @Nullable String etag, @NonNull DBStatus status, long accountId, @NonNull String excerpt, int scrollY) {
this(remoteId, modified, title, content, category, favorite, etag);
public Note(long id, @Nullable Long remoteId, @Nullable Calendar modified, @NonNull String title, @NonNull String content, @NonNull String category, boolean favorite, @Nullable String etag, @NonNull DBStatus status, long accountId, @NonNull String excerpt, int scrollY, boolean isSharedViaLink) {
this(remoteId, modified, title, content, category, favorite, etag, isSharedViaLink);
this.id = id;
this.status = status;
this.accountId = accountId;
Expand All @@ -126,6 +133,14 @@ public void setId(long id) {
this.id = id;
}

public boolean isSharedViaLink() {
return isSharedViaLink;
}

public void setIsSharedViaLink(boolean value) {
this.isSharedViaLink = value;
}

@NonNull
public String getCategory() {
return category;
Expand Down Expand Up @@ -230,6 +245,7 @@ public boolean equals(Object o) {
if (id != note.id) return false;
if (accountId != note.accountId) return false;
if (favorite != note.favorite) return false;
if (isSharedViaLink != note.isSharedViaLink) return false;
if (scrollY != note.scrollY) return false;
if (!Objects.equals(remoteId, note.remoteId))
return false;
Expand All @@ -254,6 +270,7 @@ public int hashCode() {
result = 31 * result + (modified != null ? modified.hashCode() : 0);
result = 31 * result + content.hashCode();
result = 31 * result + (favorite ? 1 : 0);
result = 31 * result + (isSharedViaLink ? 1 : 0);
result = 31 * result + (eTag != null ? eTag.hashCode() : 0);
result = 31 * result + excerpt.hashCode();
result = 31 * result + scrollY;
Expand All @@ -273,6 +290,7 @@ public String toString() {
", modified=" + modified +
", content='" + content + '\'' +
", favorite=" + favorite +
", share_by_link=" + isSharedViaLink +
", eTag='" + eTag + '\'' +
", excerpt='" + excerpt + '\'' +
", scrollY=" + scrollY +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ public void createPublicShareLink() {
executorService.schedule(() -> {
final var result = repository.addShare(note, ShareType.PUBLIC_LINK, "", "false", "", 0, "");
if (result != null) {
note.setIsSharedViaLink(true);
repository.updateNote(note);
runOnUiThread(this::recreate);
}
}, 0, TimeUnit.MICROSECONDS);
Expand Down Expand Up @@ -313,15 +315,9 @@ private String createInternalLink() {

@Override
public void copyLink(OCShare share) {
/*
if (file.isSharedViaLink()) {
if (TextUtils.isEmpty(share.getShareLink())) {
fileOperationsHelper.getFileWithLink(file, viewThemeUtils);
} else {
ClipboardUtil.copyToClipboard(this, share.getShareLink());
}
if (!note.isSharedViaLink()) {
return;
}
*/

if (TextUtils.isEmpty(share.getShareLink())) {
copyAndShareFileLink(share.getShareLink());
Expand Down Expand Up @@ -506,7 +502,7 @@ public void sendNewEmail(OCShare share) {
@Override
public void unShare(OCShare share) {
executorService.schedule(() -> {
final var result = repository.removeShare(share.getId());
final var result = repository.removeShare(share, note);

runOnUiThread(() -> {
if (result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,19 @@ class ShareRepository(private val applicationContext: Context, private val accou
}
}

fun removeShare(
shareId: Long
): Boolean {
fun removeShare(share: OCShare, note: Note): Boolean {
val shareAPI = apiProvider.getShareAPI(applicationContext, account)

return try {
val call = shareAPI.removeShare(shareId)
val call = shareAPI.removeShare(share.id)
val response = call.execute()
if (response.isSuccessful) {

if (share.shareType == ShareType.PUBLIC_LINK) {
note.setIsSharedViaLink(false)
updateNote(note)
}

Log_OC.d(tag, "Share removed successfully.")
} else {
Log_OC.d(tag, "Failed to remove share: ${response.errorBody()?.string()}")
Expand Down Expand Up @@ -232,6 +236,8 @@ class ShareRepository(private val applicationContext: Context, private val accou
}
}

fun updateNote(note: Note) = notesRepository.updateNote(note)

fun addShare(
note: Note,
shareType: ShareType,
Expand Down

0 comments on commit 065b9e2

Please sign in to comment.