Skip to content

Commit b090a52

Browse files
committed
refactor the source
1 parent 67ffc93 commit b090a52

34 files changed

+553
-469
lines changed

app/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ plugins {
1111
android {
1212
namespace = "com.vanced.store"
1313

14-
compileSdk = 32
14+
compileSdk = 33
1515

1616
defaultConfig {
1717
applicationId = "com.vanced.store"
1818
minSdk = 21
19-
targetSdk = 32
19+
targetSdk = 33
2020
versionCode = 1
2121
versionName = "1.0"
2222
}

app/src/main/java/com/vanced/store/VSApp.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ class VSApp : Application() {
1515

1616
modules(
1717
httpModule,
18-
repositoryModule,
19-
viewModelModule,
20-
managerModule,
2118
databaseModule,
22-
serviceModule
19+
preferenceModule,
20+
21+
mainModule,
22+
browseModule,
23+
libraryModule,
24+
reposModule,
25+
themesModule
2326
)
2427
}
2528
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.vanced.store.datasource
2+
3+
import androidx.datastore.core.DataStore
4+
import androidx.datastore.preferences.core.Preferences
5+
import androidx.datastore.preferences.core.edit
6+
import androidx.datastore.preferences.core.intPreferencesKey
7+
import kotlinx.coroutines.flow.Flow
8+
import kotlinx.coroutines.flow.map
9+
10+
interface PreferenceDatasource {
11+
12+
fun observeAppTheme(): Flow<AppTheme>
13+
suspend fun saveAppTheme(theme: AppTheme)
14+
15+
fun observeAppAccent(): Flow<AppAccent>
16+
suspend fun saveAppAccent(accent: AppAccent)
17+
18+
}
19+
20+
class PreferenceDatasourceImpl(
21+
private val datastore: DataStore<Preferences>
22+
) : PreferenceDatasource {
23+
24+
override fun observeAppTheme(): Flow<AppTheme> {
25+
return datastore.data.map {
26+
AppTheme.values()[it[KEY_APP_THEME] ?: 0]
27+
}
28+
}
29+
30+
override suspend fun saveAppTheme(theme: AppTheme) {
31+
datastore.edit {
32+
it[KEY_APP_THEME] = theme.ordinal
33+
}
34+
}
35+
36+
override fun observeAppAccent(): Flow<AppAccent> {
37+
return datastore.data.map {
38+
AppAccent.values()[it[KEY_APP_ACCENT] ?: 0]
39+
}
40+
}
41+
42+
override suspend fun saveAppAccent(accent: AppAccent) {
43+
datastore.edit {
44+
it[KEY_APP_ACCENT] = accent.ordinal
45+
}
46+
}
47+
48+
private companion object {
49+
val KEY_APP_THEME = intPreferencesKey("app_theme")
50+
val KEY_APP_ACCENT = intPreferencesKey("app_accent")
51+
}
52+
53+
}
54+
55+
enum class AppTheme {
56+
System, Light, Dark
57+
}
58+
59+
enum class AppAccent {
60+
Blue, Orange, Pink, Purple
61+
}

app/src/main/java/com/vanced/store/db/AppDatabase.kt app/src/main/java/com/vanced/store/db/VSDatabase.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.vanced.store.db.dao.RepoDao
66
import com.vanced.store.db.entity.EntityRepo
77

88
@Database(entities = [EntityRepo::class], version = 2)
9-
abstract class AppDatabase: RoomDatabase() {
9+
abstract class VSDatabase: RoomDatabase() {
1010

1111
abstract fun repoDao(): RepoDao
1212

app/src/main/java/com/vanced/store/db/dao/RepoDao.kt

+4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package com.vanced.store.db.dao
33
import androidx.room.*
44
import androidx.room.OnConflictStrategy.ABORT
55
import com.vanced.store.db.entity.EntityRepo
6+
import kotlinx.coroutines.flow.Flow
67

78
@Dao
89
interface RepoDao {
910

11+
@Query("SELECT * FROM repos")
12+
fun observeAll(): Flow<List<EntityRepo>>
13+
1014
@Query("SELECT * FROM repos")
1115
suspend fun getAll(): List<EntityRepo>
1216

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.vanced.store.di
2+
3+
import com.vanced.store.domain.repository.BrowseRepository
4+
import com.vanced.store.domain.repository.BrowseRepositoryImpl
5+
import com.vanced.store.network.service.GithubService
6+
import com.vanced.store.network.service.GithubServiceImpl
7+
import com.vanced.store.ui.viewmodel.BrowseViewModel
8+
import org.koin.androidx.viewmodel.dsl.viewModelOf
9+
import org.koin.core.module.dsl.singleOf
10+
import org.koin.dsl.bind
11+
import org.koin.dsl.module
12+
13+
val browseModule = module {
14+
singleOf(::GithubServiceImpl) bind GithubService::class
15+
singleOf(::BrowseRepositoryImpl) bind BrowseRepository::class
16+
viewModelOf(::BrowseViewModel)
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.vanced.store.di
22

33
import android.content.Context
4-
import com.vanced.store.db.AppDatabase
4+
import com.vanced.store.db.VSDatabase
55
import com.vanced.store.util.roomDatabase
6-
import org.koin.android.ext.koin.androidContext
6+
import org.koin.core.module.dsl.singleOf
77
import org.koin.dsl.module
88

99
val databaseModule = module {
1010

1111
fun provideAppsRepositoryDatabase(
1212
context: Context
13-
): AppDatabase {
13+
): VSDatabase {
1414
return roomDatabase(context, databaseName = "app") {
1515
fallbackToDestructiveMigration()
1616
}
1717
}
1818

19-
single { provideAppsRepositoryDatabase(androidContext()) }
19+
singleOf(::provideAppsRepositoryDatabase)
2020
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.vanced.store.di
2+
3+
import com.vanced.store.domain.repository.LibraryRepository
4+
import com.vanced.store.domain.repository.LibraryRepositoryImpl
5+
import org.koin.core.module.dsl.singleOf
6+
import org.koin.dsl.bind
7+
import org.koin.dsl.module
8+
9+
val libraryModule = module {
10+
singleOf(::LibraryRepositoryImpl) bind LibraryRepository::class
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.vanced.store.di
2+
3+
import com.vanced.store.domain.repository.MainRepository
4+
import com.vanced.store.domain.repository.MainRepositoryImpl
5+
import com.vanced.store.ui.viewmodel.MainViewModel
6+
import org.koin.androidx.viewmodel.dsl.viewModelOf
7+
import org.koin.core.module.dsl.singleOf
8+
import org.koin.dsl.bind
9+
import org.koin.dsl.module
10+
11+
val mainModule = module {
12+
singleOf(::MainRepositoryImpl) bind MainRepository::class
13+
viewModelOf(::MainViewModel)
14+
}

app/src/main/java/com/vanced/store/di/ManagerModule.kt

-19
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.vanced.store.di
2+
3+
import android.content.Context
4+
import androidx.datastore.preferences.preferencesDataStore
5+
import com.vanced.store.datasource.PreferenceDatasource
6+
import com.vanced.store.datasource.PreferenceDatasourceImpl
7+
import org.koin.android.ext.koin.androidContext
8+
import org.koin.core.module.dsl.singleOf
9+
import org.koin.dsl.bind
10+
import org.koin.dsl.module
11+
12+
val Context.datastore by preferencesDataStore("preferences")
13+
14+
val preferenceModule = module {
15+
single {
16+
androidContext().datastore
17+
}
18+
singleOf(::PreferenceDatasourceImpl) bind PreferenceDatasource::class
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.vanced.store.di
2+
3+
import com.vanced.store.domain.repository.RepoRepository
4+
import com.vanced.store.domain.repository.RepoRepositoryImpl
5+
import com.vanced.store.network.service.RepoService
6+
import com.vanced.store.network.service.RepoServiceImpl
7+
import com.vanced.store.ui.viewmodel.RepoViewModel
8+
import org.koin.androidx.viewmodel.dsl.viewModelOf
9+
import org.koin.core.module.dsl.singleOf
10+
import org.koin.dsl.bind
11+
import org.koin.dsl.module
12+
13+
val reposModule = module {
14+
singleOf(::RepoServiceImpl) bind RepoService::class
15+
singleOf(::RepoRepositoryImpl) bind RepoRepository::class
16+
viewModelOf(::RepoViewModel)
17+
}

app/src/main/java/com/vanced/store/di/RepositoryModule.kt

-38
This file was deleted.

app/src/main/java/com/vanced/store/di/ServiceModule.kt

-30
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.vanced.store.di
2+
3+
import com.vanced.store.domain.repository.ThemesRepository
4+
import com.vanced.store.domain.repository.ThemesRepositoryImpl
5+
import com.vanced.store.ui.viewmodel.ThemesViewModel
6+
import org.koin.androidx.viewmodel.dsl.viewModelOf
7+
import org.koin.core.module.dsl.singleOf
8+
import org.koin.dsl.bind
9+
import org.koin.dsl.module
10+
11+
val themesModule = module {
12+
singleOf(::ThemesRepositoryImpl) bind ThemesRepository::class
13+
viewModelOf(::ThemesViewModel)
14+
}

app/src/main/java/com/vanced/store/di/ViewModelModule.kt

-52
This file was deleted.

0 commit comments

Comments
 (0)