Skip to content

Commit 17b0d12

Browse files
committed
refactor: standardize article calls
1 parent 20e11fd commit 17b0d12

File tree

4 files changed

+17
-20
lines changed

4 files changed

+17
-20
lines changed

app/src/main/java/to/bitkit/models/widget/NewsModel.kt renamed to app/src/main/java/to/bitkit/models/widget/ArticleModel.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import java.time.temporal.ChronoUnit
1010
import java.util.Locale
1111

1212
@Serializable
13-
data class NewsModel(
13+
data class ArticleModel(
1414
val title: String,
1515
val timeAgo: String,
1616
val link: String,
1717
val publisher: String
1818
)
1919

20-
fun ArticleDTO.toNewsModel() = NewsModel(
20+
fun ArticleDTO.toArticleModel() = ArticleModel(
2121
title = this.title,
2222
timeAgo = timeAgo(this.publishedDate),
2323
link = this.link,
@@ -47,7 +47,7 @@ private fun timeAgo(dateString: String): String {
4747
}
4848

4949
if (parsedDateTime == null) {
50-
Logger.debug("Failed to parse date: Unparseable date: \"$dateString\" [NewsModel.kt:46]")
50+
Logger.debug("Failed to parse date: Unparseable date: $dateString")
5151
return ""
5252
}
5353

@@ -66,7 +66,7 @@ private fun timeAgo(dateString: String): String {
6666
else -> "$diffMonths months ago"
6767
}
6868
} catch (e: Exception) {
69-
Logger.debug("An unexpected error occurred while parsing date: ${e.message}")
69+
Logger.warn("An unexpected error occurred while parsing date: ${e.message}")
7070
""
7171
}
7272
}

app/src/main/java/to/bitkit/repositories/WidgetsRepo.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package to.bitkit.repositories
22

33
import kotlinx.coroutines.CoroutineDispatcher
44
import kotlinx.coroutines.delay
5-
import kotlinx.coroutines.flow.map
65
import kotlinx.coroutines.withContext
76
import to.bitkit.data.WidgetsStore
87
import to.bitkit.data.widgets.NewsService
98
import to.bitkit.di.BgDispatcher
10-
import to.bitkit.models.widget.toNewsModel
119
import to.bitkit.utils.Logger
1210
import javax.inject.Inject
1311
import javax.inject.Singleton
@@ -23,13 +21,13 @@ class WidgetsRepo @Inject constructor(
2321

2422
val articlesFlow = widgetsStore.data
2523

26-
suspend fun updateNewsInLoop() {
27-
updateNews()
24+
suspend fun updateArticlesInLoop() {
25+
updateArticles()
2826
delay(refreshInterval)
29-
updateNewsInLoop()
27+
updateArticlesInLoop()
3028
}
3129

32-
suspend fun updateNews(): Result<Unit> = withContext(bgDispatcher) {
30+
suspend fun updateArticles(): Result<Unit> = withContext(bgDispatcher) {
3331
return@withContext try {
3432
val news = newsService.fetchLatestNews().take(10)
3533
widgetsStore.updateArticles(news)

app/src/main/java/to/bitkit/ui/screens/wallets/HomeScreen.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import to.bitkit.R
5656
import to.bitkit.env.Env
5757
import to.bitkit.ext.requiresPermission
5858
import to.bitkit.models.Suggestion
59-
import to.bitkit.models.widget.NewsModel
59+
import to.bitkit.models.widget.ArticleModel
6060
import to.bitkit.ui.LocalBalances
6161
import to.bitkit.ui.Routes
6262
import to.bitkit.ui.activityListViewModel
@@ -324,7 +324,7 @@ fun HomeScreen(
324324
@Composable
325325
private fun HomeContentView(
326326
uiState: MainUiState,
327-
article: NewsModel?,
327+
article: ArticleModel?,
328328
suggestions: List<Suggestion>,
329329
onRemoveSuggestion: (Suggestion) -> Unit,
330330
onClickSuggestion: (Suggestion) -> Unit,

app/src/main/java/to/bitkit/ui/screens/wallets/HomeViewModel.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,16 @@ import kotlinx.coroutines.flow.StateFlow
1010
import kotlinx.coroutines.flow.asStateFlow
1111
import kotlinx.coroutines.flow.combine
1212
import kotlinx.coroutines.flow.first
13-
import kotlinx.coroutines.flow.last
1413
import kotlinx.coroutines.flow.map
1514
import kotlinx.coroutines.flow.stateIn
1615
import kotlinx.coroutines.flow.update
1716
import kotlinx.coroutines.launch
1817
import to.bitkit.data.AppStorage
1918
import to.bitkit.data.SettingsStore
2019
import to.bitkit.models.Suggestion
21-
import to.bitkit.models.WidgetType
2220
import to.bitkit.models.toSuggestionOrNull
23-
import to.bitkit.models.widget.NewsModel
24-
import to.bitkit.models.widget.toNewsModel
21+
import to.bitkit.models.widget.ArticleModel
22+
import to.bitkit.models.widget.toArticleModel
2523
import to.bitkit.repositories.WalletRepo
2624
import to.bitkit.repositories.WidgetsRepo
2725
import javax.inject.Inject
@@ -36,9 +34,9 @@ class HomeViewModel @Inject constructor(
3634
) : ViewModel() {
3735

3836
val suggestions: StateFlow<List<Suggestion>> = createSuggestionsFlow()
39-
private val articles: StateFlow<List<NewsModel>> = createArticlesFlow()
37+
private val articles: StateFlow<List<ArticleModel>> = createArticlesFlow()
4038
private val _currentArticle = MutableStateFlow(articles.value.firstOrNull())
41-
val currentArticle: StateFlow<NewsModel?> = _currentArticle.asStateFlow()
39+
val currentArticle: StateFlow<ArticleModel?> = _currentArticle.asStateFlow()
4240

4341
init {
4442
setupArticles()
@@ -101,15 +99,16 @@ class HomeViewModel @Inject constructor(
10199
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
102100
}
103101

104-
private fun createArticlesFlow(): StateFlow<List<NewsModel>> {
105-
val articles = widgetsRepo.articlesFlow.map { it.articles.map { article -> article.toNewsModel() } }
102+
private fun createArticlesFlow(): StateFlow<List<ArticleModel>> {
103+
val articles = widgetsRepo.articlesFlow.map { it.articles.map { article -> article.toArticleModel() } }
106104
return articles.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
107105
}
108106

109107
private fun setupArticles() {
110108
viewModelScope.launch {
111109
val settings = settingsStore.data.first()
112110
if (!settings.showWidgets) return@launch //TODO also filter !settings.widgets.map { it.type }.contains(WidgetType.NEWS) when implement drag and drop
111+
widgetsRepo.updateArticles()
113112
articles.first { it.isNotEmpty() }
114113
getRandomArticle()
115114
}

0 commit comments

Comments
 (0)