Composables
Docs Login
Vela

Projects

package com.composables.vela

import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.vela.components.Button
import com.composables.vela.components.EmptyState
import com.composables.vela.components.Icon
import com.composables.vela.components.IconButton
import com.composables.vela.components.IconButtonSize
import com.composables.vela.components.ModalBottomSheet
import com.composables.vela.components.OptionRow
import com.composables.vela.components.Page
import com.composables.vela.components.SearchField
import com.composables.vela.components.Section
import com.composables.vela.components.Text
import com.composables.vela.components.TextField
import com.composables.vela.theme.CaretRight
import com.composables.vela.theme.ChatCircle
import com.composables.vela.theme.File
import com.composables.vela.theme.Folder
import com.composables.vela.theme.FolderSimplePlus
import com.composables.vela.theme.Icons
import com.composables.vela.theme.Note
import com.composables.vela.theme.Plus
import com.composables.vela.theme.Space
import com.composables.vela.theme.Theme
import com.composables.vela.theme.X
import com.composeunstyled.UnstyledButton

@Composable
fun ProjectsScreen(
    navigateBack: () -> Unit,
    navigateToProject: (name: String) -> Unit,
    navigateToCreateProject: () -> Unit,
    modifier: Modifier = Modifier,
) {
    var query by remember { mutableStateOf("") }
    val matches = sampleProjects.filter {
        it.name.contains(query.trim(), ignoreCase = true) ||
            it.goal.contains(query.trim(), ignoreCase = true)
    }
    Page(
        title = "Projects",
        onBack = navigateBack,
        modifier = modifier,
        actions = {
            IconButton(onClick = navigateToCreateProject, contentDescription = "New project") {
                Icon(
                    Icons.FolderSimplePlus,
                    contentDescription = null,
                    modifier = Modifier.size(22.dp),
                )
            }
        },
    ) {
        if (sampleProjects.isEmpty()) {
            EmptyState(
                Icons.Folder,
                title = "No projects yet",
                message = "Group chats, files and instructions around one piece of work.",
                action = { Button(onClick = navigateToCreateProject) { Text("New project") } },
            )
        } else {
            SearchField(query, { query = it }, placeholder = "Search projects")
            Column(verticalArrangement = Arrangement.spacedBy(Space * 2)) {
                matches.forEach { project ->
                    ProjectCard(project, onClick = { navigateToProject(project.name) })
                }
                if (matches.isEmpty()) {
                    Text(
                        "No projects match “${query.trim()}”",
                        color = Theme.colors.onBackgroundMuted,
                        modifier = Modifier.padding(Space * 4),
                    )
                }
            }
        }
    }
}

@Composable
private fun ProjectCard(project: SampleProject, onClick: () -> Unit) {
    UnstyledButton(
        onClick = onClick,
        modifier =
            Modifier.fillMaxWidth().clip(Theme.shapes.medium).background(Theme.colors.surface),
        contentPadding = PaddingValues(Space * 4),
        indication = rememberRippleIndication(color = Theme.colors.onSurface),
        interactionSource = remember { MutableInteractionSource() },
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(Space * 3),
        ) {
            Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(Space)) {
                Text(project.name, style = Theme.typography.title)
                Text(
                    project.goal,
                    color = Theme.colors.onBackgroundMuted,
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis,
                )
                Text(
                    listOf(
                            plural(project.chats.size, "chat"),
                            plural(project.files.size, "file"),
                            "Updated ${project.updated.lowercase()}",
                        )
                        .joinToString(" · "),
                    style = Theme.typography.caption,
                    color = Theme.colors.onBackgroundMuted,
                )
            }
            Icon(
                Icons.CaretRight,
                contentDescription = null,
                modifier = Modifier.size(16.dp),
                tint = Theme.colors.onBackgroundMuted,
            )
        }
    }
}

private fun plural(count: Int, noun: String) = "$count $noun${if (count == 1) "" else "s"}"

