Skip to content

Commit 4121579

Browse files
jkmasselclaude
andcommitted
Add WP.com-specific screens to Kotlin example app
Add Me, Site, Bot Conversations, and Support Conversations screens for WP.com-authenticated sites. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 995e5a2 commit 4121579

File tree

4 files changed

+459
-0
lines changed

4 files changed

+459
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package rs.wordpress.example.shared.ui.wpcom
2+
3+
import androidx.compose.foundation.layout.Box
4+
import androidx.compose.foundation.layout.fillMaxSize
5+
import androidx.compose.foundation.layout.padding
6+
import androidx.compose.foundation.lazy.LazyColumn
7+
import androidx.compose.foundation.lazy.items
8+
import androidx.compose.material.icons.Icons
9+
import androidx.compose.material.icons.automirrored.filled.ArrowBack
10+
import androidx.compose.material3.CircularProgressIndicator
11+
import androidx.compose.material3.ExperimentalMaterial3Api
12+
import androidx.compose.material3.Icon
13+
import androidx.compose.material3.IconButton
14+
import androidx.compose.material3.ListItem
15+
import androidx.compose.material3.MaterialTheme
16+
import androidx.compose.material3.Scaffold
17+
import androidx.compose.material3.Text
18+
import androidx.compose.material3.TopAppBar
19+
import androidx.compose.runtime.Composable
20+
import androidx.compose.runtime.LaunchedEffect
21+
import androidx.compose.runtime.getValue
22+
import androidx.compose.runtime.mutableStateOf
23+
import androidx.compose.runtime.remember
24+
import androidx.compose.runtime.setValue
25+
import androidx.compose.ui.Alignment
26+
import androidx.compose.ui.Modifier
27+
import org.jetbrains.compose.ui.tooling.preview.Preview
28+
import rs.wordpress.api.kotlin.WpComApiClient
29+
import rs.wordpress.api.kotlin.WpRequestResult
30+
import uniffi.wp_api.BotConversationSummary
31+
import uniffi.wp_api.ListBotConversationsParams
32+
import java.text.SimpleDateFormat
33+
import java.util.Date
34+
import java.util.Locale
35+
36+
@OptIn(ExperimentalMaterial3Api::class)
37+
@Composable
38+
@Preview
39+
fun WpComBotConversationsScreen(
40+
wpComApiClient: WpComApiClient,
41+
onBackClicked: () -> Unit = {}
42+
) {
43+
var conversations by remember { mutableStateOf<List<BotConversationSummary>>(emptyList()) }
44+
var error by remember { mutableStateOf<String?>(null) }
45+
var isLoading by remember { mutableStateOf(true) }
46+
47+
LaunchedEffect(Unit) {
48+
when (val result = wpComApiClient.request {
49+
it.supportBots().getBotConversationList(
50+
botId = "jetpack-chat-mobile",
51+
params = ListBotConversationsParams()
52+
)
53+
}) {
54+
is WpRequestResult.Success -> {
55+
conversations = result.response.data
56+
isLoading = false
57+
}
58+
else -> {
59+
error = result.toString()
60+
isLoading = false
61+
}
62+
}
63+
}
64+
65+
Scaffold(
66+
topBar = {
67+
TopAppBar(
68+
title = { Text("Bot Conversations") },
69+
navigationIcon = {
70+
IconButton(onClick = onBackClicked) {
71+
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
72+
}
73+
}
74+
)
75+
}
76+
) { paddingValues ->
77+
when {
78+
isLoading -> {
79+
Box(
80+
modifier = Modifier.fillMaxSize().padding(paddingValues),
81+
contentAlignment = Alignment.Center
82+
) {
83+
CircularProgressIndicator()
84+
}
85+
}
86+
error != null -> {
87+
Box(
88+
modifier = Modifier.fillMaxSize().padding(paddingValues),
89+
contentAlignment = Alignment.Center
90+
) {
91+
Text(
92+
text = "Error: $error",
93+
color = MaterialTheme.colorScheme.error
94+
)
95+
}
96+
}
97+
conversations.isEmpty() -> {
98+
Box(
99+
modifier = Modifier.fillMaxSize().padding(paddingValues),
100+
contentAlignment = Alignment.Center
101+
) {
102+
Text("No bot conversations found.")
103+
}
104+
}
105+
else -> {
106+
LazyColumn(
107+
modifier = Modifier.fillMaxSize().padding(paddingValues)
108+
) {
109+
items(conversations) { conversation ->
110+
ListItem(
111+
headlineContent = { Text(conversation.summaryMessage.content) },
112+
supportingContent = { Text(formatDate(conversation.createdAt)) }
113+
)
114+
}
115+
}
116+
}
117+
}
118+
}
119+
}
120+
121+
private fun formatDate(date: Date): String {
122+
return try {
123+
SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(date)
124+
} catch (e: Exception) {
125+
date.toString()
126+
}
127+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package rs.wordpress.example.shared.ui.wpcom
2+
3+
import androidx.compose.foundation.layout.Box
4+
import androidx.compose.foundation.layout.fillMaxSize
5+
import androidx.compose.foundation.layout.padding
6+
import androidx.compose.foundation.lazy.LazyColumn
7+
import androidx.compose.material.icons.Icons
8+
import androidx.compose.material.icons.automirrored.filled.ArrowBack
9+
import androidx.compose.material3.CircularProgressIndicator
10+
import androidx.compose.material3.ExperimentalMaterial3Api
11+
import androidx.compose.material3.Icon
12+
import androidx.compose.material3.IconButton
13+
import androidx.compose.material3.ListItem
14+
import androidx.compose.material3.MaterialTheme
15+
import androidx.compose.material3.Scaffold
16+
import androidx.compose.material3.Text
17+
import androidx.compose.material3.TopAppBar
18+
import androidx.compose.runtime.Composable
19+
import androidx.compose.runtime.LaunchedEffect
20+
import androidx.compose.runtime.getValue
21+
import androidx.compose.runtime.mutableStateOf
22+
import androidx.compose.runtime.remember
23+
import androidx.compose.runtime.setValue
24+
import androidx.compose.ui.Alignment
25+
import androidx.compose.ui.Modifier
26+
import org.jetbrains.compose.ui.tooling.preview.Preview
27+
import rs.wordpress.api.kotlin.WpComApiClient
28+
import rs.wordpress.api.kotlin.WpRequestResult
29+
import uniffi.wp_api.WpComUserInfo
30+
import java.text.SimpleDateFormat
31+
import java.util.Date
32+
import java.util.Locale
33+
34+
@OptIn(ExperimentalMaterial3Api::class)
35+
@Composable
36+
@Preview
37+
fun WpComMeScreen(
38+
wpComApiClient: WpComApiClient,
39+
onBackClicked: () -> Unit = {}
40+
) {
41+
var userInfo by remember { mutableStateOf<WpComUserInfo?>(null) }
42+
var error by remember { mutableStateOf<String?>(null) }
43+
var isLoading by remember { mutableStateOf(true) }
44+
45+
LaunchedEffect(Unit) {
46+
when (val result = wpComApiClient.request { it.me().get() }) {
47+
is WpRequestResult.Success -> {
48+
userInfo = result.response.data
49+
isLoading = false
50+
}
51+
else -> {
52+
error = result.toString()
53+
isLoading = false
54+
}
55+
}
56+
}
57+
58+
Scaffold(
59+
topBar = {
60+
TopAppBar(
61+
title = { Text("Me") },
62+
navigationIcon = {
63+
IconButton(onClick = onBackClicked) {
64+
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
65+
}
66+
}
67+
)
68+
}
69+
) { paddingValues ->
70+
when {
71+
isLoading -> {
72+
Box(
73+
modifier = Modifier.fillMaxSize().padding(paddingValues),
74+
contentAlignment = Alignment.Center
75+
) {
76+
CircularProgressIndicator()
77+
}
78+
}
79+
error != null -> {
80+
Box(
81+
modifier = Modifier.fillMaxSize().padding(paddingValues),
82+
contentAlignment = Alignment.Center
83+
) {
84+
Text(
85+
text = "Error: $error",
86+
color = MaterialTheme.colorScheme.error
87+
)
88+
}
89+
}
90+
userInfo != null -> {
91+
val info = userInfo!!
92+
LazyColumn(
93+
modifier = Modifier.fillMaxSize().padding(paddingValues)
94+
) {
95+
item {
96+
ListItem(
97+
headlineContent = { Text("Display Name") },
98+
supportingContent = { Text(info.displayName) }
99+
)
100+
}
101+
item {
102+
ListItem(
103+
headlineContent = { Text("Username") },
104+
supportingContent = { Text(info.username) }
105+
)
106+
}
107+
item {
108+
ListItem(
109+
headlineContent = { Text("Email") },
110+
supportingContent = { Text(info.email) }
111+
)
112+
}
113+
item {
114+
ListItem(
115+
headlineContent = { Text("Site Count") },
116+
supportingContent = { Text(info.siteCount.toString()) }
117+
)
118+
}
119+
item {
120+
ListItem(
121+
headlineContent = { Text("Created Date") },
122+
supportingContent = { Text(formatDate(info.creationDate)) }
123+
)
124+
}
125+
}
126+
}
127+
}
128+
}
129+
}
130+
131+
private fun formatDate(date: Date): String {
132+
return try {
133+
SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(date)
134+
} catch (e: Exception) {
135+
date.toString()
136+
}
137+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package rs.wordpress.example.shared.ui.wpcom
2+
3+
import androidx.compose.foundation.clickable
4+
import androidx.compose.foundation.layout.fillMaxSize
5+
import androidx.compose.foundation.layout.padding
6+
import androidx.compose.foundation.lazy.LazyColumn
7+
import androidx.compose.material.icons.Icons
8+
import androidx.compose.material.icons.automirrored.filled.ArrowBack
9+
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
10+
import androidx.compose.material3.ExperimentalMaterial3Api
11+
import androidx.compose.material3.Icon
12+
import androidx.compose.material3.IconButton
13+
import androidx.compose.material3.ListItem
14+
import androidx.compose.material3.Scaffold
15+
import androidx.compose.material3.Text
16+
import androidx.compose.material3.TopAppBar
17+
import androidx.compose.runtime.Composable
18+
import androidx.compose.ui.Modifier
19+
import org.jetbrains.compose.ui.tooling.preview.Preview
20+
21+
@OptIn(ExperimentalMaterial3Api::class)
22+
@Composable
23+
@Preview
24+
fun WpComSiteScreen(
25+
onMeClicked: () -> Unit,
26+
onSupportConversationsClicked: () -> Unit,
27+
onBotConversationsClicked: () -> Unit,
28+
onBackClicked: () -> Unit = {}
29+
) {
30+
Scaffold(
31+
topBar = {
32+
TopAppBar(
33+
title = { Text("WordPress.com") },
34+
navigationIcon = {
35+
IconButton(onClick = onBackClicked) {
36+
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
37+
}
38+
}
39+
)
40+
}
41+
) { paddingValues ->
42+
LazyColumn(
43+
modifier = Modifier.fillMaxSize().padding(paddingValues)
44+
) {
45+
item {
46+
ListItem(
47+
headlineContent = { Text("Me") },
48+
trailingContent = {
49+
Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null)
50+
},
51+
modifier = Modifier.clickable(onClick = onMeClicked)
52+
)
53+
}
54+
item {
55+
ListItem(
56+
headlineContent = { Text("Support Conversations") },
57+
trailingContent = {
58+
Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null)
59+
},
60+
modifier = Modifier.clickable(onClick = onSupportConversationsClicked)
61+
)
62+
}
63+
item {
64+
ListItem(
65+
headlineContent = { Text("Bot Conversations") },
66+
trailingContent = {
67+
Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null)
68+
},
69+
modifier = Modifier.clickable(onClick = onBotConversationsClicked)
70+
)
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)