Use one excerpt for Grid and List layout

This commit is contained in:
Stefan Niedermann 2020-06-10 17:16:58 +02:00
parent 2b71e1b2f8
commit 484fdd5cf3
10 changed files with 34 additions and 41 deletions

View file

@ -146,7 +146,7 @@ public class NotesDatabaseTest {
String newContent = getCurDate() + " This is a even greater day my friend.";
DBNote dbNote = new DBNote(newNoteID, 1, Calendar.getInstance(), "A Greater Day",
newContent, true, "Best Friend's Record", null, DBStatus.VOID,
accountID, NoteUtil.generateNoteExcerpt(newContent, false), 0);
accountID, NoteUtil.generateNoteExcerpt(newContent), 0);
// Add a new note
long noteID = db.addNote(accountID, dbNote);
@ -161,7 +161,7 @@ public class NotesDatabaseTest {
newContent = getCurDate() + " This is a even greater day my friend.";
dbNote = new DBNote(0, 1, Calendar.getInstance(), "An Even Greater Day",
newContent, true, "Sincere Friend's Record", null, DBStatus.VOID,
accountID, NoteUtil.generateNoteExcerpt(newContent, false), 0);
accountID, NoteUtil.generateNoteExcerpt(newContent), 0);
// Add a new note
noteID = db.addNote(accountID, dbNote);
// Check if this note is added successfully

View file

@ -69,12 +69,12 @@ import it.niedermann.owncloud.notes.model.GridItemDecoration;
import it.niedermann.owncloud.notes.model.ISyncCallback;
import it.niedermann.owncloud.notes.model.Item;
import it.niedermann.owncloud.notes.model.ItemAdapter;
import it.niedermann.owncloud.notes.model.SectionItemDecoration;
import it.niedermann.owncloud.notes.model.LocalAccount;
import it.niedermann.owncloud.notes.model.NavigationAdapter;
import it.niedermann.owncloud.notes.model.NavigationAdapter.CategoryNavigationItem;
import it.niedermann.owncloud.notes.model.NavigationAdapter.NavigationItem;
import it.niedermann.owncloud.notes.model.NoteClickListener;
import it.niedermann.owncloud.notes.model.SectionItemDecoration;
import it.niedermann.owncloud.notes.persistence.CapabilitiesClient;
import it.niedermann.owncloud.notes.persistence.CapabilitiesWorker;
import it.niedermann.owncloud.notes.persistence.LoadNotesListTask;
@ -88,6 +88,7 @@ import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static it.niedermann.owncloud.notes.branding.BrandingUtil.getSecondaryForegroundColorDependingOnTheme;
import static it.niedermann.owncloud.notes.util.ColorUtil.contrastRatioIsSufficient;
import static it.niedermann.owncloud.notes.util.Notes.isDarkThemeActive;
import static it.niedermann.owncloud.notes.util.Notes.isGridViewEnabled;
import static it.niedermann.owncloud.notes.util.SSOUtil.askForNewAccount;
import static java.util.Arrays.asList;
@ -185,8 +186,8 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
db = NotesDatabase.getInstance(this);
gridView = isGridViewEnabled(this);
if (!gridView) {
gridView = isGridViewEnabled();
if (!gridView || isDarkThemeActive(this)) {
activityBinding.activityNotesListView.setBackgroundColor(ContextCompat.getColor(this, R.color.primary));
}

View file

@ -66,7 +66,6 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Bra
final Boolean gridView = (Boolean) newValue;
Log.v(TAG, "gridView: " + gridView);
requireActivity().setResult(Activity.RESULT_OK);
new Thread(() -> NotesDatabase.getInstance(requireContext()).regenerateExcerpts(gridView)).start();
Notes.updateGridViewEnabled(gridView);
return true;
});

View file

