Skip to content

Commit a02eef0

Browse files
moved theme data to separate package
1 parent 39f4602 commit a02eef0

20 files changed

Lines changed: 382 additions & 376 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.github.amanshuraikwar.portfolio
2+
3+
import io.ktor.client.HttpClient
4+
import io.ktor.client.engine.HttpClientEngine
5+
import io.ktor.client.features.json.JsonFeature
6+
import io.ktor.client.features.json.serializer.KotlinxSerializer
7+
import io.ktor.client.features.logging.DEFAULT
8+
import io.ktor.client.features.logging.LogLevel
9+
import io.ktor.client.features.logging.Logger
10+
import io.ktor.client.features.logging.Logging
11+
import kotlinx.serialization.json.Json
12+
13+
object Factory {
14+
fun createHttpClient(
15+
engine: HttpClientEngine,
16+
json: Json = createJson(),
17+
enableNetworkLogs: Boolean
18+
) = HttpClient(engine) {
19+
install(JsonFeature) {
20+
serializer = KotlinxSerializer(json)
21+
}
22+
if (enableNetworkLogs) {
23+
install(Logging) {
24+
logger = Logger.DEFAULT
25+
level = LogLevel.INFO
26+
}
27+
}
28+
}
29+
30+
fun createHttpClient(
31+
json: Json = createJson(),
32+
enableNetworkLogs: Boolean
33+
) = HttpClient {
34+
install(JsonFeature) {
35+
serializer = KotlinxSerializer(json)
36+
}
37+
if (enableNetworkLogs) {
38+
install(Logging) {
39+
logger = Logger.DEFAULT
40+
level = LogLevel.INFO
41+
}
42+
}
43+
}
44+
45+
fun createJson() = Json { isLenient = true; ignoreUnknownKeys = true }
46+
}

project-files/Portfolio/shared/src/commonMain/kotlin/io/github/amanshuraikwar/portfolio/PortfolioRepository.kt

Lines changed: 1 addition & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -2,171 +2,21 @@
22

33
package io.github.amanshuraikwar.portfolio
44

5-
import com.russhwolf.settings.Settings
65
import io.github.amanshuraikwar.portfolio.model.AppData
76
import io.github.amanshuraikwar.portfolio.model.AppLink
87
import io.github.amanshuraikwar.portfolio.model.ExperienceData
98
import io.github.amanshuraikwar.portfolio.model.LinkData
109
import io.github.amanshuraikwar.portfolio.model.PortfolioData
11-
import io.github.amanshuraikwar.portfolio.model.ThemeColorsData
12-
import io.github.amanshuraikwar.portfolio.model.ThemeData
1310
import io.github.amanshuraikwar.portfolio.network.PortfolioApi
14-
import kotlinx.coroutines.CoroutineExceptionHandler
1511
import kotlinx.coroutines.Dispatchers
16-
import kotlinx.coroutines.MainScope
17-
import kotlinx.coroutines.flow.MutableStateFlow
18-
import kotlinx.coroutines.flow.StateFlow
19-
import kotlinx.coroutines.launch
20-
import kotlinx.coroutines.plus
2112
import kotlinx.coroutines.withContext
22-
import kotlinx.serialization.decodeFromString
23-
import kotlinx.serialization.encodeToString
24-
import kotlinx.serialization.json.Json
2513

