1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-25 12:32:31 +01: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(
AppDatabase.DATABASE_NAME, Migrations.DB_VER_6,
true, Migrations.MIGRATION_5_6
AppDatabase.DATABASE_NAME,
Migrations.DB_VER_8,
true,
Migrations.MIGRATION_7_8
)
val migratedDatabaseV3 = getMigratedDatabase()
@ -150,10 +152,13 @@ class DatabaseMigrationTest {
}
@Test
fun migrateDatabaseFrom5to6() {
val databaseInV5 = testHelper.createDatabase(AppDatabase.DATABASE_NAME, Migrations.DB_VER_5)
fun migrateDatabaseFrom7to8() {
val databaseInV7 = testHelper.createDatabase(AppDatabase.DATABASE_NAME, Migrations.DB_VER_7)
databaseInV5.run {
val defaultSearch1 = " abc "
val defaultSearch2 = " abc"
databaseInV7.run {
insert(
"search_history", SQLiteDatabase.CONFLICT_FAIL,
ContentValues().apply {
@ -186,12 +191,12 @@ class DatabaseMigrationTest {
}
testHelper.runMigrationsAndValidate(
AppDatabase.DATABASE_NAME, Migrations.DB_VER_6,
true, Migrations.MIGRATION_5_6
AppDatabase.DATABASE_NAME, Migrations.DB_VER_8,
true, Migrations.MIGRATION_7_8
)
val migratedDatabaseV6 = getMigratedDatabase()
val listFromDB = migratedDatabaseV6.searchHistoryDAO().all.blockingFirst()
val migratedDatabaseV8 = getMigratedDatabase()
val listFromDB = migratedDatabaseV8.searchHistoryDAO().all.blockingFirst()
assertEquals(2, listFromDB.size)
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) {
@Override
public void migrate(@NonNull final SupportSQLiteDatabase database) {
database.execSQL("DELETE FROM search_history WHERE id NOT IN (SELECT id FROM "
+ "(SELECT id FROM search_history GROUP BY trim(search), service_id) tmp)");
database.execSQL("DELETE FROM search_history WHERE id NOT IN (SELECT id FROM (SELECT "
+ "MIN(id) as id FROM search_history GROUP BY trim(search), service_id ) tmp)");
database.execSQL("UPDATE search_history SET search = trim(search)");
}
};

View File

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