Create ideas page_
Add pagination and ordering to you Android application powered by Appwrite Databases.
1 min read
Using the IdeasService, we can build a screen to submit and view ideas. Overwrite the contents of ui/screens/IdeasScreen.kt with the following code.
Kotlin
package <YOUR_ROOT_PACKAGE_HERE>.screens
import androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.lazy.itemsimport androidx.compose.material3.Buttonimport androidx.compose.material3.ExperimentalMaterial3Apiimport androidx.compose.material3.Textimport androidx.compose.material3.TextFieldimport androidx.compose.runtime.Composableimport androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.rememberCoroutineScopeimport androidx.compose.runtime.setValueimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.text.font.FontWeightimport androidx.compose.ui.unit.dpimport io.appwrite.models.Rowimport io.appwrite.models.Userimport kotlinx.coroutines.launchimport <YOUR_ROOT_PACKAGE_HERE>services.IdeaService
@OptIn(ExperimentalMaterial3Api::class)@Composablefun IdeasScreen( user: User<Map<String, Any>>?, ideasService: IdeaService) { var ideas by remember { mutableStateOf<List<Row<Map<String, Any>>>>(listOf()) } val coroutineScope = rememberCoroutineScope()
LaunchedEffect(ideasService) { coroutineScope.launch { ideas = ideasService.fetch() } }
fun onSubmit(title: String, description: String) { if (user === null) return coroutineScope.launch { ideas = listOf(ideasService.add(user.id, title, description)).plus(ideas) } }
fun onRemove(ideaId: String) { coroutineScope.launch { ideas = ideas.filter { idea -> idea.id !== ideaId } ideasService.remove(ideaId) } }
var title by remember { mutableStateOf("") } var description by remember { mutableStateOf("") }
Column( modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally ) { if (user != null) { TextField( value = title, onValueChange = { title = it }, label = { Text("Title") }, modifier = Modifier .fillMaxWidth() .padding(16.dp) ) TextField( value = description, onValueChange = { description = it }, label = { Text("Description") }, modifier = Modifier .fillMaxWidth() .padding(16.dp) ) Button(onClick = { onSubmit(title, description) title = "" description = "" }) { Text("Submit") } }
LazyColumn(modifier = Modifier.fillMaxSize()) { items(ideas) { idea -> Column(modifier = Modifier.padding(16.dp)) { Text(text = idea.data["title"]?.toString() ?: "", fontWeight = FontWeight(700)) Text(text = idea.data["description"]?.toString() ?: "") if (user?.id == idea.data["userId"]) Button(onClick = { onRemove(idea.id) }) { Text("Remove") } } } } }}Update navigation
Update MainActivity.kt to add the IdeasScreen to the navigation bar.
Look for // Add this line π to find where the changes made here.
Kotlin
package <YOUR_ROOT_PACKAGE_HERE>
import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.compose.runtime.*import androidx.compose.foundation.layout.*import androidx.compose.material.icons.Icons// Add this line πimport androidx.compose.material.icons.filled.Listimport androidx.compose.material.icons.filled.Personimport androidx.compose.material3.*import androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport io.appwrite.models.Userimport <YOUR_ROOT_PACKAGE_HERE>.services.Appwriteimport <YOUR_ROOT_PACKAGE_HERE>.services.AccountServiceimport <YOUR_ROOT_PACKAGE_HERE>.ui.screens.UserScreen
// Add this line πimport <YOUR_ROOT_PACKAGE_HERE>.services.IdeaService
// Add this line πimport <YOUR_ROOT_PACKAGE_HERE>.ui.screens.IdeasScreen
enum class Screen { User, // Add this line π Ideas}
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
Appwrite.init(applicationContext)
setContent { // Update this line π AppContent(Appwrite.account, Appwrite.ideas) } }}
// Add navigation with this composable π@Composableprivate fun AppBottomBar(screen: MutableState<Screen>) { BottomAppBar { Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center ) { IconButton(onClick = { screen.value = Screen.Ideas }) { Column(horizontalAlignment = Alignment.CenterHorizontally) { Icon(Icons.Default.List, contentDescription = "Ideas") Text("Ideas") } } IconButton(onClick = { screen.value = Screen.User }) { Column(horizontalAlignment = Alignment.CenterHorizontally) { Icon(Icons.Default.Person, contentDescription = "User") Text("User") } } } }}
@OptIn(ExperimentalMaterial3Api::class)@Composable// Update this line πprivate fun AppContent(accountService: AccountService, ideasService: IdeaService) { val user = remember { mutableStateOf<User<Map<String, Any>>?>(null) } // Update this line π val screen = remember { mutableStateOf(Screen.Ideas) }
LaunchedEffect(screen) { user.value = accountService.getLoggedIn() }
Scaffold(bottomBar = { AppBottomBar(screen) }) { padding -> Column(modifier = Modifier.padding(padding)) { when (screen.value) { Screen.User -> UserScreen(user, accountService) // Update this line π else -> IdeasScreen(user.value, ideasService) } } }}Was this page helpful?
Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.