2614
class PortfolioRepository(
2715
private val portfolioApi: PortfolioApi = PortfolioApi(
28-
client = PortfolioApi.createHttpClient(enableNetworkLogs = true)
16+
client = Factory.createHttpClient(enableNetworkLogs = true)
2917
),
30-
private val settings: Settings = Settings(),
31-
private val defaultThemeColorsName: String = DEFAULT_THEME_COLORS_NAME,
32-
private val defaultThemeData: ThemeData = DEFAULT_THEME_DATA,
3318
private val dataStore: DataStore = GeneratedDataStore(),
3419
) {
35-
private val errorHandler = CoroutineExceptionHandler { _, th ->
36-
// do nothing
37-
}
38-
private val repositoryScope = MainScope() + Dispatchers.Default + errorHandler
39-
40-
private var themeColorsName: String
41-
get() {
42-
return settings.getString(
43-
PREFS_THEME_COLORS_NAME,
44-
defaultThemeColorsName.replace(" ", "").lowercase()
45-
)
46-
}
47-
set(value) {
48-
settings.putString(
49-
PREFS_THEME_COLORS_NAME,
50-
value
51-
)
52-
}
53-
54-
private val themeColorsNameFlow: MutableStateFlow<String>
55-
56-
private var themeData: ThemeData
57-
get() {
58-
return try {
59-
Json.decodeFromString(
60-
settings.getString(
61-
PREFS_THEME_DATA,
62-
Json.encodeToString(defaultThemeData)
63-
)
64-
)
65-
} catch (e: Exception) {
66-
settings.putString(
67-
PREFS_THEME_DATA,
68-
Json.encodeToString(defaultThemeData)
69-
)
70-
DEFAULT_THEME_DATA
71-
}
72-
}
73-
set(value) {
74-
settings.putString(
75-
PREFS_THEME_DATA,
76-
Json.encodeToString(value)
77-
)
78-
}
79-
80-
private val themeDataFlow = MutableStateFlow(themeData)
81-
82-
private val themeColors: MutableStateFlow<ThemeColorsData>
83-
84-
init {
85-
val initThemeColors: ThemeColorsData
86-
87-
while (true) {
88-
val selectedThemeColors = themeData.getThemeColors(themeColorsName)
89-
if (selectedThemeColors != null) {
90-
initThemeColors = selectedThemeColors
91-
break
92-
}
93-
val parsedName = themeData.themes[0]
94-
.name
95-
.replace(" ", "")
96-
.lowercase()
97-
themeColorsName = parsedName
98-
}
99-
100-
themeColorsNameFlow = MutableStateFlow(themeColorsName)
101-
themeColors = MutableStateFlow(initThemeColors)
102-
}
103-
104-
fun getSelectedThemeColorsName(): StateFlow<String> {
105-
return themeColorsNameFlow
106-
}
107-
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
115-
}
116-
}
117-
118-
fun getSelectedThemeColors(): StateFlow<ThemeColorsData> {
119-
return themeColors
120-
}
121-
122-
fun getThemeData(): StateFlow<ThemeData> {
123-
repositoryScope.launch {
124-
fetchThemeDataFromRemote()
125-
}
126-
127-
return themeDataFlow
128-
}
129-
130-
private suspend fun fetchThemeDataFromRemote() {
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-
)
150-
151-
themeData = newThemeData
152-
themeDataFlow.value = themeData
153-
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
166-
}
167-
}
168-
}
169-
17020
private suspend fun getPortfolioData(): PortfolioData {
17121
return withContext(Dispatchers.Default) {
17222
portfolioApi.getPortfolioData().let { response ->
@@ -219,53 +69,4 @@ class PortfolioRepository(
21969
PageType.ABOUT_ME -> PageData.AboutMe(getPortfolioData())
22070
}
22171
}
222-
223-
companion object {
224-
private fun ThemeData.getThemeColors(name: String): ThemeColorsData? {
225-
return themes.find {
226-
it.name.replace(" ", "").lowercase() == name
227-
}
228-
}
229-
230-
private const val PREFS_THEME_COLORS_NAME = "theme_colors_name"
231-
private const val PREFS_THEME_DATA = "theme_data"
232-
233-
private val DEFAULT_THEME_DATA = ThemeData(
234-
listOf(
235-
ThemeColorsData(
236-
name = "Dark Salmon",
237-
isDark = true,
238-
primaryColor = "#FFFFCDD2",
239-
onPrimaryColor = "#FF4E342E",
240-
surfaceColor = "#FF212121",
241-
onSurfaceColor = "#FFffffff",
242-
errorColor = "#FFE57373",
243-
onErrorColor = "#FF4E342E",
244-
),
245-
ThemeColorsData(
246-
name = "Light Blue",
247-
isDark = false,
248-
primaryColor = "#ffEA5C5A",
249-
onPrimaryColor = "#FFffffff",
250-
surfaceColor = "#ffCDECF9",
251-
onSurfaceColor = "#FF030204",
252-
errorColor = "#FFE57373",
253-
onErrorColor = "#FF212121",
254-
),
255-
ThemeColorsData(
256-
name = "Matt D'av Ella",
257-
isDark = false,
258-
primaryColor = "#ffE35638",
259-
onPrimaryColor = "#FFFADACA",
260-
surfaceColor = "#ffFBF8EC",
261-
onSurfaceColor = "#FF24242C",
262-
errorColor = "#FFE57373",
263-
onErrorColor = "#FF212121",
264-
)
265-
)
266-
)
267-
268-
private val DEFAULT_THEME_COLORS_NAME =
269-
DEFAULT_THEME_DATA.themes[0].name.replace(" ", "").lowercase()
270-
}
27172
}

project-files/Portfolio/shared/src/commonMain/kotlin/io/github/amanshuraikwar/portfolio/model/AppLinkType.kt renamed to project-files/Portfolio/shared/src/commonMain/kotlin/io/github/amanshuraikwar/portfolio/model/AppLink.kt

File renamed without changes.
Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
package io.github.amanshuraikwar.portfolio.network
22

33
import io.github.amanshuraikwar.portfolio.network.model.PortfolioDataResponse
4-
import io.github.amanshuraikwar.portfolio.network.model.ThemeDataResponse
54
import io.ktor.client.HttpClient
6-
import io.ktor.client.engine.HttpClientEngine
7-
import io.ktor.client.features.json.JsonFeature
8-
import io.ktor.client.features.json.serializer.KotlinxSerializer
9-
import io.ktor.client.features.logging.DEFAULT
10-
import io.ktor.client.features.logging.LogLevel
11-
import io.ktor.client.features.logging.Logger
12-
import io.ktor.client.features.logging.Logging
135
import io.ktor.client.request.get
14-
import io.ktor.utils.io.core.withBuffer
15-
import kotlinx.serialization.json.Json
166

177
class PortfolioApi(
188
private val client: HttpClient,
@@ -23,42 +13,4 @@ class PortfolioApi(
2313

2414
suspend fun getAppData(appId: String) =
2515
client.get<PortfolioDataResponse>("$baseUrl/app_data_$appId.json")
26-
27-
suspend fun getThemeData() =
28-
client.get<ThemeDataResponse>("$baseUrl/theme.json")
29-
30-
companion object {
31-
fun createHttpClient(
32-
engine: HttpClientEngine,
33-
json: Json = createJson(),
34-
enableNetworkLogs: Boolean
35-
) = HttpClient(engine) {
36-
install(JsonFeature) {
37-
serializer = KotlinxSerializer(json)
38-
}
39-
if (enableNetworkLogs) {
40-
install(Logging) {
41-
logger = Logger.DEFAULT
42-
level = LogLevel.INFO
43-
}
44-
}
45-
}
46-
47-
fun createHttpClient(
48-
json: Json = createJson(),
49-
enableNetworkLogs: Boolean
50-
) = HttpClient {
51-
install(JsonFeature) {
52-
serializer = KotlinxSerializer(json)
53-
}
54-
if (enableNetworkLogs) {
55-
install(Logging) {
56-
logger = Logger.DEFAULT
57-
level = LogLevel.INFO
58-
}
59-
}
60-
}
61-
62-
fun createJson() = Json { isLenient = true; ignoreUnknownKeys = true }
63-
}
6416
}

0 commit comments

Comments
 (0)