-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement repository pattern for datastore settings
- Loading branch information
Showing
10 changed files
with
112 additions
and
76 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
app/src/main/java/dev/chungjungsoo/gptmobile/data/dto/ThemeSetting.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.chungjungsoo.gptmobile.data.dto | ||
|
||
import dev.chungjungsoo.gptmobile.data.model.DynamicTheme | ||
import dev.chungjungsoo.gptmobile.data.model.ThemeMode | ||
|
||
data class ThemeSetting( | ||
val dynamicTheme: DynamicTheme = DynamicTheme.OFF, | ||
val themeMode: ThemeMode = ThemeMode.SYSTEM | ||
) |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/dev/chungjungsoo/gptmobile/data/repository/SettingRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.chungjungsoo.gptmobile.data.repository | ||
|
||
import dev.chungjungsoo.gptmobile.data.dto.Platform | ||
import dev.chungjungsoo.gptmobile.data.dto.ThemeSetting | ||
|
||
interface SettingRepository { | ||
suspend fun fetchPlatforms(): List<Platform> | ||
suspend fun fetchThemes(): ThemeSetting | ||
suspend fun updatePlatforms(platforms: List<Platform>) | ||
suspend fun updateThemes(themeSetting: ThemeSetting) | ||
} |
50 changes: 50 additions & 0 deletions
50
app/src/main/java/dev/chungjungsoo/gptmobile/data/repository/SettingRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dev.chungjungsoo.gptmobile.data.repository | ||
|
||
import dev.chungjungsoo.gptmobile.data.datastore.SettingDataSource | ||
import dev.chungjungsoo.gptmobile.data.dto.Platform | ||
import dev.chungjungsoo.gptmobile.data.dto.ThemeSetting | ||
import dev.chungjungsoo.gptmobile.data.model.ApiType | ||
import dev.chungjungsoo.gptmobile.data.model.DynamicTheme | ||
import dev.chungjungsoo.gptmobile.data.model.ThemeMode | ||
import javax.inject.Inject | ||
|
||
class SettingRepositoryImpl @Inject constructor( | ||
private val settingDataSource: SettingDataSource | ||
) : SettingRepository { | ||
|
||
override suspend fun fetchPlatforms(): List<Platform> { | ||
return ApiType.entries.map { apiType -> | ||
val status = settingDataSource.getStatus(apiType) | ||
val token = settingDataSource.getToken(apiType) | ||
val model = settingDataSource.getModel(apiType) | ||
|
||
Platform(apiType, enabled = status ?: false, token = token, model = model) | ||
} | ||
} | ||
|
||
override suspend fun fetchThemes(): ThemeSetting { | ||
return ThemeSetting( | ||
dynamicTheme = settingDataSource.getDynamicTheme() ?: DynamicTheme.OFF, | ||
themeMode = settingDataSource.getThemeMode() ?: ThemeMode.SYSTEM | ||
) | ||
} | ||
|
||
override suspend fun updatePlatforms(platforms: List<Platform>) { | ||
platforms.forEach { platform -> | ||
settingDataSource.updateStatus(platform.name, platform.selected) | ||
|
||
if (platform.token != null) { | ||
settingDataSource.updateToken(platform.name, platform.token) | ||
} | ||
|
||
if (platform.model != null) { | ||
settingDataSource.updateModel(platform.name, platform.model) | ||
} | ||
} | ||
} | ||
|
||
override suspend fun updateThemes(themeSetting: ThemeSetting) { | ||
settingDataSource.updateDynamicTheme(themeSetting.dynamicTheme) | ||
settingDataSource.updateThemeMode(themeSetting.themeMode) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/dev/chungjungsoo/gptmobile/di/SettingRepositoryModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dev.chungjungsoo.gptmobile.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import dev.chungjungsoo.gptmobile.data.datastore.SettingDataSource | ||
import dev.chungjungsoo.gptmobile.data.repository.SettingRepository | ||
import dev.chungjungsoo.gptmobile.data.repository.SettingRepositoryImpl | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object SettingRepositoryModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideSettingRepository( | ||
settingDataSource: SettingDataSource | ||
): SettingRepository = SettingRepositoryImpl(settingDataSource) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters