Use automatic title generation for v0.2 APIs

This commit is contained in:
Stefan Niedermann 2020-06-07 11:46:45 +02:00
parent 310df67fdc
commit cfb501c4e0
4 changed files with 16 additions and 14 deletions

View file

@ -44,7 +44,7 @@ public class AppendToNoteActivity extends NotesListViewActivity {
} else { } else {
newContent = receivedText; newContent = receivedText;
} }
db.updateNoteAndSync(ssoAccount, localAccount.getId(), note, newContent, () -> Toast.makeText(this, getString(R.string.added_content, receivedText), Toast.LENGTH_SHORT).show()); db.updateNoteAndSync(ssoAccount, localAccount, note, newContent, () -> Toast.makeText(this, getString(R.string.added_content, receivedText), Toast.LENGTH_SHORT).show());
} else { } else {
Toast.makeText(this, R.string.shared_text_empty, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.shared_text_empty, Toast.LENGTH_SHORT).show();
} }

View file

@ -18,8 +18,8 @@ import android.view.MenuItem;
import androidx.annotation.ColorInt; import androidx.annotation.ColorInt;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@ -32,8 +32,8 @@ import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.android.activity.EditNoteActivity; import it.niedermann.owncloud.notes.android.activity.EditNoteActivity;
import it.niedermann.owncloud.notes.android.fragment.CategoryDialogFragment.CategoryDialogListener; import it.niedermann.owncloud.notes.android.fragment.CategoryDialogFragment.CategoryDialogListener;
import it.niedermann.owncloud.notes.android.fragment.EditTitleDialogFragment.EditTitleListener; import it.niedermann.owncloud.notes.android.fragment.EditTitleDialogFragment.EditTitleListener;
import it.niedermann.owncloud.notes.model.ApiVersion;
import it.niedermann.owncloud.notes.branding.BrandedFragment; import it.niedermann.owncloud.notes.branding.BrandedFragment;
import it.niedermann.owncloud.notes.model.ApiVersion;
import it.niedermann.owncloud.notes.model.CloudNote; import it.niedermann.owncloud.notes.model.CloudNote;
import it.niedermann.owncloud.notes.model.DBNote; import it.niedermann.owncloud.notes.model.DBNote;
import it.niedermann.owncloud.notes.model.DBStatus; import it.niedermann.owncloud.notes.model.DBStatus;
@ -170,7 +170,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
MenuItem itemFavorite = menu.findItem(R.id.menu_favorite); MenuItem itemFavorite = menu.findItem(R.id.menu_favorite);
prepareFavoriteOption(itemFavorite); prepareFavoriteOption(itemFavorite);
menu.findItem(R.id.menu_title).setVisible(localAccount.getPreferredApiVersion().compareTo(new ApiVersion("1.0", 0, 0)) >= 0); menu.findItem(R.id.menu_title).setVisible(localAccount.getPreferredApiVersion() != null && localAccount.getPreferredApiVersion().compareTo(new ApiVersion("1.0", 0, 0)) >= 0);
menu.findItem(R.id.menu_delete).setVisible(!isNew); menu.findItem(R.id.menu_delete).setVisible(!isNew);
} }
@ -190,7 +190,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
if (originalNote == null) { if (originalNote == null) {
db.deleteNoteAndSync(ssoAccount, note.getId()); db.deleteNoteAndSync(ssoAccount, note.getId());
} else { } else {
db.updateNoteAndSync(ssoAccount, localAccount.getId(), originalNote, null, null); db.updateNoteAndSync(ssoAccount, localAccount, originalNote, null, null);
} }
listener.close(); listener.close();
return true; return true;
@ -271,7 +271,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
if (note.getContent().equals(newContent)) { if (note.getContent().equals(newContent)) {
Log.v(TAG, "... not saving, since nothing has changed"); Log.v(TAG, "... not saving, since nothing has changed");
} else { } else {
note = db.updateNoteAndSync(ssoAccount, localAccount.getId(), note, newContent, callback); note = db.updateNoteAndSync(ssoAccount, localAccount, note, newContent, callback);
listener.onNoteUpdated(note); listener.onNoteUpdated(note);
requireActivity().invalidateOptionsMenu(); requireActivity().invalidateOptionsMenu();
} }
@ -342,7 +342,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
public void onTitleEdited(String newTitle) { public void onTitleEdited(String newTitle) {
titleModified = true; titleModified = true;
note.setTitle(newTitle); note.setTitle(newTitle);
note = db.updateNoteAndSync(ssoAccount, localAccount.getId(), note, note.getContent(), newTitle, null); note = db.updateNoteAndSync(ssoAccount, localAccount, note, note.getContent(), newTitle, null);
listener.onNoteUpdated(note); listener.onNoteUpdated(note);
} }

