@@ -29,7 +29,8 @@ class PortfolioRepository(
2929 ),
3030 private val settings : Settings = Settings (),
3131 private val defaultThemeColorsName : String = DEFAULT_THEME_COLORS_NAME ,
32- private val defaultThemeData : ThemeData = DEFAULT_THEME_DATA
32+ private val defaultThemeData : ThemeData = DEFAULT_THEME_DATA ,
33+ private val dataStore : DataStore = GeneratedDataStore (),
3334) {
3435 private val errorHandler = CoroutineExceptionHandler { _, th ->
3536 // do nothing
@@ -100,17 +101,21 @@ class PortfolioRepository(
100101 themeColors = MutableStateFlow (initThemeColors)
101102 }
102103
103- private fun ThemeData.getThemeColors (name : String ): ThemeColorsData ? {
104- return themes.find {
105- it.name.replace(" " , " " ).lowercase() == name
106- }
104+ fun getSelectedThemeColorsName (): StateFlow <String > {
105+ return themeColorsNameFlow
107106 }
108107
109- fun getSelectedThemeColors (): StateFlow <ThemeColorsData > {
110- repositoryScope.launch {
111- // fetchThemeDataFromRemote()
108+ fun setSelectedThemeColorsName (name : String ) {
109+ val parsedName = name.replace(" " , " " ).lowercase()
110+ val selectedThemeColors = themeData.getThemeColors(parsedName)
111+ if (selectedThemeColors != null ) {
112+ themeColorsName = parsedName
113+ themeColorsNameFlow.value = themeColorsName
114+ themeColors.value = selectedThemeColors
112115 }
116+ }
113117
118+ fun getSelectedThemeColors (): StateFlow <ThemeColorsData > {
114119 return themeColors
115120 }
116121
@@ -123,55 +128,46 @@ class PortfolioRepository(
123128 }
124129
125130 private suspend fun fetchThemeDataFromRemote () {
126- portfolioApi.getThemeData().let { response ->
127- val newThemeData = ThemeData (
128- response.themes.map { themeColors ->
129- ThemeColorsData (
130- name = themeColors.name,
131- isDark = themeColors.isDark,
132- primaryColor = themeColors.primaryColor,
133- onPrimaryColor = themeColors.onPrimaryColor,
134- surfaceColor = themeColors.surfaceColor,
135- onSurfaceColor = themeColors.onSurfaceColor,
136- errorColor = themeColors.errorColor,
137- onErrorColor = themeColors.onErrorColor,
138- )
139- }
140- )
131+ portfolioApi.getThemeData()
132+ .takeIf {
133+ it.themes.isNotEmpty()
134+ }
135+ ?.let { response ->
136+ val newThemeData = ThemeData (
137+ response.themes.map { themeColors ->
138+ ThemeColorsData (
139+ name = themeColors.name,
140+ isDark = themeColors.isDark,
141+ primaryColor = themeColors.primaryColor,
142+ onPrimaryColor = themeColors.onPrimaryColor,
143+ surfaceColor = themeColors.surfaceColor,
144+ onSurfaceColor = themeColors.onSurfaceColor,
145+ errorColor = themeColors.errorColor,
146+ onErrorColor = themeColors.onErrorColor,
147+ )
148+ }
149+ )
141150
142- themeData = newThemeData
143- themeDataFlow.value = themeData
151+ themeData = newThemeData
152+ themeDataFlow.value = themeData
144153
145- while (true ) {
146- val newThemeColors = themeData.getThemeColors(themeColorsName)
147- if (newThemeColors != null ) {
148- themeColors.value = newThemeColors
149- break
154+ while (true ) {
155+ val newThemeColors = themeData.getThemeColors(themeColorsName)
156+ if (newThemeColors != null ) {
157+ themeColorsNameFlow.value = themeColorsName
158+ themeColors.value = newThemeColors
159+ break
160+ }
161+ val parsedName = themeData.themes[0 ]
162+ .name
163+ .replace(" " , " " )
164+ .lowercase()
165+ themeColorsName = parsedName
150166 }
151- val parsedName = themeData.themes[0 ]
152- .name
153- .replace(" " , " " )
154- .lowercase()
155- themeColorsName = parsedName
156167 }
157- }
158168 }
159169
160- fun getSelectedThemeColorsName (): StateFlow <String > {
161- return themeColorsNameFlow
162- }
163-
164- fun setSelectedThemeColorsName (name : String ) {
165- val parsedName = name.replace(" " , " " ).lowercase()
166- val selectedThemeColors = themeData.getThemeColors(parsedName)
167- if (selectedThemeColors != null ) {
168- themeColorsName = parsedName
169- themeColorsNameFlow.value = themeColorsName
170- themeColors.value = selectedThemeColors
171- }
172- }
173-
174- suspend fun getPortfolioData (): PortfolioData {
170+ private suspend fun getPortfolioData (): PortfolioData {
175171 return withContext(Dispatchers .Default ) {
176172 portfolioApi.getPortfolioData().let { response ->
177173 PortfolioData (
@@ -211,27 +207,26 @@ class PortfolioRepository(
211207 }
212208 }
213209
214- // to be called from Kotlin/Native client
215- fun getPortfolioData (callback : (PortfolioData ) -> Unit ) {
216- repositoryScope.launch {
217- callback(getPortfolioData())
218- }
219- }
220-
221210 suspend fun getPageData (): PageData {
222- return when (GeneratedDataStore () .getPageType()) {
211+ return when (dataStore .getPageType()) {
223212 PageType .HOME -> PageData .Home (
224213 getPortfolioData(),
225214 BlogListDataStore ().getBlogListData()
226215 )
227- PageType .MD -> PageData .Md (getPortfolioData(), GeneratedDataStore () .getData())
216+ PageType .MD -> PageData .Md (getPortfolioData(), dataStore .getData())
228217 PageType .PROJECTS -> PageData .Projects (getPortfolioData())
229218 PageType .BACKGROUND -> PageData .Background (getPortfolioData())
230219 PageType .ABOUT_ME -> PageData .AboutMe (getPortfolioData())
231220 }
232221 }
233222
234223 companion object {
224+ private fun ThemeData.getThemeColors (name : String ): ThemeColorsData ? {
225+ return themes.find {
226+ it.name.replace(" " , " " ).lowercase() == name
227+ }
228+ }
229+
235230 private const val PREFS_THEME_COLORS_NAME = " theme_colors_name"
236231 private const val PREFS_THEME_DATA = " theme_data"
237232
0 commit comments