Skip to content

Commit 0057192

Browse files
committed
Changed business logic to be in its own files
1 parent 86dbfea commit 0057192

18 files changed

Lines changed: 614 additions & 0 deletions
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.schabi.newpipe.dependency_injection
2+
3+
import android.content.Context
4+
import android.content.SharedPreferences
5+
import androidx.preference.PreferenceManager
6+
import dagger.Module
7+
import dagger.Provides
8+
import dagger.hilt.InstallIn
9+
import dagger.hilt.android.qualifiers.ApplicationContext
10+
import dagger.hilt.components.SingletonComponent
11+
import org.schabi.newpipe.error.usecases.OpenErrorActivity
12+
import javax.inject.Singleton
13+
14+
@Module
15+
@InstallIn(SingletonComponent::class)
16+
object AppModule {
17+
@Provides
18+
@Singleton
19+
fun provideSharedPreferences(@ApplicationContext context: Context): SharedPreferences =
20+
PreferenceManager.getDefaultSharedPreferences(context)
21+
22+
@Provides
23+
@Singleton
24+
fun provideOpenActivity(
25+
@ApplicationContext context: Context,
26+
): OpenErrorActivity = OpenErrorActivity(context)
27+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.schabi.newpipe.dependency_injection
2+
3+
import android.content.Context
4+
import androidx.room.Room
5+
import dagger.Module
6+
import dagger.Provides
7+
import dagger.hilt.InstallIn
8+
import dagger.hilt.android.qualifiers.ApplicationContext
9+
import dagger.hilt.components.SingletonComponent
10+
import org.schabi.newpipe.database.AppDatabase
11+
import org.schabi.newpipe.database.AppDatabase.DATABASE_NAME
12+
import org.schabi.newpipe.database.Migrations.MIGRATION_1_2
13+
import org.schabi.newpipe.database.Migrations.MIGRATION_2_3
14+
import org.schabi.newpipe.database.Migrations.MIGRATION_3_4
15+
import org.schabi.newpipe.database.Migrations.MIGRATION_4_5
16+
import org.schabi.newpipe.database.Migrations.MIGRATION_5_6
17+
import org.schabi.newpipe.database.Migrations.MIGRATION_6_7
18+
import org.schabi.newpipe.database.Migrations.MIGRATION_7_8
19+
import org.schabi.newpipe.database.Migrations.MIGRATION_8_9
20+
import org.schabi.newpipe.database.history.dao.SearchHistoryDAO
21+
import org.schabi.newpipe.database.history.dao.StreamHistoryDAO
22+
import org.schabi.newpipe.database.stream.dao.StreamDAO
23+
import org.schabi.newpipe.database.stream.dao.StreamStateDAO
24+
import javax.inject.Singleton
25+
26+
@InstallIn(SingletonComponent::class)
27+
@Module
28+
class DatabaseModule {
29+
30+
@Provides
31+
@Singleton
32+
fun provideAppDatabase(@ApplicationContext appContext: Context): AppDatabase =
33+
Room.databaseBuilder(
34+
appContext,
35+
AppDatabase::class.java,
36+
DATABASE_NAME
37+
).addMigrations(
38+
MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5,
39+
MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9
40+
).build()
41+
42+
@Provides
43+
fun provideStreamStateDao(appDatabase: AppDatabase): StreamStateDAO =
44+
appDatabase.streamStateDAO()
45+
46+
@Provides
47+
fun providesStreamDao(appDatabase: AppDatabase): StreamDAO = appDatabase.streamDAO()
48+
49+
@Provides
50+
fun provideStreamHistoryDao(appDatabase: AppDatabase): StreamHistoryDAO =
51+
appDatabase.streamHistoryDAO()
52+
53+
@Provides
54+
fun provideSearchHistoryDao(appDatabase: AppDatabase): SearchHistoryDAO =
55+
appDatabase.searchHistoryDAO()
56+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.schabi.newpipe.error.usecases
2+
3+
import android.content.Context
4+
import android.content.Intent
5+
import org.schabi.newpipe.error.ErrorActivity
6+
import org.schabi.newpipe.error.ErrorInfo
7+
8+
class OpenErrorActivity(
9+
private val context: Context,
10+
) {
11+
operator fun invoke(errorInfo: ErrorInfo) {
12+
val intent = Intent(context, ErrorActivity::class.java)
13+
intent.putExtra(ErrorActivity.ERROR_INFO, errorInfo)
14+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
15+
16+
context.startActivity(intent)
17+
}
18+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package org.schabi.newpipe.settings.dependency_injection
2+
3+
import android.content.Context
4+
import android.content.SharedPreferences
5+
import dagger.Module
6+
import dagger.Provides
7+
import dagger.hilt.InstallIn
8+
import dagger.hilt.android.qualifiers.ApplicationContext
9+
import dagger.hilt.components.SingletonComponent
10+
import org.schabi.newpipe.database.history.dao.SearchHistoryDAO
11+
import org.schabi.newpipe.database.history.dao.StreamHistoryDAO
12+
import org.schabi.newpipe.database.stream.dao.StreamDAO
13+
import org.schabi.newpipe.database.stream.dao.StreamStateDAO
14+
import org.schabi.newpipe.error.usecases.OpenErrorActivity
15+
import org.schabi.newpipe.settings.domain.repositories.HistoryRecordRepository
16+
import org.schabi.newpipe.settings.domain.repositories.HistoryRecordRepositoryImpl
17+
import org.schabi.newpipe.settings.domain.usecases.DeleteCompleteSearchHistory
18+
import org.schabi.newpipe.settings.domain.usecases.DeleteCompleteStreamStateHistory
19+
import org.schabi.newpipe.settings.domain.usecases.DeletePlaybackStates
20+
import org.schabi.newpipe.settings.domain.usecases.DeleteWatchHistory
21+
import org.schabi.newpipe.settings.domain.usecases.RemoveOrphanedRecords
22+
import org.schabi.newpipe.settings.domain.usecases.get_preference.GetPreference
23+
import org.schabi.newpipe.settings.domain.usecases.get_preference.GetPreferenceImpl
24+
import org.schabi.newpipe.settings.domain.usecases.update_preference.UpdatePreference
25+
import org.schabi.newpipe.settings.domain.usecases.update_preference.UpdatePreferenceImpl
26+
import javax.inject.Singleton
27+
28+
@Module
29+
@InstallIn(SingletonComponent::class)
30+
object SettingsModule {
31+
32+
@Provides
33+
@Singleton
34+
fun provideGetBooleanPreference(
35+
sharedPreferences: SharedPreferences,
36+
@ApplicationContext context: Context,
37+
): GetPreference<Boolean> = GetPreferenceImpl(sharedPreferences, context)
38+
39+
@Provides
40+
@Singleton
41+
fun provideGetStringPreference(
42+
sharedPreferences: SharedPreferences,
43+
@ApplicationContext context: Context,
44+
): GetPreference<String> = GetPreferenceImpl(sharedPreferences, context)
45+
46+
@Provides
47+
@Singleton
48+
fun provideUpdateBooleanPreference(
49+
sharedPreferences: SharedPreferences,
50+
@ApplicationContext context: Context,
51+
): UpdatePreference<Boolean> = UpdatePreferenceImpl(context, sharedPreferences) { key, value ->
52+
putBoolean(
53+
key,
54+
value
55+
)
56+
}
57+
58+
@Provides
59+
@Singleton
60+
fun provideUpdateStringPreference(
61+
sharedPreferences: SharedPreferences,
62+
@ApplicationContext context: Context,
63+
): UpdatePreference<String> = UpdatePreferenceImpl(context, sharedPreferences) { key, value ->
64+
putString(
65+
key,
66+
value
67+
)
68+
}
69+
70+
@Provides
71+
@Singleton
72+
fun provideUpdateIntPreference(
73+
sharedPreferences: SharedPreferences,
74+
@ApplicationContext context: Context,
75+
): UpdatePreference<Int> = UpdatePreferenceImpl(context, sharedPreferences) { key, value ->
76+
putInt(key, value)
77+
}
78+
79+
@Provides
80+
@Singleton
81+
fun provideHistoryRecordRepository(
82+
streamStateDao: StreamStateDAO,
83+
streamHistoryDAO: StreamHistoryDAO,
84+
streamDAO: StreamDAO,
85+
searchHistoryDAO: SearchHistoryDAO,
86+
): HistoryRecordRepository = HistoryRecordRepositoryImpl(
87+
streamStateDao = streamStateDao,
88+
streamHistoryDAO = streamHistoryDAO,
89+
streamDAO = streamDAO,
90+
searchHistoryDAO = searchHistoryDAO,
91+
)
92+
93+
@Provides
94+
@Singleton
95+
fun provideDeletePlaybackStatesUseCase(
96+
historyRecordRepository: HistoryRecordRepository,
97+
): DeletePlaybackStates = DeletePlaybackStates(
98+
historyRecordRepository = historyRecordRepository,
99+
)
100+
101+
@Provides
102+
@Singleton
103+
fun provideDeleteWholeStreamHistoryUseCase(
104+
historyRecordRepository: HistoryRecordRepository,
105+
): DeleteCompleteStreamStateHistory = DeleteCompleteStreamStateHistory(
106+
historyRecordRepository = historyRecordRepository,
107+
)
108+
109+
@Provides
110+
@Singleton
111+
fun provideRemoveOrphanedRecordsUseCase(
112+
historyRecordRepository: HistoryRecordRepository,
113+
): RemoveOrphanedRecords = RemoveOrphanedRecords(
114+
historyRecordRepository = historyRecordRepository,
115+
)
116+
117+
@Provides
118+
@Singleton
119+
fun provideDeleteCompleteSearchHistoryUseCase(
120+
historyRecordRepository: HistoryRecordRepository,
121+
): DeleteCompleteSearchHistory = DeleteCompleteSearchHistory(
122+
historyRecordRepository = historyRecordRepository,
123+
)
124+
125+
@Provides
126+
@Singleton
127+
fun provideDeleteWatchHistoryUseCase(
128+
deletePlaybackStates: DeletePlaybackStates,
129+
deleteCompleteStreamStateHistory: DeleteCompleteStreamStateHistory,
130+
removeOrphanedRecords: RemoveOrphanedRecords,
131+
openErrorActivity: OpenErrorActivity,
132+
): DeleteWatchHistory = DeleteWatchHistory(
133+
deletePlaybackStates = deletePlaybackStates,
134+
deleteCompleteStreamStateHistory = deleteCompleteStreamStateHistory,
135+
removeOrphanedRecords = removeOrphanedRecords,
136+
openErrorActivity = openErrorActivity
137+
)
138+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.schabi.newpipe.settings.domain.repositories
2+
3+
import kotlinx.coroutines.CoroutineDispatcher
4+
import kotlinx.coroutines.flow.Flow
5+
6+
interface HistoryRecordRepository {
7+
fun deleteCompleteStreamState(dispatcher: CoroutineDispatcher): Flow<Int>
8+
fun deleteWholeStreamHistory(dispatcher: CoroutineDispatcher): Flow<Int>
9+
fun removeOrphanedRecords(dispatcher: CoroutineDispatcher): Flow<Int>
10+
fun deleteCompleteSearchHistory(dispatcher: CoroutineDispatcher): Flow<Int>
11+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.schabi.newpipe.settings.domain.repositories
2+
3+
import kotlinx.coroutines.CoroutineDispatcher
4+
import kotlinx.coroutines.flow.Flow
5+
import kotlinx.coroutines.flow.MutableStateFlow
6+
import kotlinx.coroutines.flow.asStateFlow
7+
import kotlinx.coroutines.flow.flow
8+
import kotlinx.coroutines.flow.flowOn
9+
import kotlinx.coroutines.flow.update
10+
import org.schabi.newpipe.database.history.model.SearchHistoryEntry
11+
import org.schabi.newpipe.database.history.model.StreamHistoryEntity
12+
import org.schabi.newpipe.database.stream.model.StreamEntity
13+
import org.schabi.newpipe.database.stream.model.StreamStateEntity
14+
15+
class HistoryRecordRepositoryFake : HistoryRecordRepository {
16+
private val _searchHistory: MutableStateFlow<List<SearchHistoryEntry>> = MutableStateFlow(
17+
emptyList()
18+
)
19+
val searchHistory = _searchHistory.asStateFlow()
20+
private val _streamHistory = MutableStateFlow<List<StreamHistoryEntity>>(emptyList())
21+
val streamHistory = _streamHistory.asStateFlow()
22+
private val _streams = MutableStateFlow<List<StreamEntity>>(emptyList())
23+
val streams = _streams.asStateFlow()
24+
private val _streamStates = MutableStateFlow<List<StreamStateEntity>>(emptyList())
25+
val streamStates = _streamStates.asStateFlow()
26+
27+
override fun deleteCompleteStreamState(dispatcher: CoroutineDispatcher): Flow<Int> = flow {
28+
val count = streamStates.value.size
29+
_streamStates.update {
30+
emptyList()
31+
}
32+
emit(count)
33+
}.flowOn(dispatcher)
34+
35+
override fun deleteWholeStreamHistory(dispatcher: CoroutineDispatcher): Flow<Int> = flow {
36+
val count = streamHistory.value.size
37+
_streamHistory.update {
38+
emptyList()
39+
}
40+
emit(count)
41+
}.flowOn(dispatcher)
42+
43+
override fun removeOrphanedRecords(dispatcher: CoroutineDispatcher): Flow<Int> = flow {
44+
val orphanedStreams = streams.value.filter { stream ->
45+
!streamHistory.value.any { it.streamUid == stream.uid }
46+
}
47+
48+
val deletedCount = orphanedStreams.size
49+
50+
_streams.update { oldStreams ->
51+
oldStreams.filter { it !in orphanedStreams }
52+
}
53+
54+
emit(deletedCount)
55+
}.flowOn(dispatcher)
56+
57+
override fun deleteCompleteSearchHistory(dispatcher: CoroutineDispatcher): Flow<Int> = flow {
58+
val count = searchHistory.value.size
59+
_searchHistory.update {
60+
emptyList()
61+
}
62+
emit(count)
63+
}.flowOn(dispatcher)
64+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.schabi.newpipe.settings.domain.repositories
2+
3+
import kotlinx.coroutines.CoroutineDispatcher
4+
import kotlinx.coroutines.flow.Flow
5+
import kotlinx.coroutines.flow.flow
6+
import kotlinx.coroutines.flow.flowOn
7+
import org.schabi.newpipe.database.history.dao.SearchHistoryDAO
8+
import org.schabi.newpipe.database.history.dao.StreamHistoryDAO
9+
import org.schabi.newpipe.database.stream.dao.StreamDAO
10+
import org.schabi.newpipe.database.stream.dao.StreamStateDAO
11+
12+
class HistoryRecordRepositoryImpl(
13+
private val streamStateDao: StreamStateDAO,
14+
private val streamHistoryDAO: StreamHistoryDAO,
15+
private val streamDAO: StreamDAO,
16+
private val searchHistoryDAO: SearchHistoryDAO,
17+
) : HistoryRecordRepository {
18+
override fun deleteCompleteStreamState(dispatcher: CoroutineDispatcher): Flow<Int> =
19+
flow {
20+
val deletedCount = streamStateDao.deleteAll()
21+
emit(deletedCount)
22+
}.flowOn(dispatcher)
23+
24+
override fun deleteWholeStreamHistory(dispatcher: CoroutineDispatcher): Flow<Int> =
25+
flow {
26+
val deletedCount = streamHistoryDAO.deleteAll()
27+
emit(deletedCount)
28+
}.flowOn(dispatcher)
29+
30+
override fun removeOrphanedRecords(dispatcher: CoroutineDispatcher): Flow<Int> = flow {
31+
val deletedCount = streamDAO.deleteOrphans()
32+
emit(deletedCount)
33+
}.flowOn(dispatcher)
34+
35+
override fun deleteCompleteSearchHistory(dispatcher: CoroutineDispatcher): Flow<Int> = flow {
36+
val deletedCount = searchHistoryDAO.deleteAll()
37+
emit(deletedCount)
38+
}.flowOn(dispatcher)
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.schabi.newpipe.settings.domain.usecases
2+
3+
import kotlinx.coroutines.CoroutineDispatcher
4+
import kotlinx.coroutines.flow.catch
5+
import kotlinx.coroutines.flow.take
6+
import org.schabi.newpipe.settings.domain.repositories.HistoryRecordRepository
7+
8+
class DeleteCompleteSearchHistory(
9+
private val historyRecordRepository: HistoryRecordRepository,
10+
) {
11+
suspend operator fun invoke(
12+
dispatcher: CoroutineDispatcher,
13+
onError: (Throwable) -> Unit,
14+
onSuccess: () -> Unit,
15+
) = historyRecordRepository.deleteCompleteSearchHistory(dispatcher).catch { error ->
16+
onError(error)
17+
}.take(1).collect {
18+
onSuccess()
19+
}
20+
}

0 commit comments

Comments
 (0)