View file

@ -24,6 +24,7 @@ public class LocalAccount {
private String etag; private String etag;
private String capabilitiesETag; private String capabilitiesETag;
private long modified; private long modified;
@Nullable
private ApiVersion preferredApiVersion; private ApiVersion preferredApiVersion;
@ColorInt @ColorInt
private int color; private int color;
@ -78,6 +79,7 @@ public class LocalAccount {
this.modified = modified; this.modified = modified;
} }
@Nullable
public ApiVersion getPreferredApiVersion() { public ApiVersion getPreferredApiVersion() {
return preferredApiVersion; return preferredApiVersion;
} }

View file

@ -495,8 +495,8 @@ public class NotesDatabase extends AbstractNotesDatabase {
return db.insert(table_category, null, values); return db.insert(table_category, null, values);
} }
public DBNote updateNoteAndSync(SingleSignOnAccount ssoAccount, long accountId, @NonNull DBNote oldNote, @Nullable String newContent, @Nullable ISyncCallback callback) { public DBNote updateNoteAndSync(SingleSignOnAccount ssoAccount, @NonNull LocalAccount localAccount, @NonNull DBNote oldNote, @Nullable String newContent, @Nullable ISyncCallback callback) {
return updateNoteAndSync(ssoAccount, accountId, oldNote, newContent, null, callback); return updateNoteAndSync(ssoAccount, localAccount, oldNote, newContent, null, callback);
} }
/** /**
@ -509,22 +509,22 @@ public class NotesDatabase extends AbstractNotesDatabase {
* @param callback When the synchronization is finished, this callback will be invoked (optional). * @param callback When the synchronization is finished, this callback will be invoked (optional).
* @return changed note if differs from database, otherwise the old note. * @return changed note if differs from database, otherwise the old note.
*/ */
public DBNote updateNoteAndSync(SingleSignOnAccount ssoAccount, long accountId, @NonNull DBNote oldNote, @Nullable String newContent, @Nullable String newTitle, @Nullable ISyncCallback callback) { public DBNote updateNoteAndSync(SingleSignOnAccount ssoAccount, @NonNull LocalAccount localAccount, @NonNull DBNote oldNote, @Nullable String newContent, @Nullable String newTitle, @Nullable ISyncCallback callback) {
DBNote newNote; DBNote newNote;
if (newContent == null) { if (newContent == null) {
newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED, accountId, oldNote.getExcerpt()); newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED, localAccount.getId(), oldNote.getExcerpt());
} else { } else {
final String title; final String title;
if (newTitle != null) { if (newTitle != null) {
title = newTitle; title = newTitle;
} else { } else {
if (oldNote.getRemoteId() == 0) { if (oldNote.getRemoteId() == 0 || localAccount.getPreferredApiVersion() == null || localAccount.getPreferredApiVersion().compareTo(new ApiVersion("1.0", 0, 0)) < 0) {
title = NoteUtil.generateNonEmptyNoteTitle(newContent, getContext()); title = NoteUtil.generateNonEmptyNoteTitle(newContent, getContext());
} else { } else {
title = oldNote.getTitle(); title = oldNote.getTitle();
} }
} }
newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), Calendar.getInstance(), title, newContent, oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED, accountId, generateNoteExcerpt(newContent, title)); newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), Calendar.getInstance(), title, newContent, oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED, localAccount.getId(), generateNoteExcerpt(newContent, title));
} }
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
@ -535,7 +535,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
values.put(key_content, newNote.getContent()); values.put(key_content, newNote.getContent());
values.put(key_excerpt, newNote.getExcerpt()); values.put(key_excerpt, newNote.getExcerpt());
int rows = db.update(table_notes, values, key_id + " = ? AND (" + key_content + " != ? OR " + key_category + " != ?)", new String[]{String.valueOf(newNote.getId()), newNote.getContent(), newNote.getCategory()}); int rows = db.update(table_notes, values, key_id + " = ? AND (" + key_content + " != ? OR " + key_category + " != ?)", new String[]{String.valueOf(newNote.getId()), newNote.getContent(), newNote.getCategory()});
removeEmptyCategory(accountId); removeEmptyCategory(localAccount.getId());
// if data was changed, set new status and schedule sync (with callback); otherwise invoke callback directly. // if data was changed, set new status and schedule sync (with callback); otherwise invoke callback directly.
if (rows > 0) { if (rows > 0) {
notifyNotesChanged(); notifyNotesChanged();