Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

History cache compose #11493

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {
id "kotlin-parcelize"
id "checkstyle"
id "org.sonarqube" version "4.0.0.2929"
id "dagger.hilt.android.plugin"
}

android {
Expand Down Expand Up @@ -92,6 +93,7 @@ android {

buildFeatures {
viewBinding true
compose true
}

packagingOptions {
Expand All @@ -103,6 +105,10 @@ android {
'META-INF/COPYRIGHT']
}
}

composeOptions {
kotlinCompilerExtensionVersion = "1.5.3"
}
}

ext {
Expand All @@ -117,6 +123,8 @@ ext {
googleAutoServiceVersion = '1.1.1'
groupieVersion = '2.10.1'
markwonVersion = '4.6.2'
hiltVersion = '1.2.0'
daggerVersion = '2.51.1'

leakCanaryVersion = '2.12'
stethoVersion = '1.6.0'
Expand Down Expand Up @@ -284,6 +292,20 @@ dependencies {
// Date and time formatting
implementation "org.ocpsoft.prettytime:prettytime:5.0.8.Final"

// Jetpack Compose
implementation(platform('androidx.compose:compose-bom:2024.02.01'))
implementation 'androidx.compose.material3:material3'
implementation 'androidx.activity:activity-compose'
implementation 'androidx.compose.ui:ui-tooling-preview'

// hilt
implementation("androidx.hilt:hilt-navigation-compose:${hiltVersion}")
kapt("androidx.hilt:hilt-compiler:${hiltVersion}")

// dagger
implementation("com.google.dagger:hilt-android:${daggerVersion}")
kapt("com.google.dagger:hilt-android-compiler:${daggerVersion}")

/** Debugging **/
// Memory leak detection
debugImplementation "com.squareup.leakcanary:leakcanary-object-watcher-android:${leakCanaryVersion}"
Expand All @@ -293,6 +315,9 @@ dependencies {
debugImplementation "com.facebook.stetho:stetho:${stethoVersion}"
debugImplementation "com.facebook.stetho:stetho-okhttp3:${stethoVersion}"

// Jetpack Compose
debugImplementation 'androidx.compose.ui:ui-tooling'

/** Testing **/
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:5.6.0'
Expand All @@ -301,6 +326,10 @@ dependencies {
androidTestImplementation "androidx.test:runner:1.5.2"
androidTestImplementation "androidx.room:room-testing:${androidxRoomVersion}"
androidTestImplementation "org.assertj:assertj-core:3.24.2"

// dagger
testImplementation("com.google.dagger:hilt-android-testing:${daggerVersion}")
androidTestImplementation("com.google.dagger:hilt-android-testing:${daggerVersion}")
}

static String getGitWorkingBranch() {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/schabi/newpipe/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Objects;

import dagger.hilt.android.HiltAndroidApp;
import io.reactivex.rxjava3.exceptions.CompositeException;
import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
import io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException;
Expand All @@ -57,6 +58,7 @@
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/

@HiltAndroidApp
public class App extends Application {
public static final String PACKAGE_NAME = BuildConfig.APPLICATION_ID;
private static final String TAG = App.class.toString();
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
import java.util.List;
import java.util.Objects;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@SuppressWarnings("ConstantConditions")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.schabi.newpipe.dependency_injection

import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import org.schabi.newpipe.error.usecases.OpenErrorActivity
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun provideSharedPreferences(@ApplicationContext context: Context): SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context)

@Provides
@Singleton
fun provideOpenActivity(
@ApplicationContext context: Context,
): OpenErrorActivity = OpenErrorActivity(context)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.schabi.newpipe.dependency_injection

import android.content.Context
import androidx.room.Room
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import org.schabi.newpipe.database.AppDatabase
import org.schabi.newpipe.database.AppDatabase.DATABASE_NAME
import org.schabi.newpipe.database.Migrations.MIGRATION_1_2
import org.schabi.newpipe.database.Migrations.MIGRATION_2_3
import org.schabi.newpipe.database.Migrations.MIGRATION_3_4
import org.schabi.newpipe.database.Migrations.MIGRATION_4_5
import org.schabi.newpipe.database.Migrations.MIGRATION_5_6
import org.schabi.newpipe.database.Migrations.MIGRATION_6_7
import org.schabi.newpipe.database.Migrations.MIGRATION_7_8
import org.schabi.newpipe.database.Migrations.MIGRATION_8_9
import org.schabi.newpipe.database.history.dao.SearchHistoryDAO
import org.schabi.newpipe.database.history.dao.StreamHistoryDAO
import org.schabi.newpipe.database.stream.dao.StreamDAO
import org.schabi.newpipe.database.stream.dao.StreamStateDAO
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
class DatabaseModule {

@Provides
@Singleton
fun provideAppDatabase(@ApplicationContext appContext: Context): AppDatabase =
Room.databaseBuilder(
appContext,
AppDatabase::class.java,
DATABASE_NAME
).addMigrations(
MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5,
MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9
).build()

@Provides
fun provideStreamStateDao(appDatabase: AppDatabase): StreamStateDAO =
appDatabase.streamStateDAO()

@Provides
fun providesStreamDao(appDatabase: AppDatabase): StreamDAO = appDatabase.streamDAO()

@Provides
fun provideStreamHistoryDao(appDatabase: AppDatabase): StreamHistoryDAO =
appDatabase.streamHistoryDAO()

@Provides
fun provideSearchHistoryDao(appDatabase: AppDatabase): SearchHistoryDAO =
appDatabase.searchHistoryDAO()
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private void handleCookiesFromUrl(@Nullable final String url) {
String abuseCookie = url.substring(abuseStart + 13, abuseEnd);
abuseCookie = Utils.decodeUrlUtf8(abuseCookie);
handleCookies(abuseCookie);
} catch (IllegalArgumentException | StringIndexOutOfBoundsException e) {
} catch (final StringIndexOutOfBoundsException e) {
if (MainActivity.DEBUG) {
Log.e(TAG, "handleCookiesFromUrl: invalid google abuse starting at "
+ abuseStart + " and ending at " + abuseEnd + " for url " + url, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.schabi.newpipe.error.usecases

import android.content.Context
import android.content.Intent
import org.schabi.newpipe.error.ErrorActivity
import org.schabi.newpipe.error.ErrorInfo

class OpenErrorActivity(
private val context: Context,
) {
operator fun invoke(errorInfo: ErrorInfo) {
val intent = Intent(context, ErrorActivity::class.java)
intent.putExtra(ErrorActivity.ERROR_INFO, errorInfo)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

context.startActivity(intent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.viewbinding.ViewBinding;

import com.google.android.material.snackbar.Snackbar;
Expand All @@ -26,6 +27,7 @@
import org.schabi.newpipe.databinding.PlaylistControlBinding;
import org.schabi.newpipe.databinding.StatisticPlaylistControlBinding;
import org.schabi.newpipe.error.ErrorInfo;
import org.schabi.newpipe.error.ErrorUtil;
import org.schabi.newpipe.error.UserAction;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.fragments.list.playlist.PlaylistControlViewHolder;
Expand All @@ -34,7 +36,6 @@
import org.schabi.newpipe.local.BaseLocalListFragment;
import org.schabi.newpipe.player.playqueue.PlayQueue;
import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
import org.schabi.newpipe.settings.HistorySettingsFragment;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.OnClickGesture;
import org.schabi.newpipe.util.PlayButtonHelper;
Expand Down Expand Up @@ -161,14 +162,72 @@ public void held(final LocalItem selectedItem) {
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
if (item.getItemId() == R.id.action_history_clear) {
HistorySettingsFragment
.openDeleteWatchHistoryDialog(requireContext(), recordManager, disposables);
openDeleteWatchHistoryDialog(requireContext(), recordManager, disposables);
} else {
return super.onOptionsItemSelected(item);
}
return true;
}

private static void openDeleteWatchHistoryDialog(
@NonNull final Context context,
final HistoryRecordManager recordManager,
final CompositeDisposable disposables
) {
new AlertDialog.Builder(context)
.setTitle(R.string.delete_view_history_alert)
.setNegativeButton(R.string.cancel, ((dialog, which) -> dialog.dismiss()))
.setPositiveButton(R.string.delete, ((dialog, which) -> {
disposables.add(getDeletePlaybackStatesDisposable(context, recordManager));
disposables.add(getWholeStreamHistoryDisposable(context, recordManager));
disposables.add(getRemoveOrphanedRecordsDisposable(context, recordManager));
}))
.show();
}

private static Disposable getDeletePlaybackStatesDisposable(
@NonNull final Context context,
final HistoryRecordManager recordManager
) {
return recordManager.deleteCompleteStreamStateHistory()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
howManyDeleted -> Toast.makeText(context,
R.string.watch_history_states_deleted, Toast.LENGTH_SHORT).show(),
throwable -> ErrorUtil.openActivity(context,
new ErrorInfo(throwable, UserAction.DELETE_FROM_HISTORY,
"Delete playback states"))
);
}

private static Disposable getWholeStreamHistoryDisposable(
@NonNull final Context context,
final HistoryRecordManager recordManager
) {
return recordManager.deleteWholeStreamHistory()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
howManyDeleted -> Toast.makeText(context,
R.string.watch_history_deleted, Toast.LENGTH_SHORT).show(),
throwable -> ErrorUtil.openActivity(context,
new ErrorInfo(throwable, UserAction.DELETE_FROM_HISTORY,
"Delete from history"))
);
}

private static Disposable getRemoveOrphanedRecordsDisposable(
@NonNull final Context context, final HistoryRecordManager recordManager) {
return recordManager.removeOrphanedRecords()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
howManyDeleted -> {
},
throwable -> ErrorUtil.openActivity(context,
new ErrorInfo(throwable, UserAction.DELETE_FROM_HISTORY,
"Clear orphaned records"))
);
}

///////////////////////////////////////////////////////////////////////////
// Fragment LifeCycle - Loading
///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -307,7 +366,7 @@ private void toggleSortMode() {
sortMode = StatisticSortMode.LAST_PLAYED;
setTitle(getString(R.string.title_last_played));
headerBinding.sortButtonIcon.setImageResource(
R.drawable.ic_filter_list);
R.drawable.ic_filter_list);
headerBinding.sortButtonText.setText(R.string.title_most_played);
}
startLoading(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,8 @@ private void showPathInSummary(final String prefKey, @StringRes final int defaul
target.setSummary(new File(URI.create(rawUri)).getPath());
return;
}
rawUri = decodeUrlUtf8(rawUri);

try {
rawUri = decodeUrlUtf8(rawUri);
} catch (final IllegalArgumentException e) {
// nothing to do
}

target.setSummary(rawUri);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import java.util.concurrent.TimeUnit;

import dagger.hilt.android.AndroidEntryPoint;
import icepick.Icepick;
import icepick.State;

Expand All @@ -64,6 +65,7 @@
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/

@AndroidEntryPoint
public class SettingsActivity extends AppCompatActivity implements
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
PreferenceSearchResultListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.XmlRes;
import androidx.fragment.app.Fragment;

import org.schabi.newpipe.R;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -35,7 +33,6 @@ private SettingsResourceRegistry() {
add(ContentSettingsFragment.class, R.xml.content_settings);
add(DebugSettingsFragment.class, R.xml.debug_settings).setSearchable(false);
add(DownloadSettingsFragment.class, R.xml.download_settings);
add(HistorySettingsFragment.class, R.xml.history_settings);
add(NotificationSettingsFragment.class, R.xml.notifications_settings);
add(PlayerNotificationSettingsFragment.class, R.xml.player_notification_settings);
add(UpdateSettingsFragment.class, R.xml.update_settings);
Expand Down
Loading
Loading