1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-01-03 14:00:32 +00:00

Fix database migration and string trimming

Co-authored-by:  Yingwei Zheng <dtcxzyw@qq.com>
This commit is contained in:
TobiGr 2023-08-16 22:01:58 +02:00
parent 90f0809029
commit 4af5b5f6f2
3 changed files with 21 additions and 23 deletions

View File

@ -111,8 +111,10 @@ class DatabaseMigrationTest {
) )
testHelper.runMigrationsAndValidate( testHelper.runMigrationsAndValidate(
AppDatabase.DATABASE_NAME, Migrations.DB_VER_6, AppDatabase.DATABASE_NAME,
true, Migrations.MIGRATION_5_6 Migrations.DB_VER_8,
true,
Migrations.MIGRATION_7_8
) )
val migratedDatabaseV3 = getMigratedDatabase() val migratedDatabaseV3 = getMigratedDatabase()
@ -150,10 +152,13 @@ class DatabaseMigrationTest {
} }
@Test @Test
fun migrateDatabaseFrom5to6() { fun migrateDatabaseFrom7to8() {
val databaseInV5 = testHelper.createDatabase(AppDatabase.DATABASE_NAME, Migrations.DB_VER_5) val databaseInV7 = testHelper.createDatabase(AppDatabase.DATABASE_NAME, Migrations.DB_VER_7)
databaseInV5.run { val defaultSearch1 = " abc "
val defaultSearch2 = " abc"
databaseInV7.run {
insert( insert(
"search_history", SQLiteDatabase.CONFLICT_FAIL, "search_history", SQLiteDatabase.CONFLICT_FAIL,
ContentValues().apply { ContentValues().apply {
@ -186,12 +191,12 @@ class DatabaseMigrationTest {
} }
testHelper.runMigrationsAndValidate( testHelper.runMigrationsAndValidate(
AppDatabase.DATABASE_NAME, Migrations.DB_VER_6, AppDatabase.DATABASE_NAME, Migrations.DB_VER_8,
true, Migrations.MIGRATION_5_6 true, Migrations.MIGRATION_7_8
) )
val migratedDatabaseV6 = getMigratedDatabase() val migratedDatabaseV8 = getMigratedDatabase()
val listFromDB = migratedDatabaseV6.searchHistoryDAO().all.blockingFirst() val listFromDB = migratedDatabaseV8.searchHistoryDAO().all.blockingFirst()
assertEquals(2, listFromDB.size) assertEquals(2, listFromDB.size)
assertEquals("abc", listFromDB[0].search) assertEquals("abc", listFromDB[0].search)

View File

@ -239,8 +239,8 @@ public final class Migrations {
public static final Migration MIGRATION_7_8 = new Migration(DB_VER_7, DB_VER_8) { public static final Migration MIGRATION_7_8 = new Migration(DB_VER_7, DB_VER_8) {
@Override @Override
public void migrate(@NonNull final SupportSQLiteDatabase database) { public void migrate(@NonNull final SupportSQLiteDatabase database) {
database.execSQL("DELETE FROM search_history WHERE id NOT IN (SELECT id FROM " database.execSQL("DELETE FROM search_history WHERE id NOT IN (SELECT id FROM (SELECT "
+ "(SELECT id FROM search_history GROUP BY trim(search), service_id) tmp)"); + "MIN(id) as id FROM search_history GROUP BY trim(search), service_id ) tmp)");
database.execSQL("UPDATE search_history SET search = trim(search)"); database.execSQL("UPDATE search_history SET search = trim(search)");
} }
}; };

View File

@ -1,6 +1,7 @@
package org.schabi.newpipe.fragments.list.search; package org.schabi.newpipe.fragments.list.search;
import static androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags; import static androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags;
import static org.schabi.newpipe.extractor.utils.Utils.isBlank;
import static org.schabi.newpipe.ktx.ViewUtils.animate; import static org.schabi.newpipe.ktx.ViewUtils.animate;
import static org.schabi.newpipe.util.ExtractorHelper.showMetaInfoInTextView; import static org.schabi.newpipe.util.ExtractorHelper.showMetaInfoInTextView;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
@ -398,7 +399,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
@Override @Override
public void reloadContent() { public void reloadContent() {
if (!TextUtils.isEmpty(searchString) || (searchEditText != null if (!TextUtils.isEmpty(searchString) || (searchEditText != null
&& TextUtils.getTrimmedLength(searchEditText.getText()) > 0)) { && !isBlank(searchEditText.getText().toString()))) {
search(!TextUtils.isEmpty(searchString) search(!TextUtils.isEmpty(searchString)
? searchString ? searchString
: searchEditText.getText().toString(), this.contentFilter, ""); : searchEditText.getText().toString(), this.contentFilter, "");
@ -496,7 +497,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
searchEditText.setText(searchString); searchEditText.setText(searchString);
if (TextUtils.isEmpty(searchString) if (TextUtils.isEmpty(searchString)
|| TextUtils.getTrimmedLength(searchEditText.getText()) == 0) { || isBlank(searchEditText.getText().toString())) {
searchToolbarContainer.setTranslationX(100); searchToolbarContainer.setTranslationX(100);
searchToolbarContainer.setAlpha(0.0f); searchToolbarContainer.setAlpha(0.0f);
searchToolbarContainer.setVisibility(View.VISIBLE); searchToolbarContainer.setVisibility(View.VISIBLE);
@ -520,7 +521,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "onClick() called with: v = [" + v + "]"); Log.d(TAG, "onClick() called with: v = [" + v + "]");
} }
if (TextUtils.getTrimmedLength(searchEditText.getText()) == 0) { if (isBlank(searchEditText.getText().toString())) {
NavigationHelper.gotoMainFragment(getFM()); NavigationHelper.gotoMainFragment(getFM());
return; return;
} }
@ -582,12 +583,9 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
searchEditText.removeTextChangedListener(textWatcher); searchEditText.removeTextChangedListener(textWatcher);
} }
textWatcher = new TextWatcher() { textWatcher = new TextWatcher() {
private boolean isPastedText = false;
@Override @Override
public void beforeTextChanged(final CharSequence s, final int start, public void beforeTextChanged(final CharSequence s, final int start,
final int count, final int after) { final int count, final int after) {
isPastedText = TextUtils.isEmpty(s) && after > 1;
} }
@Override @Override
@ -604,11 +602,6 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
final String newText = searchEditText.getText().toString().trim(); final String newText = searchEditText.getText().toString().trim();
suggestionPublisher.onNext(newText); suggestionPublisher.onNext(newText);
if (isPastedText) {
// trim pasted text
searchEditText.setText(newText);
}
} }
}; };
searchEditText.addTextChangedListener(textWatcher); searchEditText.addTextChangedListener(textWatcher);
@ -817,7 +810,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
Log.d(TAG, "search() called with: query = [" + theSearchString + "]"); Log.d(TAG, "search() called with: query = [" + theSearchString + "]");
final String trimmedSearchString = theSearchString.trim(); final String trimmedSearchString = theSearchString.trim();
if (!trimmedSearchString.equals(theSearchString)) { if (!trimmedSearchString.equals(theSearchString)) {
Log.d(TAG, "The precondition is not satisfied. " Log.w(TAG, "The precondition is not satisfied. "
+ "\"theSearchString\" is not allowed to have leading or trailing spaces"); + "\"theSearchString\" is not allowed to have leading or trailing spaces");
} }
} }