@Composable
fun CreateProjectScreen(
    navigateBack: () -> Unit,
    navigateToProject: (name: String, goal: String) -> Unit,
    modifier: Modifier = Modifier,
) {
    var name by remember { mutableStateOf("") }
    var goal by remember { mutableStateOf("") }

    fun createProject() {
        // create the project here
        navigateToProject(name.trim(), goal.trim())
    }

    Page(
        title = "New project",
        onBack = navigateBack,
        backIcon = Icons.X,
        backLabel = "Close",
        modifier = modifier,
        bottomBar = {
            Button(
                onClick = ::createProject,
                enabled = name.isNotBlank(),
                modifier = Modifier.fillMaxWidth(),
            ) {
                Text("Create project")
            }
        },
    ) {
        LabelledField("What are you working on?") {
            TextField(name, { name = it }, label = "Project name")
        }
        LabelledField(
            "What are you trying to achieve?",
            note = "Vela keeps this in mind in every chat in the project.",
        ) {
            TextField(goal, { goal = it }, label = "Goal", minLines = 3)
        }
    }
}

@Composable
fun ProjectDetailScreen(
    navigateBack: () -> Unit,
    navigateToChat: () -> Unit,
    modifier: Modifier = Modifier,
    name: String = sampleProjects.first().name,
    goal: String = "",
) {
    fun startChat() {
        // start a chat in this project here
        navigateToChat()
    }

    fun openChat(title: String) {
        // open the project's chat here
        navigateToChat()
    }

    fun addFiles() {
        // pick files to add to the project here
    }

    val project =
        remember(name) {
            sampleProjects.firstOrNull { it.name == name }
                ?: SampleProject(name, goal, "", emptyList(), emptyList(), "Today")
        }
    var instructions by remember(project) { mutableStateOf(project.instructions) }
    var draft by remember { mutableStateOf("") }
    var editing by remember { mutableStateOf(false) }
    val files = remember(project) { mutableStateListOf(*project.files.toTypedArray()) }
    Page(
        title = project.name,
        onBack = navigateBack,
        modifier = modifier,
        bottomBar = {
            Button(onClick = ::startChat, modifier = Modifier.fillMaxWidth()) {
                Text("New chat in project")
            }
        },
        overlay = {
            ModalBottomSheet(
                visible = editing,
                onDismiss = { editing = false },
                title = "Instructions",
            ) {
                Text(
                    "How Vela should work in this project, such as tone, format or sources to prefer.",
                    color = Theme.colors.onBackgroundMuted,
                )
                TextField(draft, { draft = it }, label = "Instructions", minLines = 4)
                Button(
                    onClick = {
                        instructions = draft.trim()
                        editing = false
                    },
                    modifier = Modifier.fillMaxWidth(),
                ) {
                    Text("Save instructions")
                }
            }
        },
    ) {
        if (project.goal.isNotBlank()) Text(project.goal, color = Theme.colors.onBackgroundMuted)
        Section("Instructions") {
            OptionRow(
                if (instructions.isBlank()) "Add instructions" else instructions,
                onClick = {
                    draft = instructions
                    editing = true
                },
                icon = Icons.Note,
                navigates = true,
            )
        }
        Section(
            "Files",
            footer =
                if (files.isEmpty()) "Files you add are available in every chat in this project."
                else null,
        ) {
            files.forEach { file ->
                Row(
                    Modifier.fillMaxWidth()
                        .padding(start = Space * 4, end = Space * 2, top = Space, bottom = Space),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.spacedBy(Space * 3),
                ) {
                    Icon(Icons.File, contentDescription = null, modifier = Modifier.size(20.dp))
                    Column(Modifier.weight(1f)) {
                        Text(
                            file.name,
                            style = Theme.typography.label,
                            maxLines = 1,
                            overflow = TextOverflow.Ellipsis,
                        )
                        Text(
                            file.size,
                            style = Theme.typography.caption,
                            color = Theme.colors.onBackgroundMuted,
                        )
                    }
                    IconButton(
                        onClick = { files.remove(file) },
                        contentDescription = "Remove ${file.name}",
                        size = IconButtonSize.Small,
                    ) {
                        Icon(
                            Icons.X,
                            contentDescription = null,
                            modifier = Modifier.size(16.dp),
                            tint = Theme.colors.onBackgroundMuted,
                        )
                    }
                }
            }
            OptionRow("Add files", onClick = ::addFiles, icon = Icons.Plus)
        }
        if (project.chats.isEmpty()) {
            EmptyState(
                Icons.ChatCircle,
                title = "No chats yet",
                message = "Chats you start here use this project's files and instructions.",
            )
        } else {
            Section("Chats") {
                project.chats.forEach { chat ->
                    OptionRow(
                        chat,
                        onClick = { openChat(chat) },
                        icon = Icons.ChatCircle,
                        navigates = true,
                    )
                }
            }
        }
    }
}

More screens from Vela

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.