@ -1,7 +1,7 @@
package it.niedermann.owncloud.notes.model;
import android.content.Context;
import android.util.Log;
import android.text.TextUtils;
import android.view.View;
import androidx.annotation.NonNull;
@ -9,6 +9,10 @@ import androidx.annotation.Nullable;
import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemGridBinding;
import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
public class NoteViewGridHolder extends NoteViewHolder {
@NonNull
private final ItemNotesListNoteItemGridBinding binding;
@ -26,10 +30,11 @@ public class NoteViewGridHolder extends NoteViewHolder {
super.bind(note, showCategory, mainColor, textColor, searchQuery);
@NonNull final Context context = itemView.getContext();
bindCategory(context, binding.noteCategory, showCategory, note.getCategory(), mainColor);
binding.noteStatus.setVisibility(DBStatus.VOID.equals(note.getStatus()) ? View.INVISIBLE : View.VISIBLE);
binding.noteStatus.setVisibility(DBStatus.VOID.equals(note.getStatus()) ? INVISIBLE : VISIBLE);
bindFavorite(binding.noteFavorite, note.isFavorite());
bindSearchableContent(context, binding.noteTitle, searchQuery, note.getTitle(), mainColor);
bindSearchableContent(context, binding.noteContent, searchQuery, note.getExcerpt(), mainColor);
bindSearchableContent(context, binding.noteExcerpt, searchQuery, note.getExcerpt().replace(" ", "\n"), mainColor);
binding.noteExcerpt.setVisibility(TextUtils.isEmpty(note.getExcerpt()) ? GONE : VISIBLE);
}
public View getNoteSwipeable() {

View file

@ -1,6 +1,7 @@
package it.niedermann.owncloud.notes.model;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import androidx.annotation.NonNull;

View file

@ -58,7 +58,6 @@ import static it.niedermann.owncloud.notes.android.activity.EditNoteActivity.ACT
import static it.niedermann.owncloud.notes.android.appwidget.NoteListWidget.updateNoteListWidgets;
import static it.niedermann.owncloud.notes.android.appwidget.SingleNoteWidget.updateSingleNoteWidgets;
import static it.niedermann.owncloud.notes.model.NoteListsWidgetData.MODE_DISPLAY_CATEGORY;
import static it.niedermann.owncloud.notes.util.Notes.isGridViewEnabled;
/**
* Helps to add, get, update and delete Notes with the option to trigger a Resync with the Server.
@ -95,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(), isGridViewEnabled(context)), 0);
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);
@ -122,7 +121,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
} else {
values.put(key_status, DBStatus.VOID.getTitle());
values.put(key_account_id, accountId);
values.put(key_excerpt, NoteUtil.generateNoteExcerpt(note.getContent(), isGridViewEnabled(context)));
values.put(key_excerpt, NoteUtil.generateNoteExcerpt(note.getContent()));
}
if (note.getRemoteId() > 0) {
values.put(key_remote_id, note.getRemoteId());
@ -136,17 +135,6 @@ public class NotesDatabase extends AbstractNotesDatabase {
return db.insert(table_notes, null, values);
}
public void regenerateExcerpts(boolean keepMarkdown) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.query(table_notes, new String[]{key_id, key_content}, key_status + " != ?", new String[]{DBStatus.LOCAL_DELETED.getTitle()}, null, null, null);
ContentValues values = new ContentValues(1);
while (cursor.moveToNext()) {
values.put(key_excerpt, NoteUtil.generateNoteExcerpt(cursor.getString(1), keepMarkdown));
db.update(table_notes, values, key_id + " = ?", new String[]{String.valueOf(cursor.getInt(0))});
}
cursor.close();
}
public void moveNoteToAnotherAccount(SingleSignOnAccount ssoAccount, long oldAccountId, DBNote note, long newAccountId) {
// Add new note
addNoteAndSync(ssoAccount, newAccountId, new CloudNote(0, note.getModified(), note.getTitle(), note.getContent(), note.isFavorite(), note.getCategory(), null));
@ -522,7 +510,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
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(), 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, isGridViewEnabled(context)), oldNote.getScrollY());
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(7);
@ -586,7 +574,7 @@ public class NotesDatabase extends AbstractNotesDatabase {
values.put(key_favorite, remoteNote.isFavorite());
values.put(key_category, getCategoryIdByTitle(localAccount.getId(), remoteNote.getCategory()));
values.put(key_etag, remoteNote.getEtag());
values.put(key_excerpt, NoteUtil.generateNoteExcerpt(remoteNote.getContent(), isGridViewEnabled(context)));
values.put(key_excerpt, NoteUtil.generateNoteExcerpt(remoteNote.getContent()));
String whereClause;
String[] whereArgs;
if (forceUnchangedDBNoteState != null) {

View file

@ -18,7 +18,7 @@ public class Migration_9_10 {
Cursor cursor = db.query("NOTES", new String[]{"ID", "CONTENT"}, null, null, null, null, null, null);
while (cursor.moveToNext()) {
ContentValues values = new ContentValues();
values.put("EXCERPT", NoteUtil.generateNoteExcerpt(cursor.getString(1), false));
values.put("EXCERPT", NoteUtil.generateNoteExcerpt(cursor.getString(1)));
db.update("NOTES", values, "ID" + " = ? ", new String[]{cursor.getString(0)});
}
cursor.close();

View file

@ -81,18 +81,15 @@ public class NoteUtil {
/**
* Generates an excerpt of a content String (reads second line which is not empty)
*
* @param content {@link String}
* @param keepMarkdown whether or not the markdown should be stripped from the excerpt
* @param content String
* @return excerpt String
*/
@NonNull
public static String generateNoteExcerpt(@NonNull String content, boolean keepMarkdown) {
if (!content.contains("\n")) {
public static String generateNoteExcerpt(@NonNull String content) {
if (content.contains("\n"))
return truncateString(removeMarkDown(content.replaceFirst("^.*\n", "")), 200).replace("\n", " ");
else
return "";
}
return keepMarkdown
? truncateString(removeMarkDown(content.replaceFirst("^.*\n", "")), 150)
: truncateString(removeMarkDown(content.replaceFirst("^.*\n", "")), 150).replace("\n", " ");
}
@NonNull

View file

@ -39,11 +39,11 @@ public class Notes extends Application {
AppCompatDelegate.setDefaultNightMode(setting.getModeId());
}
public static boolean isGridViewEnabled(@NonNull Context context) {
public static boolean isGridViewEnabled() {
return isGridViewEnabled;
}
public static void updateGridViewEnabled(Boolean gridView) {
public static void updateGridViewEnabled(boolean gridView) {
isGridViewEnabled = gridView;
}

View file

@ -11,7 +11,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/spacer_2x">
android:paddingBottom="@dimen/spacer_1x">
<LinearLayout
@ -24,8 +24,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingTop="@dimen/spacer_1x">
android:orientation="vertical">
<TextView
android:id="@+id/noteCategory"
@ -33,6 +32,7 @@
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/spacer_1x"
android:layout_marginLeft="@dimen/spacer_1x"
android:layout_marginTop="@dimen/spacer_1x"
android:background="@drawable/border"
android:maxLines="1"
android:paddingLeft="@dimen/spacer_1x"
@ -54,6 +54,7 @@
android:layout_marginTop="@dimen/spacer_1x"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="@dimen/spacer_1x"
android:textAppearance="?attr/textAppearanceHeadline5"
android:textColor="@color/fg_default"
android:textIsSelectable="true"
@ -90,14 +91,15 @@
</FrameLayout>
</LinearLayout>
<com.yydcdut.markdown.MarkdownTextView
android:id="@+id/noteContent"
<TextView
android:id="@+id/noteExcerpt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/spacer_2x"
android:layout_marginLeft="@dimen/spacer_2x"
android:layout_marginEnd="@dimen/spacer_2x"
android:layout_marginRight="@dimen/spacer_2x"
android:layout_marginBottom="@dimen/spacer_1x"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/fg_default"
android:textIsSelectable="true"