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

Migrate “History and Cache” to Jetpack Compose #11494

Open
wants to merge 2 commits into
base: refactor
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,11 @@ dependencies {
// Coroutines interop
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx3:1.8.1'

// Hilt
// Hilt & Dagger
implementation("com.google.dagger:hilt-android:2.51.1")
kapt("com.google.dagger:hilt-compiler:2.51.1")
implementation("androidx.hilt:hilt-navigation-compose:1.2.0")
kapt("androidx.hilt:hilt-compiler:1.2.0")

// Scroll
implementation 'com.github.nanihadesuka:LazyColumnScrollbar:2.2.0'
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 @@ -94,6 +94,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
@@ -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.evernote.android.state.State;
Expand All @@ -27,6 +28,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 @@ -35,7 +37,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 @@ -43,6 +43,9 @@

import java.util.concurrent.TimeUnit;

import dagger.hilt.android.AndroidEntryPoint;


/*
* Created by Christian Schabesberger on 31.08.15.
*
Expand All @@ -63,6 +66,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
@@ -0,0 +1,85 @@
package org.schabi.newpipe.settings.components.irreversible_preference

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.tooling.preview.Preview
import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.ui.theme.SizeTokens.SpacingExtraSmall
import org.schabi.newpipe.ui.theme.SizeTokens.SpacingMedium

@Composable
fun IrreversiblePreferenceComponent(
title: String,
summary: String,
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
) {
val clickModifier = if (enabled) {
Modifier.clickable { onClick() }
} else {
Modifier
}
Row(
modifier = clickModifier.then(modifier),
Comment on lines +31 to +37
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
val clickModifier = if (enabled) {
Modifier.clickable { onClick() }
} else {
Modifier
}
Row(
modifier = clickModifier.then(modifier),
Row(
modifier = Modifier
.clickable(enabled, onClick = onClick)
.then(modifier),

verticalAlignment = Alignment.CenterVertically,
) {
val alpha by remember {
derivedStateOf {
if (enabled) 1f else 0.38f
}
}
Column(
modifier = Modifier.padding(SpacingMedium)
) {
Text(
text = title,
modifier = Modifier.alpha(alpha),
)
Spacer(modifier = Modifier.padding(SpacingExtraSmall))
Text(
text = summary,
style = MaterialTheme.typography.labelSmall,
modifier = Modifier.alpha(alpha * 0.6f),
)
}
}
}

@Preview(showBackground = true, backgroundColor = 0xFFFFFFFF)
@Composable
private fun IrreversiblePreferenceComponentPreview() {
val title = "Wipe cached metadata"
val summary = "Remove all cached webpage data"
AppTheme {
Column {

IrreversiblePreferenceComponent(
title = title,
summary = summary,
onClick = {},
modifier = Modifier.fillMaxWidth()
)
IrreversiblePreferenceComponent(
title = title,
summary = summary,
onClick = {},
modifier = Modifier.fillMaxWidth(),
enabled = false
)
}
}
}
Loading
Loading