Skip to content

Commit 30471c4

Browse files
committed
update
1 parent a8e2422 commit 30471c4

File tree

11 files changed

+295
-13
lines changed

11 files changed

+295
-13
lines changed

app/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
plugins {
33
alias(libs.plugins.androidApp)
44
alias(libs.plugins.kotlin.android)
5+
alias(libs.plugins.kotlin.serialization)
56
}
67

78
android {
@@ -64,6 +65,13 @@ dependencies {
6465

6566
implementation(libs.androidx.navigation.compose)
6667

68+
val ktor_version = "2.3.6"
69+
implementation("io.ktor:ktor-client-core:$ktor_version")
70+
implementation("io.ktor:ktor-client-okhttp:$ktor_version")
71+
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
72+
implementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
73+
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
74+
6775
testImplementation(libs.junit)
6876
androidTestImplementation(libs.androidx.test.ext.junit)
6977
androidTestImplementation(libs.espresso.core)

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
33

4+
<uses-permission android:name="android.permission.INTERNET" />
5+
46
<application
57
android:allowBackup="true"
68
android:icon="@mipmap/ic_launcher"
79
android:label="@string/app_name"
810
android:roundIcon="@mipmap/ic_launcher_round"
911
android:supportsRtl="true"
12+
android:usesCleartextTraffic="true"
1013
android:theme="@style/Theme.JetpackDemo">
1114
<activity
1215
android:name=".MainActivity"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.wzq.jd.compose.app.data
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class ArticleItem(
7+
val adminAdd: Boolean,
8+
val apkLink: String,
9+
val audit: Int,
10+
val author: String,
11+
val canEdit: Boolean,
12+
val chapterId: Int,
13+
val chapterName: String,
14+
val collect: Boolean,
15+
val courseId: Int,
16+
val desc: String,
17+
val descMd: String,
18+
val envelopePic: String,
19+
val fresh: Boolean,
20+
val host: String,
21+
val id: Int,
22+
val isAdminAdd: Boolean,
23+
val link: String,
24+
val niceDate: String,
25+
val niceShareDate: String,
26+
val origin: String,
27+
val prefix: String,
28+
val projectLink: String,
29+
val publishTime: Long,
30+
val realSuperChapterId: Int,
31+
val selfVisible: Int,
32+
val shareDate: Long,
33+
val shareUser: String,
34+
val superChapterId: Int,
35+
val superChapterName: String,
36+
// val tags: List<String>,
37+
val title: String,
38+
val type: Int,
39+
val userId: Int,
40+
val visible: Int,
41+
val zan: Int
42+
)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//package com.wzq.sample.data.remote
2+
//
3+
//import com.google.gson.JsonElement
4+
//import com.wzq.sample.data.model.ArticleList
5+
//import com.wzq.sample.data.model.Banner
6+
//import com.wzq.sample.data.model.Category
7+
//import com.wzq.sample.data.model.HotKeywordsItem
8+
//import com.wzq.sample.data.model.Response
9+
//import retrofit2.http.*
10+
//
11+
//
12+
///**
13+
// * create by wzq on 2021/4/6
14+
// *
15+
// */
16+
//interface MainApi {
17+
//
18+
// /**
19+
// * 获取轮播图
20+
// * http://www.wanandroid.com/banner/json
21+
// */
22+
// @GET("banner/json")
23+
// suspend fun getBanners(): Response<List<Banner>>
24+
//
25+
// /**
26+
// * 获取文章列表
27+
// * http://www.wanandroid.com/article/list/0/json
28+
// * @param pageNum
29+
// */
30+
// @GET("article/list/{pageNum}/json")
31+
// suspend fun getArticles(@Path("pageNum") pageNum: Int): Response<ArticleList>
32+
//
33+
// @GET("article/listproject/{pageNum}/json")
34+
// suspend fun getProjects(@Path("pageNum") pageNum: Int): Response<ArticleList>
35+
//
36+
// /**
37+
// * 获取知识体系
38+
// * http://www.wanandroid.com/tree/json
39+
// */
40+
// @GET("tree/json")
41+
// suspend fun getCategory(): Response<List<Category>>
42+
//
43+
// /**
44+
// * 知识体系下的文章
45+
// * http://www.wanandroid.com/article/list/0/json?cid=168
46+
// * @param page
47+
// * @param cid
48+
// */
49+
// @GET("article/list/{page}/json")
50+
// suspend fun getCategoryArticles(
51+
// @Path("page") page: Int,
52+
// @Query("cid") cid: Int
53+
// ): Response<ArticleList>
54+
//
55+
// /**
56+
// * 登录
57+
// * http://www.wanandroid.com/user/login
58+
// * @param username
59+
// * @param password
60+
// */
61+
// @POST("user/login")
62+
// @FormUrlEncoded
63+
// suspend fun login(
64+
// @Field("username") username: String,
65+
// @Field("password") password: String
66+
// ): Response<JsonElement>
67+
//
68+
// /**
69+
// * 搜索热词
70+
// * http://www.wanandroid.com/hotkey/json
71+
// */
72+
// @GET("hotkey/json")
73+
// suspend fun getHotSearchData(): Response<List<HotKeywordsItem>>
74+
//
75+
// /**
76+
// * 搜索
77+
// * http://www.wanandroid.com/article/query/0/json
78+
// * @param page
79+
// * @param key
80+
// */
81+
// @POST("article/query/{page}/json")
82+
// @FormUrlEncoded
83+
// suspend fun queryBySearchKey(
84+
// @Path("page") page: Int,
85+
// @Field("k") key: String
86+
// ): Response<ArticleList>
87+
//}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.wzq.jd.compose.app.data
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
/**
7+
* create by wzq on 2023/11/27
8+
*
9+
*/
10+
@Serializable
11+
data class NetResult<T>(
12+
val data: T,
13+
val errorCode: Int,
14+
val errorMsg: String
15+
)
16+
@Serializable
17+
data class NetResultList<T>(
18+
val curPage: Int,
19+
@SerialName("datas")
20+
val listData: List<T>,
21+
val offset: Int,
22+
val over: Boolean,
23+
val pageCount: Int,
24+
val size: Int,
25+
val total: Int
26+
)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.wzq.jd.compose.app.data
2+
3+
import io.ktor.client.HttpClient
4+
import io.ktor.client.call.body
5+
import io.ktor.client.engine.okhttp.OkHttp
6+
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
7+
import io.ktor.client.request.get
8+
import io.ktor.serialization.kotlinx.json.json
9+
import kotlinx.serialization.json.Json
10+
import okhttp3.logging.HttpLoggingInterceptor
11+
12+
/**
13+
* create by wzq on 2023/11/27
14+
*
15+
*/
16+
object NetworkUtil {
17+
18+
object Url {
19+
const val HomeArticleList = "https://www.wanandroid.com/article/list/0/json"
20+
}
21+
22+
suspend inline fun <reified T> requestGet(url: String): T {
23+
return client.get(url).body()
24+
}
25+
26+
val client by lazy {
27+
HttpClient(OkHttp) {
28+
engine {
29+
config {
30+
followRedirects(true)
31+
}
32+
addInterceptor(HttpLoggingInterceptor().apply {
33+
level = HttpLoggingInterceptor.Level.BODY
34+
})
35+
36+
}
37+
38+
install(ContentNegotiation) {
39+
json(Json {
40+
prettyPrint = true
41+
isLenient = true
42+
ignoreUnknownKeys = true
43+
})
44+
}
45+
}
46+
}
47+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.wzq.jd.compose.app.page
2+
3+
import androidx.compose.foundation.layout.Row
4+
import androidx.compose.foundation.layout.fillMaxWidth
5+
import androidx.compose.foundation.layout.padding
6+
import androidx.compose.material3.Card
7+
import androidx.compose.material3.ExperimentalMaterial3Api
8+
import androidx.compose.material3.MaterialTheme
9+
import androidx.compose.material3.Text
10+
import androidx.compose.runtime.Composable
11+
import androidx.compose.ui.Modifier
12+
import androidx.compose.ui.unit.dp
13+
import com.wzq.jd.compose.app.data.ArticleItem
14+
15+
/**
16+
* create by wzq on 2023/11/27
17+
*
18+
*/
19+
@OptIn(ExperimentalMaterial3Api::class)
20+
@Composable
21+
fun ArticleItemPage(itemData: ArticleItem) {
22+
Card(
23+
onClick = { /*TODO*/ }, modifier = Modifier
24+
.padding(8.dp)
25+
.fillMaxWidth()
26+
) {
27+
Text(
28+
text = itemData.title,
29+
modifier = Modifier.padding(start = 16.dp, end = 16.dp, top = 16.dp, bottom = 8.dp),
30+
style = MaterialTheme.typography.titleMedium
31+
)
32+
Row(modifier = Modifier.padding(top = 8.dp, start = 16.dp, end = 16.dp, bottom = 16.dp)) {
33+
Text(
34+
text = itemData.chapterName,
35+
modifier = Modifier.weight(1f),
36+
style = MaterialTheme.typography.labelMedium,
37+
color = MaterialTheme.colorScheme.tertiary
38+
)
39+
Text(
40+
text = itemData.niceShareDate,
41+
style = MaterialTheme.typography.labelMedium,
42+
color = MaterialTheme.colorScheme.tertiary
43+
)
44+
}
45+
}
46+
}

app/src/main/java/com/wzq/jd/compose/app/page/HomePage.kt

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,44 @@ package com.wzq.jd.compose.app.page
22

33
import androidx.compose.foundation.lazy.LazyColumn
44
import androidx.compose.foundation.lazy.items
5-
import androidx.compose.material3.Text
65
import androidx.compose.runtime.Composable
6+
import androidx.compose.runtime.LaunchedEffect
77
import androidx.compose.runtime.mutableStateListOf
88
import androidx.compose.runtime.remember
9+
import androidx.compose.runtime.rememberCoroutineScope
10+
import com.wzq.jd.compose.app.data.ArticleItem
11+
import com.wzq.jd.compose.app.data.NetResult
12+
import com.wzq.jd.compose.app.data.NetResultList
13+
import com.wzq.jd.compose.app.data.NetworkUtil
14+
import kotlinx.coroutines.launch
915

1016
/**
1117
* create by wzq on 2023/11/27
1218
*
1319
*/
1420
@Composable
1521
fun HomePage() {
22+
val coroutineScope = rememberCoroutineScope()
1623
val data = remember {
17-
mutableStateListOf<String>().apply {
18-
repeat(20) {
19-
add("i$it")
24+
mutableStateListOf<ArticleItem>()
25+
}
26+
LaunchedEffect(key1 = true, block = {
27+
coroutineScope.launch {
28+
try {
29+
val response: NetResult<NetResultList<ArticleItem>> =
30+
NetworkUtil.requestGet(NetworkUtil.Url.HomeArticleList)
31+
data.addAll(response.data.listData)
32+
} catch (e: Exception) {
33+
e.printStackTrace()
2034
}
35+
2136
}
22-
}
23-
LazyColumn(content = {
37+
})
38+
39+
LazyColumn(content = {
2440
items(data) {
25-
Text(text = "item -> $it")
41+
ArticleItemPage(itemData = it)
2642
}
2743
})
28-
}
44+
45+
}

app/src/main/java/com/wzq/jd/compose/app/page/MainPage.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package com.wzq.jd.compose.app.page
44

55
import androidx.compose.foundation.ExperimentalFoundationApi
6+
import androidx.compose.foundation.background
67
import androidx.compose.foundation.layout.padding
78
import androidx.compose.foundation.pager.HorizontalPager
89
import androidx.compose.foundation.pager.PagerState
@@ -42,7 +43,12 @@ fun MainPage(navController: NavHostController) {
4243
MainBottomBar(pagerState)
4344
}
4445
) { paddingValues ->
45-
HorizontalPager(state = pagerState, modifier = Modifier.padding(paddingValues)) {
46+
HorizontalPager(
47+
state = pagerState,
48+
modifier = Modifier
49+
.padding(paddingValues)
50+
.background(color = MaterialTheme.colorScheme.background)
51+
) {
4652
when (it) {
4753
0 -> HomePage()
4854
else -> {

build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ plugins {
55
alias(libs.plugins.android.library) apply false
66
alias(libs.plugins.kotlin.android) apply false
77
alias(libs.plugins.google.ksp) apply false
8-
9-
// id ("org.jetbrains.kotlin.plugin.serialization") version "1.8.0" apply false
8+
alias(libs.plugins.kotlin.serialization) apply false
109
}
1110
true
1211

0 commit comments

Comments
 (0)