Skip to content

feat: Implementation Story List in Gallery, Add DSL methods for building Story Lists #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions modules/gallery/src/androidMain/kotlin/StoryBook.android.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable

actual fun getStoryBook(): List<Story> = buildStoryBook {
addStory(
object : Story() {
override val storyName: String = "Android Button"
@Composable
override fun content() {
Button(onClick = {}) {
Text("Android Button")
}
}
}
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jetbrains.storytale

import App
import Story
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand All @@ -13,9 +12,6 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)
setContent {
App()
val story = Story("Android Story") {
App()
}
}
}
}
Expand Down
36 changes: 9 additions & 27 deletions modules/gallery/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.Button
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
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 org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.ui.tooling.preview.Preview
import storytale.modules.gallery.generated.resources.Res
import storytale.modules.gallery.generated.resources.compose_multiplatform

@OptIn(ExperimentalResourceApi::class)
@Composable
@Preview
fun App() {
MaterialTheme {
var showContent by remember { mutableStateOf(false) }
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
Button(onClick = { showContent = !showContent }) {
Text("Click me!")
}
AnimatedVisibility(showContent) {
val greeting = remember { Greeting().greet() }
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
Image(painterResource(Res.drawable.compose_multiplatform), null)
Text("Compose: $greeting")
}
}
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
LazyStoryList(
storyList = getStoryBook()
)
}
}
}
7 changes: 0 additions & 7 deletions modules/gallery/src/commonMain/kotlin/Greeting.kt

This file was deleted.

45 changes: 45 additions & 0 deletions modules/gallery/src/commonMain/kotlin/LazyStoryList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import androidx.compose.foundation.gestures.FlingBehavior
import androidx.compose.foundation.gestures.ScrollableDefaults
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun LazyStoryList(
storyList: List<Story>,
modifier: Modifier = Modifier,
state: LazyListState = rememberLazyListState(),
contentPadding: PaddingValues = PaddingValues(0.dp),
reverseLayout: Boolean = false,
verticalArrangement: Arrangement.Vertical =
if (!reverseLayout) Arrangement.Top else Arrangement.Bottom,
horizontalAlignment: Alignment.Horizontal = Alignment.Start,
flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior(),
userScrollEnabled: Boolean = true
) {
LazyColumn(
modifier = modifier,
state = state,
contentPadding = contentPadding,
flingBehavior = flingBehavior,
horizontalAlignment = horizontalAlignment,
verticalArrangement = verticalArrangement,
reverseLayout = reverseLayout,
userScrollEnabled = userScrollEnabled
) {
items(
items = storyList,
key = { it.storyName },
contentType = { it }
) {
it.content()
}
}
}
5 changes: 0 additions & 5 deletions modules/gallery/src/commonMain/kotlin/Platform.kt

This file was deleted.

1 change: 1 addition & 0 deletions modules/gallery/src/commonMain/kotlin/StoryBook.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
expect fun getStoryBook(): List<Story>
5 changes: 0 additions & 5 deletions modules/gallery/src/desktopMain/kotlin/Platform.jvm.kt

This file was deleted.

17 changes: 17 additions & 0 deletions modules/gallery/src/desktopMain/kotlin/StoryBook.desktop.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable

actual fun getStoryBook(): List<Story> = buildStoryBook {
addStory(
object : Story() {
override val storyName: String = "Desktop Button"
@Composable
override fun content() {
Button(onClick = {}) {
Text("Desktop Button")
}
}
}
)
}
7 changes: 0 additions & 7 deletions modules/gallery/src/iosMain/kotlin/Platform.ios.kt

This file was deleted.

17 changes: 17 additions & 0 deletions modules/gallery/src/iosMain/kotlin/StoryBook.ios.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable

actual fun getStoryBook(): List<Story> = buildStoryBook {
addStory(
object : Story() {
override val storyName: String = "iOS Button"
@Composable
override fun content() {
Button(onClick = {}) {
Text("iOS Button")
}
}
}
)
}
5 changes: 0 additions & 5 deletions modules/gallery/src/wasmJsMain/kotlin/Platform.wasmJs.kt

This file was deleted.

17 changes: 17 additions & 0 deletions modules/gallery/src/wasmJsMain/kotlin/StoryBook.wasmJs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable

actual fun getStoryBook(): List<Story> = buildStoryBook {
addStory(
object : Story() {
override val storyName: String = "Web Button"
@Composable
override fun content() {
Button(onClick = {}) {
Text("Web Button")
}
}
}
)
}
9 changes: 5 additions & 4 deletions modules/runtime/src/commonMain/kotlin/Story.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import androidx.compose.runtime.Composable

data class Story(
val name: String,
val content: @Composable () -> Unit
)
abstract class Story {
abstract val storyName: String
@Composable
abstract fun content()
}
8 changes: 8 additions & 0 deletions modules/runtime/src/commonMain/kotlin/StoryBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class StoryBuilder {
val list = mutableListOf<Story>()
fun addStory(story: Story) = list.add(story)
}

fun buildStoryBook(builder: StoryBuilder.() -> Unit): List<Story> {
return StoryBuilder().apply(builder).list
}