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 #11494

Open
wants to merge 30 commits into
base: refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2ac0d1f
add NP icon for Android Studio's NewUI
theScrabi Jul 2, 2024
5e5e77f
Merge pull request #11230 from TeamNewPipe/idea_icon
TobiGr Jul 3, 2024
9f766eb
Fix NPE in MediaSessionPlayerUi while destroying player
Stypox Jul 11, 2024
6f71c00
Merge pull request #11261 from Stypox/fix-media-session-ui-npe
Stypox Jul 11, 2024
0e0cee1
Update NewPipeExtractor to v0.24.1
Stypox Jul 11, 2024
acc5be9
Add changelogs for hotfix release v0.27.1 (998)
Stypox Jul 11, 2024
0f64158
Hotfix release v0.27.1 (998)
Stypox Jul 11, 2024
ad6b676
Update README.pt_BR.md (#11275)
tag27 Jul 13, 2024
72e9f7f
Merge branch 'master' into dev
Stypox Jul 15, 2024
0e8303f
Update Matrix room link, and prioritise it (#11350)
opusforlife2 Jul 25, 2024
947ac28
Update NewPipeExtractor to v0.24.2
Stypox Jul 25, 2024
56b6241
Hotfix release v0.27.2 (999)
Stypox Jul 25, 2024
03a6b5c
Add changelogs for hotfix release v0.27.2 (999)
Stypox Jul 11, 2024
64a8f65
Merge pull request #11351 from Stypox/update-npe
Stypox Jul 25, 2024
dbcb721
Don't warn about rhino class in proguard
Stypox Jul 25, 2024
d442b45
Remove code committed accidentally
Stypox Jul 25, 2024
51ee2f8
Merge branch 'master' into dev
Stypox Jul 25, 2024
d2cc37a
Added daggerhilt for better dependency injection for Compose Viewmodels
Aug 4, 2024
e1169dc
fixed inversed colors of surface for light theme
Aug 4, 2024
91af8b0
Added the new compose screen with its components and events
Aug 4, 2024
38c823a
Remove outdated returnActivity test code
Two-Ai Aug 10, 2024
7a498d8
changed a bit the components to make the screen stable for compose
Aug 11, 2024
86dbfea
updated gradle
Aug 13, 2024
fad3120
Merge pull request #11428 from Two-Ai/remove-returnActivity-test
TobiGr Aug 14, 2024
035c394
Fixing the 404 page not found, when clicking on "contribution notes" …
Stackoverflow-addict Aug 30, 2024
0057192
Changed business logic to be in its own files
Sep 1, 2024
ff42678
Updated view and viewmodel and remapped the fragments/activities
Sep 1, 2024
0530a6b
fix imports in some files
Sep 1, 2024
4c5cb31
Merge branch 'dev' into history_cache_compose
braiso-22 Sep 1, 2024
89576ad
Merge branch 'refactor' into history_cache_compose
braiso-22 Sep 1, 2024
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
15 changes: 15 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"
id "org.jetbrains.kotlin.plugin.compose" version "${kotlin_version}"
}

Expand Down Expand Up @@ -119,6 +120,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 @@ -299,6 +302,14 @@ dependencies {
// Coroutines interop
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx3:1.8.1'

// 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 @@ -319,6 +330,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
4 changes: 2 additions & 2 deletions app/src/main/java/org/schabi/newpipe/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.net.SocketException;
import java.util.List;
import java.util.Objects;

import dagger.hilt.android.HiltAndroidApp;
import coil.ImageLoader;
import coil.ImageLoaderFactory;
import coil.util.DebugLogger;
Expand Down Expand Up @@ -60,7 +60,7 @@
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/

@HiltAndroidApp
public class App extends Application implements ImageLoaderFactory {
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()
}
22 changes: 0 additions & 22 deletions app/src/main/java/org/schabi/newpipe/error/ErrorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
Expand All @@ -13,7 +12,6 @@
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -22,7 +20,6 @@
import com.grack.nanojson.JsonWriter;

import org.schabi.newpipe.BuildConfig;
import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R;
import org.schabi.newpipe.databinding.ActivityErrorBinding;
import org.schabi.newpipe.util.Localization;
Expand Down Expand Up @@ -187,25 +184,6 @@ private String formErrorText(final String[] el) {
.collect(Collectors.joining(separator + "\n", separator + "\n", separator));
}

/**
* Get the checked activity.
*
* @param returnActivity the activity to return to
* @return the casted return activity or null
*/
@Nullable
static Class<? extends Activity> getReturnActivity(final Class<?> returnActivity) {
Class<? extends Activity> checkedReturnActivity = null;
if (returnActivity != null) {
if (Activity.class.isAssignableFrom(returnActivity)) {
checkedReturnActivity = returnActivity.asSubclass(Activity.class);
} else {
checkedReturnActivity = MainActivity.class;
}
}
return checkedReturnActivity;
}

private void buildInfo(final ErrorInfo info) {
String text = "";

Expand Down
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)
}
}
Comment on lines +8 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be better as a static method in the error activity class itself.

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,7 @@ private void showPathInSummary(final String prefKey, @StringRes final int defaul
target.setSummary(new File(URI.create(rawUri)).getPath());
return;
}

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

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