1
1
package com.mubarak.wikinews.ui.home
2
2
3
3
import android.content.res.Configuration
4
- import android.util.Log
5
4
import android.widget.Toast
6
5
import androidx.compose.foundation.clickable
7
6
import androidx.compose.foundation.horizontalScroll
@@ -13,7 +12,9 @@ import androidx.compose.foundation.layout.PaddingValues
13
12
import androidx.compose.foundation.layout.Row
14
13
import androidx.compose.foundation.layout.Spacer
15
14
import androidx.compose.foundation.layout.fillMaxSize
15
+ import androidx.compose.foundation.layout.fillMaxWidth
16
16
import androidx.compose.foundation.layout.height
17
+ import androidx.compose.foundation.layout.heightIn
17
18
import androidx.compose.foundation.layout.padding
18
19
import androidx.compose.foundation.layout.width
19
20
import androidx.compose.foundation.layout.wrapContentSize
@@ -36,22 +37,24 @@ import androidx.compose.material3.TopAppBarScrollBehavior
36
37
import androidx.compose.material3.TopAppBarState
37
38
import androidx.compose.material3.rememberTopAppBarState
38
39
import androidx.compose.runtime.Composable
40
+ import androidx.compose.runtime.remember
39
41
import androidx.compose.ui.Alignment
40
42
import androidx.compose.ui.Modifier
43
+ import androidx.compose.ui.draw.blur
44
+ import androidx.compose.ui.draw.clip
41
45
import androidx.compose.ui.input.nestedscroll.nestedScroll
42
46
import androidx.compose.ui.platform.LocalContext
43
47
import androidx.compose.ui.res.stringResource
44
48
import androidx.compose.ui.tooling.preview.Preview
45
49
import androidx.compose.ui.unit.dp
46
50
import androidx.hilt.navigation.compose.hiltViewModel
47
51
import com.mubarak.wikinews.R
48
- import com.mubarak.wikinews.data.sources.remote.dto.Mobile
49
- import com.mubarak.wikinews.data.sources.remote.dto.Thumbnail
50
- import com.mubarak.wikinews.data.sources.remote.dto.Url
51
52
import com.mubarak.wikinews.data.sources.remote.dto.newsfeed.NewsFeed
53
+ import com.mubarak.wikinews.data.sources.remote.dto.newsfeed.mostread.article.Article
52
54
import com.mubarak.wikinews.data.sources.remote.dto.newsfeed.news.News
55
+ import com.mubarak.wikinews.data.sources.remote.dto.newsfeed.onthisday.Onthisday
53
56
import com.mubarak.wikinews.data.sources.remote.dto.newsfeed.tfa.Tfa
54
- import com.mubarak.wikinews.utils.DateFormatter
57
+ import com.mubarak.wikinews.utils.TimeStampConvertor
55
58
56
59
@Composable
57
60
fun HomeScreen (
@@ -76,7 +79,8 @@ fun HomeScreen(
76
79
}) {
77
80
when (uiState) {
78
81
HomeUiState .Error -> {
79
- Toast .makeText(context, " Some sort of error Happened!" , Toast .LENGTH_SHORT ).show()
82
+ // TODO: show error screen
83
+ Toast .makeText(context, " Some sort of error Happened!\n Check Internet Connection" , Toast .LENGTH_SHORT ).show()
80
84
}
81
85
82
86
HomeUiState .Loading -> {
@@ -99,8 +103,6 @@ fun NewsFeed(
99
103
modifier : Modifier = Modifier ,
100
104
newsFeed : NewsFeed ,
101
105
) {
102
-
103
- Log .d(" dateRime" , " NewsFeed: ${DateFormatter .formattedDate} " )
104
106
LazyColumn (
105
107
modifier = modifier,
106
108
contentPadding = PaddingValues (0 .dp),
@@ -111,9 +113,8 @@ fun NewsFeed(
111
113
112
114
val onThisDayNews = newsFeed.onThisDay ? : emptyList()
113
115
if (onThisDayNews.isNotEmpty() && onThisDayNews[0 ].pages.isNotEmpty()) {
114
- items(items = onThisDayNews.take(8 )) {
115
- OnThisDaySection (onThisDay = it, modifier = modifier)
116
- NewsItemDivider ()
116
+ item {
117
+ OnThisDaySection (onThisDay = onThisDayNews)
117
118
}
118
119
}
119
120
@@ -123,6 +124,67 @@ fun NewsFeed(
123
124
FeedListNewsSection (news = newsFeedArticle) // news
124
125
}
125
126
}
127
+
128
+ val mostReadArticles = newsFeed.mostRead?.articles ? : emptyList()
129
+ if (mostReadArticles.isNotEmpty()) {
130
+ items(mostReadArticles.take(8 )){
131
+ MostReadArticleSection (article = it)
132
+ }
133
+ }
134
+ }
135
+ }
136
+
137
+ @Composable
138
+ fun MostReadArticleSection (
139
+ modifier : Modifier = Modifier ,
140
+ article : Article
141
+ ) {
142
+ MostReadArticles (article = article, modifier = modifier)
143
+ NewsItemDivider ()
144
+ }
145
+
146
+ @Composable
147
+ fun MostReadArticles (
148
+ modifier : Modifier = Modifier ,
149
+ article : Article
150
+ ) {
151
+
152
+ val timeStamp = remember(article.timestamp) {
153
+ TimeStampConvertor .formatTimestampToUtc(article.timestamp)
154
+ }
155
+ Column (
156
+ modifier = modifier
157
+ .fillMaxWidth()
158
+ .padding(16 .dp)
159
+ ) {
160
+ val imgModifier = Modifier
161
+ .heightIn(min = 180 .dp, max = 200 .dp)
162
+ .fillMaxWidth()
163
+ .clip(shape = MaterialTheme .shapes.small)
164
+ .blur(90 .dp) // change it to appropriate
165
+
166
+ NewsFeedImage (
167
+ modifier = imgModifier,
168
+ imgUrl = article.thumbnail?.imgUrl
169
+ )
170
+ Spacer (modifier = Modifier .height(16 .dp))
171
+
172
+ NewsTitle (
173
+ text = article.longDescription,
174
+ modifier = Modifier .padding(bottom = 8 .dp)
175
+ )
176
+ Text (
177
+ text = timeStamp,
178
+ style = MaterialTheme .typography.bodySmall
179
+ )
180
+ }
181
+ }
182
+
183
+ @Composable
184
+ fun OnThisDaySection (modifier : Modifier = Modifier , onThisDay : List <Onthisday >) {
185
+ for (onThisDayItem in onThisDay) {
186
+ OnThisDaySection (onThisDay = onThisDayItem, modifier = modifier)
187
+ NewsItemDivider ()
126
188
}
127
189
}
128
190
@@ -150,18 +212,22 @@ fun TfaSection(
150
212
NewsTitle (
151
213
text = stringResource(id = R .string.today_featured_article),
152
214
color = MaterialTheme .colorScheme.onTertiaryContainer,
153
- modifier = Modifier .padding(
215
+ modifier = modifier .padding(
154
216
start = 16 .dp, top = 16 .dp, end = 16 .dp
155
217
)
156
218
)
157
219
158
- tfa?.let {
220
+ tfa?.let {
159
221
TodayFeaturedArticle (tfa = tfa, modifier = Modifier .clickable { })
160
222
}
161
223
162
224
NewsItemDivider ()
163
-
164
- NewsTitle (text = " On This Day Happened" ,color = MaterialTheme .colorScheme.onTertiaryContainer, modifier = Modifier .padding(16 .dp))
225
+
226
+ NewsTitle (
227
+ text = stringResource(id = R .string.onThis_day_happened),
228
+ color = MaterialTheme .colorScheme.onTertiaryContainer,
229
+ modifier = Modifier .padding(16 .dp)
230
+ )
165
231
}
166
232
167
233
@@ -181,21 +247,27 @@ fun FeedListNewsSection(
181
247
.height(IntrinsicSize .Max )
182
248
.padding(16 .dp), horizontalArrangement = Arrangement .spacedBy(16 .dp)
183
249
) {
184
- for (newsitem in news) {
185
- if (newsitem .newsArticles.isNotEmpty()) {
186
- NewsArticlesFeed (news = newsitem .newsArticles[0 ], modifier = modifier)
250
+ for (newsItem in news) {
251
+ if (newsItem .newsArticles.isNotEmpty()) {
252
+ NewsArticlesFeed (news = newsItem .newsArticles[0 ], modifier = modifier)
187
253
}
188
254
}
189
255
}
190
- Spacer (modifier = Modifier .height(16 .dp))
191
- NewsItemDivider ()
256
+
257
+ NewsTitle (
258
+ text = stringResource(id = R .string.most_read),
259
+ color = MaterialTheme .colorScheme.onTertiaryContainer,
260
+ modifier = Modifier .padding(
261
+ start = 16 .dp, top = 16 .dp, end = 16 .dp
262
+ )
263
+ )
192
264
}
193
265
}
194
266
195
267
@Composable
196
268
fun NewsItemDivider (modifier : Modifier = Modifier ) {
197
269
HorizontalDivider (
198
- modifier = Modifier .padding(horizontal = 14 .dp),
270
+ modifier = modifier .padding(horizontal = 14 .dp),
199
271
color = MaterialTheme .colorScheme.onSurface.copy(alpha = 0.07f )
200
272
)
201
273
}
0 commit comments