#227 Remember last scrolling position per note

This commit is contained in:
Stefan Niedermann 2020-06-07 21:00:20 +02:00 committed by Niedermann IT-Dienstleistungen
parent b002cd1787
commit 212ac7f638
5 changed files with 53 additions and 13 deletions

View file

@ -13,6 +13,8 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ScrollView;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
@ -94,7 +96,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
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 DBNote(-1, -1, null, NoteUtil.generateNoteTitle(content), content, false, getString(R.string.category_readonly), null, DBStatus.VOID, -1, "");
note = new DBNote(-1, -1, null, NoteUtil.generateNoteTitle(content), content, false, getString(R.string.category_readonly), null, DBStatus.VOID, -1, "", 0);
}
} else {
note = db.getNote(localAccount.getId(), db.addNoteAndSync(ssoAccount, localAccount.getId(), cloudNote));
@ -111,6 +113,31 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
}
}
@Nullable
protected abstract ScrollView getScrollView();
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final ScrollView scrollView = getScrollView();
if (scrollView != null) {
scrollView.getViewTreeObserver().addOnScrollChangedListener(() -> {
if (scrollView.getScrollY() > 0) {
note.setScrollY(scrollView.getScrollY());
}
});
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final ScrollView scrollView = getScrollView();
if (scrollView != null) {
scrollView.post(() -> scrollView.scrollTo(0, note.getScrollY()));
}
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);

View file

@ -11,7 +11,6 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import androidx.annotation.CallSuper;
import androidx.annotation.ColorInt;
@ -201,8 +200,6 @@ public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {
protected abstract void colorWithText(@NonNull String newText, @Nullable Integer current, int mainColor, int textColor);
protected abstract ScrollView getScrollView();
protected abstract Layout getLayout();
protected abstract FloatingActionButton getSearchNextButton();

View file

@ -15,13 +15,15 @@ public class DBNote extends CloudNote implements Item, Serializable {
private final long accountId;
private DBStatus status;
private String excerpt;
private int scrollY;
public DBNote(long id, long remoteId, Calendar modified, String title, String content, boolean favorite, String category, String etag, DBStatus status, long accountId, String excerpt) {
public DBNote(long id, long remoteId, Calendar modified, String title, String content, boolean favorite, String category, String etag, DBStatus status, long accountId, String excerpt, int scrollY) {
super(remoteId, modified, title, content, favorite, category, etag);
this.id = id;
this.excerpt = excerpt;
this.status = status;
this.accountId = accountId;
this.scrollY = scrollY;
}
public long getId() {
@ -67,4 +69,12 @@ public class DBNote extends CloudNote implements Item, Serializable {
", excerpt='" + excerpt + '\'' +
'}';
}
public int getScrollY() {
return scrollY;
}
public void setScrollY(int scrollY) {
this.scrollY = scrollY;
}
}

View file

@ -25,7 +25,7 @@ import it.niedermann.owncloud.notes.util.DatabaseIndexUtil;
abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
private static final String TAG = AbstractNotesDatabase.class.getSimpleName();
private static final int database_version = 16;
private static final int database_version = 17;
@NonNull
protected final Context context;
@ -61,6 +61,7 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
protected static final String key_category_account_id = "CATEGORY_ACCOUNT_ID";
protected static final String key_theme_mode = "THEME_MODE";
protected static final String key_mode = "MODE";
protected static final String key_scroll_y = "SCROLL_Y";
protected AbstractNotesDatabase(@NonNull Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory) {
super(context, name, factory, database_version);
@ -100,6 +101,7 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
key_category + " INTEGER, " +
key_etag + " TEXT," +
key_excerpt + " TEXT NOT NULL DEFAULT '', " +
key_scroll_y + " INTEGER DEFAULT 0, " +
"FOREIGN KEY(" + key_category + ") REFERENCES " + table_category + "(" + key_category_id + "), " +
"FOREIGN KEY(" + key_account_id + ") REFERENCES " + table_accounts + "(" + key_id + "))");
DatabaseIndexUtil.createIndex(db, table_notes, key_remote_id, key_account_id, key_status, key_favorite, key_category, key_modified);
@ -185,6 +187,9 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
case 15:
new Migration_15_16(db, oldVersion, context, this::notifyWidgets);
}
if(oldVersion < 17) {
db.execSQL("ALTER TABLE NOTES ADD COLUMN SCROLL_Y INTEGER DEFAULT 0");
}
}
@Override

View file

@ -94,7 +94,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
* @param note Note
*/
public long addNoteAndSync(SingleSignOnAccount ssoAccount, long accountId, CloudNote note) {
DBNote dbNote = new DBNote(0, 0, note.getModified(), note.getTitle(), note.getContent(), note.isFavorite(), note.getCategory(), note.getEtag(), DBStatus.LOCAL_EDITED, accountId, NoteUtil.generateNoteExcerpt(note.getContent()));
DBNote dbNote = new DBNote(0, 0, note.getModified(), note.getTitle(), note.getContent(), note.isFavorite(), note.getCategory(), note.getEtag(), DBStatus.LOCAL_EDITED, accountId, NoteUtil.generateNoteExcerpt(note.getContent()), 0);
long id = addNote(accountId, dbNote);
notifyWidgets();
getNoteServerSyncHelper().scheduleSync(ssoAccount, true);
@ -214,8 +214,8 @@ public class NotesDatabase extends AbstractNotesDatabase {
if (selectionArgs.length > 2) {
Log.v(TAG, selection + " ---- " + selectionArgs[0] + " " + selectionArgs[1] + " " + selectionArgs[2]);
}
String cols = String.format("%s, %s, %s, %s, %s, %s, %s, %s, %s",
key_id, key_remote_id, key_status, key_title, key_modified, key_favorite, key_category_title, key_etag, key_excerpt);
String cols = String.format("%s, %s, %s, %s, %s, %s, %s, %s, %s, %s",
key_id, key_remote_id, key_status, key_title, key_modified, key_favorite, key_category_title, key_etag, key_excerpt, key_scroll_y);
if (!pruneContent) {
cols = String.format("%s, %s", cols, key_content);
}
@ -248,13 +248,14 @@ public class NotesDatabase extends AbstractNotesDatabase {
cursor.getLong(1),
modified,
cursor.getString(3),
pruneContent ? "" : cursor.getString(9),
pruneContent ? "" : cursor.getString(10),
cursor.getInt(5) > 0,
cursor.getString(6),
cursor.getString(7),
DBStatus.parse(cursor.getString(2)),
accountId,
cursor.getString(8)
cursor.getString(8),
cursor.getInt(9)
);
}
@ -507,9 +508,9 @@ public class NotesDatabase extends AbstractNotesDatabase {
//debugPrintFullDB();
DBNote newNote;
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, accountId, oldNote.getExcerpt(), oldNote.getScrollY());
} else {
newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), Calendar.getInstance(), NoteUtil.generateNonEmptyNoteTitle(newContent, getContext()), newContent, oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED, accountId, NoteUtil.generateNoteExcerpt(newContent));
newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), Calendar.getInstance(), NoteUtil.generateNonEmptyNoteTitle(newContent, getContext()), newContent, oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED, accountId, NoteUtil.generateNoteExcerpt(newContent), oldNote.getScrollY());
}
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();