Skip to content

Commit 4af5b5f

Browse files
TobiGrdtcxzyw
andcommitted
Fix database migration and string trimming
Co-authored-by: Yingwei Zheng <dtcxzyw@qq.com>
1 parent 90f0809 commit 4af5b5f

3 files changed

Lines changed: 21 additions & 23 deletions

File tree

app/src/androidTest/java/org/schabi/newpipe/database/DatabaseMigrationTest.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ class DatabaseMigrationTest {
111111
)
112112

113113
testHelper.runMigrationsAndValidate(
114-
AppDatabase.DATABASE_NAME, Migrations.DB_VER_6,
115-
true, Migrations.MIGRATION_5_6
114+
AppDatabase.DATABASE_NAME,
115+
Migrations.DB_VER_8,
116+
true,
117+
Migrations.MIGRATION_7_8
116118
)
117119

118120
val migratedDatabaseV3 = getMigratedDatabase()
@@ -150,10 +152,13 @@ class DatabaseMigrationTest {
150152
}
151153

152154
@Test
153-
fun migrateDatabaseFrom5to6() {
154-
val databaseInV5 = testHelper.createDatabase(AppDatabase.DATABASE_NAME, Migrations.DB_VER_5)
155+
fun migrateDatabaseFrom7to8() {
156+
val databaseInV7 = testHelper.createDatabase(AppDatabase.DATABASE_NAME, Migrations.DB_VER_7)
157+
158+
val defaultSearch1 = " abc "
159+
val defaultSearch2 = " abc"
155160

156-
databaseInV5.run {
161+
databaseInV7.run {
157162
insert(
158163
"search_history", SQLiteDatabase.CONFLICT_FAIL,
159164
ContentValues().apply {
@@ -186,12 +191,12 @@ class DatabaseMigrationTest {
186191
}
187192

188193
testHelper.runMigrationsAndValidate(
189-
AppDatabase.DATABASE_NAME, Migrations.DB_VER_6,
190-
true, Migrations.MIGRATION_5_6
194+
AppDatabase.DATABASE_NAME, Migrations.DB_VER_8,
195+
true, Migrations.MIGRATION_7_8
191196
)
192197

193-
val migratedDatabaseV6 = getMigratedDatabase()
194-
val listFromDB = migratedDatabaseV6.searchHistoryDAO().all.blockingFirst()
198+
val migratedDatabaseV8 = getMigratedDatabase()
199+
val listFromDB = migratedDatabaseV8.searchHistoryDAO().all.blockingFirst()
195200

196201
assertEquals(2, listFromDB.size)
197202
assertEquals("abc", listFromDB[0].search)

app/src/main/java/org/schabi/newpipe/database/Migrations.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ public void migrate(@NonNull final SupportSQLiteDatabase database) {
239239
public static final Migration MIGRATION_7_8 = new Migration(DB_VER_7, DB_VER_8) {
240240
@Override
241241
public void migrate(@NonNull final SupportSQLiteDatabase database) {
242-
database.execSQL("DELETE FROM search_history WHERE id NOT IN (SELECT id FROM "
243-
+ "(SELECT id FROM search_history GROUP BY trim(search), service_id) tmp)");
242+
database.execSQL("DELETE FROM search_history WHERE id NOT IN (SELECT id FROM (SELECT "
243+
+ "MIN(id) as id FROM search_history GROUP BY trim(search), service_id ) tmp)");
244244
database.execSQL("UPDATE search_history SET search = trim(search)");
245245
}
246246
};

app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.schabi.newpipe.fragments.list.search;
22

33
import static androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags;
4+
import static org.schabi.newpipe.extractor.utils.Utils.isBlank;
45
import static org.schabi.newpipe.ktx.ViewUtils.animate;
56
import static org.schabi.newpipe.util.ExtractorHelper.showMetaInfoInTextView;
67
import static java.util.Arrays.asList;
@@ -398,7 +399,7 @@ public void onSaveInstanceState(@NonNull final Bundle bundle) {
398399
@Override
399400
public void reloadContent() {
400401
if (!TextUtils.isEmpty(searchString) || (searchEditText != null
401-
&& TextUtils.getTrimmedLength(searchEditText.getText()) > 0)) {
402+
&& !isBlank(searchEditText.getText().toString()))) {
402403
search(!TextUtils.isEmpty(searchString)
403404
? searchString
404405
: searchEditText.getText().toString(), this.contentFilter, "");
@@ -496,7 +497,7 @@ private void showSearchOnStart() {
496497
searchEditText.setText(searchString);
497498

498499
if (TextUtils.isEmpty(searchString)
499-
|| TextUtils.getTrimmedLength(searchEditText.getText()) == 0) {
500+
|| isBlank(searchEditText.getText().toString())) {
500501
searchToolbarContainer.setTranslationX(100);
501502
searchToolbarContainer.setAlpha(0.0f);
502503
searchToolbarContainer.setVisibility(View.VISIBLE);
@@ -520,7 +521,7 @@ private void initSearchListeners() {
520521
if (DEBUG) {
521522
Log.d(TAG, "onClick() called with: v = [" + v + "]");
522523
}
523-
if (TextUtils.getTrimmedLength(searchEditText.getText()) == 0) {
524+
if (isBlank(searchEditText.getText().toString())) {
524525
NavigationHelper.gotoMainFragment(getFM());
525526
return;
526527
}
@@ -582,12 +583,9 @@ public void onSuggestionItemLongClick(final SuggestionItem item) {
582583
searchEditText.removeTextChangedListener(textWatcher);
583584
}
584585
textWatcher = new TextWatcher() {
585-
private boolean isPastedText = false;
586-
587586
@Override
588587
public void beforeTextChanged(final CharSequence s, final int start,
589588
final int count, final int after) {
590-
isPastedText = TextUtils.isEmpty(s) && after > 1;
591589
}
592590

593591
@Override
@@ -604,11 +602,6 @@ public void afterTextChanged(final Editable s) {
604602

605603
final String newText = searchEditText.getText().toString().trim();
606604
suggestionPublisher.onNext(newText);
607-
608-
if (isPastedText) {
609-
// trim pasted text
610-
searchEditText.setText(newText);
611-
}
612605
}
613606
};
614607
searchEditText.addTextChangedListener(textWatcher);
@@ -817,7 +810,7 @@ private void search(final String theSearchString,
817810
Log.d(TAG, "search() called with: query = [" + theSearchString + "]");
818811
final String trimmedSearchString = theSearchString.trim();
819812
if (!trimmedSearchString.equals(theSearchString)) {
820-
Log.d(TAG, "The precondition is not satisfied. "
813+
Log.w(TAG, "The precondition is not satisfied. "
821814
+ "\"theSearchString\" is not allowed to have leading or trailing spaces");
822815
}
823816
}

0 commit comments

Comments
 (0)