Skip to content

Commit ac35d19

Browse files
committed
Update news app
- Update dependencies - Update News details screen - Update News item - Add Category model - Add filter chips
1 parent 9564b3d commit ac35d19

File tree

22 files changed

+348
-168
lines changed

22 files changed

+348
-168
lines changed

app/build.gradle

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ android {
2828
}
2929
}
3030
compileOptions {
31-
sourceCompatibility JavaVersion.VERSION_1_8
32-
targetCompatibility JavaVersion.VERSION_1_8
31+
sourceCompatibility JavaVersion.VERSION_17
32+
targetCompatibility JavaVersion.VERSION_17
3333
}
3434
kotlinOptions {
35-
jvmTarget = '1.8'
35+
jvmTarget = '17'
3636
}
3737
buildFeatures {
3838
compose true
3939
}
4040
composeOptions {
4141

42-
kotlinCompilerExtensionVersion '1.1.1'
42+
kotlinCompilerExtensionVersion '1.4.0'
4343
}
4444
packagingOptions {
4545
resources {
@@ -50,31 +50,31 @@ android {
5050

5151
dependencies {
5252

53-
implementation 'androidx.core:core-ktx:1.9.0'
54-
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
55-
implementation 'androidx.activity:activity-compose:1.6.1'
53+
implementation 'androidx.core:core-ktx:1.10.1'
54+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
55+
implementation 'androidx.activity:activity-compose:1.7.2'
5656
implementation "androidx.compose.ui:ui:$compose_version"
5757
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
58-
implementation 'androidx.compose.material3:material3:1.0.1'
58+
implementation 'androidx.compose.material3:material3:1.1.1'
5959

60-
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"
60+
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1"
6161
//retrofit
6262
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
6363
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
6464
//coil
6565
implementation("io.coil-kt:coil-compose:2.2.2")
6666

6767
testImplementation 'junit:junit:4.13.2'
68-
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
69-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
68+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
69+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
7070
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
7171
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
7272
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
7373

74-
implementation "androidx.navigation:navigation-compose:2.5.3"
75-
implementation 'androidx.browser:browser:1.4.0'
74+
implementation "androidx.navigation:navigation-compose:2.6.0"
75+
implementation 'androidx.browser:browser:1.5.0'
7676

7777
testImplementation 'com.squareup.okhttp3:mockwebserver:4.9.0'
78-
testImplementation 'org.mockito:mockito-core:2.23.0'
78+
testImplementation 'org.mockito:mockito-core:2.25.0'
7979
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'
8080
}

app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import androidx.compose.material3.Surface
99
import androidx.compose.runtime.Composable
1010
import androidx.compose.ui.Modifier
1111
import androidx.compose.ui.tooling.preview.Preview
12-
import com.lahsuak.apps.jetpackcomposebasic.ui.MyAppNavHost
12+
import com.lahsuak.apps.jetpackcomposebasic.ui.navigation.MyAppNavHost
1313
import com.lahsuak.apps.jetpackcomposebasic.ui.theme.JetPackComposeBasicTheme
1414

1515
class MainActivity : ComponentActivity() {

app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/api/NewApi.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ interface NewApi {
1313
const val BASE_URL = "https://saurav.tech"//NewsAPI/top-headlines/category/general/in.json"
1414
}
1515

16-
@GET("/NewsAPI/top-headlines/category/general/in.json")
16+
@GET("/NewsAPI/top-headlines/category/{category}/in.json")
1717
suspend fun getNews(
18-
// @Path("category")
19-
// category: String
18+
@Path("category")
19+
category: String
2020
): NewsParentModel
2121

2222
@GET("/NewsAPI/top-headlines/category/general/in.json")

app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/repo/NewsRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.lahsuak.apps.jetpackcomposebasic.model.NewsParentModel
88
class NewsRepository(private val apiInterface: NewApi= ApiClient.apiInterface) {
99
suspend fun getNews(category: String): NewsParentModel {
1010
return apiInterface.getNews(
11-
// category
11+
category
1212
)
1313
}
1414
}

app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/CromeTab.kt

Lines changed: 0 additions & 15 deletions
This file was deleted.

app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/Helper.kt

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/NavigationItem.kt

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.lahsuak.apps.jetpackcomposebasic.ui.components
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.padding
5+
import androidx.compose.foundation.lazy.LazyRow
6+
import androidx.compose.foundation.lazy.items
7+
import androidx.compose.material.icons.Icons
8+
import androidx.compose.material.icons.filled.List
9+
import androidx.compose.material3.ExperimentalMaterial3Api
10+
import androidx.compose.material3.FilterChip
11+
import androidx.compose.material3.Icon
12+
import androidx.compose.material3.IconButton
13+
import androidx.compose.material3.Text
14+
import androidx.compose.runtime.Composable
15+
import androidx.compose.ui.Modifier
16+
import androidx.compose.ui.res.stringResource
17+
import androidx.compose.ui.unit.dp
18+
import com.lahsuak.apps.jetpackcomposebasic.R
19+
import com.lahsuak.apps.jetpackcomposebasic.ui.components.model.Category
20+
21+
@OptIn(ExperimentalMaterial3Api::class)
22+
@Composable
23+
fun ChipGroup(
24+
categories: List<Category>,
25+
selectedCategory: Category,
26+
onSelectedChanged: (Category) -> Unit = {},
27+
) {
28+
Column(modifier = Modifier.padding(8.dp)) {
29+
LazyRow {
30+
item {
31+
IconButton(
32+
onClick = { /* no-op */ }
33+
) {
34+
Icon(
35+
imageVector = Icons.Default.List,
36+
contentDescription = stringResource(R.string.label_filters)
37+
)
38+
}
39+
}
40+
items(categories) {
41+
FilterChip(
42+
label = {
43+
Text(it.name.uppercase())
44+
},
45+
modifier = Modifier.padding(horizontal = 4.dp),
46+
selected = selectedCategory == it,
47+
onClick = {
48+
onSelectedChanged(it)
49+
},
50+
)
51+
}
52+
}
53+
}
54+
}

app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/NewsItem.kt renamed to app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/components/NewsItem.kt

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
package com.lahsuak.apps.jetpackcomposebasic.ui
1+
package com.lahsuak.apps.jetpackcomposebasic.ui.components
22

33
import androidx.compose.foundation.Image
44
import androidx.compose.foundation.clickable
5-
import androidx.compose.foundation.layout.*
5+
import androidx.compose.foundation.layout.Column
6+
import androidx.compose.foundation.layout.fillMaxSize
7+
import androidx.compose.foundation.layout.fillMaxWidth
8+
import androidx.compose.foundation.layout.height
9+
import androidx.compose.foundation.layout.padding
10+
import androidx.compose.material3.Card
611
import androidx.compose.material3.MaterialTheme
712
import androidx.compose.material3.Surface
813
import androidx.compose.material3.Text
@@ -14,7 +19,6 @@ import androidx.compose.ui.platform.LocalContext
1419
import androidx.compose.ui.text.style.TextOverflow
1520
import androidx.compose.ui.tooling.preview.Preview
1621
import androidx.compose.ui.unit.dp
17-
import androidx.navigation.NavController
1822
import coil.compose.rememberAsyncImagePainter
1923
import coil.request.ImageRequest
2024
import com.lahsuak.apps.jetpackcomposebasic.R
@@ -23,6 +27,7 @@ import com.lahsuak.apps.jetpackcomposebasic.ui.theme.JetPackComposeBasicTheme
2327

2428
@Composable
2529
fun NewsItem(news: News, onClick: () -> Unit) {
30+
// coil image painter
2631
val painter = rememberAsyncImagePainter(
2732
model = ImageRequest.Builder(LocalContext.current)
2833
.data(news.urlToImage)
@@ -31,30 +36,36 @@ fun NewsItem(news: News, onClick: () -> Unit) {
3136
.build(),
3237
contentScale = ContentScale.FillWidth
3338
)
34-
Column(modifier = Modifier.fillMaxWidth().clickable {
35-
onClick()
36-
}) {
37-
Image(
38-
painter = painter,
39-
contentDescription = null,
40-
modifier = Modifier
41-
.fillMaxWidth()
42-
.height(200.dp),
43-
)
44-
Text(
45-
text = news.title,
46-
style = MaterialTheme.typography.bodyMedium,
47-
maxLines = 1,
48-
modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp)
49-
)
50-
Text(
51-
text = news.description,
52-
style = MaterialTheme.typography.bodySmall,
53-
maxLines = 3,
54-
overflow = TextOverflow.Ellipsis,
55-
color = Color.Gray,
56-
modifier = Modifier.padding(horizontal = 8.dp)
57-
)
39+
Card(modifier = Modifier
40+
.fillMaxWidth()
41+
.padding(vertical = 4.dp)
42+
.clickable {
43+
onClick()
44+
}) {
45+
Column() {
46+
Image(
47+
painter = painter,
48+
contentDescription = null,
49+
modifier = Modifier
50+
.fillMaxWidth()
51+
.height(200.dp),
52+
contentScale = ContentScale.Crop
53+
)
54+
Text(
55+
text = news.title,
56+
style = MaterialTheme.typography.bodyMedium,
57+
maxLines = 1,
58+
modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp)
59+
)
60+
Text(
61+
text = news.description,
62+
style = MaterialTheme.typography.bodySmall,
63+
maxLines = 3,
64+
overflow = TextOverflow.Ellipsis,
65+
color = Color.Gray,
66+
modifier = Modifier.padding(start = 8.dp, end = 8.dp, bottom = 8.dp)
67+
)
68+
}
5869
}
5970
}
6071

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.lahsuak.apps.jetpackcomposebasic.ui.components.model
2+
3+
import android.os.Parcelable
4+
import kotlinx.parcelize.Parcelize
5+
6+
@Parcelize
7+
data class Category(
8+
val pos: Int,
9+
val name: String,
10+
) : Parcelable
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.lahsuak.apps.jetpackcomposebasic.ui.components.model
2+
3+
enum class CategoryType {
4+
GENERAL,
5+
ENTERTAINMENT,
6+
SPORTS
7+
}

app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/detailscreen/Detailscreen.kt

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/src/main/java/com/lahsuak/apps/jetpackcomposebasic/ui/home/HomeScreen.kt

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)