Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
[Android] Add support for subcomposition and sharing state
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnyandrew committed Sep 25, 2023
1 parent 7fa6e63 commit 3444608
Show file tree
Hide file tree
Showing 21 changed files with 654 additions and 360 deletions.
4 changes: 2 additions & 2 deletions platforms/android/example-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

android {
namespace 'io.element.wysiwyg.compose'
compileSdk 33
compileSdk 34

defaultConfig {
applicationId "io.element.wysiwyg.compose"
Expand Down Expand Up @@ -34,7 +34,7 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.5.1'
kotlinCompilerExtensionVersion '1.5.3'
}
packagingOptions {
resources {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -28,6 +29,7 @@ import io.element.android.wysiwyg.view.models.LinkAction
import io.element.wysiwyg.compose.ui.components.FormattingButtons
import io.element.wysiwyg.compose.ui.theme.RichTextEditorTheme
import kotlinx.collections.immutable.toPersistentMap
import kotlinx.coroutines.launch
import timber.log.Timber
import uniffi.wysiwyg_composer.ComposerAction

Expand All @@ -39,13 +41,22 @@ class MainActivity : ComponentActivity() {
val state = rememberRichTextEditorState()

var linkDialogAction by remember { mutableStateOf<LinkAction?>(null) }
val coroutineScope = rememberCoroutineScope()


linkDialogAction?.let { linkAction ->
LinkDialog(
linkAction = linkAction,
onRemoveLink = state::removeLink,
onSetLink = state::setLink,
onInsertLink = state::insertLink,
onRemoveLink = { coroutineScope.launch { state.removeLink() } },
onSetLink = { coroutineScope.launch { state.setLink(it) } },
onInsertLink = { url, text ->
coroutineScope.launch {
state.insertLink(
url,
text
)
}
},
onDismissRequest = { linkDialogAction = null }
)
}
Expand All @@ -61,10 +72,12 @@ class MainActivity : ComponentActivity() {
modifier = Modifier
.padding(8.dp)
.border(
border = BorderStroke(1.dp, MaterialTheme.colorScheme.outlineVariant),
border = BorderStroke(
1.dp,
MaterialTheme.colorScheme.outlineVariant
),
)
.padding(8.dp)
,
.padding(8.dp),
color = MaterialTheme.colorScheme.surface,
) {
RichTextEditor(
Expand All @@ -76,36 +89,49 @@ class MainActivity : ComponentActivity() {
}
FormattingButtons(
onResetText = {
state.setHtml("")
coroutineScope.launch {
state.setHtml("")
}
},
actionStates = state.actions.toPersistentMap(),
onActionClick = {
when (it) {
ComposerAction.BOLD -> state.toggleInlineFormat(InlineFormat.Bold)
ComposerAction.ITALIC -> state.toggleInlineFormat(InlineFormat.Italic)
ComposerAction.STRIKE_THROUGH -> state.toggleInlineFormat(
InlineFormat.StrikeThrough
)
coroutineScope.launch {
when (it) {
ComposerAction.BOLD -> state.toggleInlineFormat(
InlineFormat.Bold
)

ComposerAction.ITALIC -> state.toggleInlineFormat(
InlineFormat.Italic
)

ComposerAction.STRIKE_THROUGH -> state.toggleInlineFormat(
InlineFormat.StrikeThrough
)

ComposerAction.UNDERLINE -> state.toggleInlineFormat(
InlineFormat.Underline
)

ComposerAction.UNDERLINE -> state.toggleInlineFormat(
InlineFormat.Underline
)
ComposerAction.INLINE_CODE -> state.toggleInlineFormat(
InlineFormat.InlineCode
)

ComposerAction.INLINE_CODE -> state.toggleInlineFormat(
InlineFormat.InlineCode
)
ComposerAction.LINK ->
linkDialogAction = state.linkAction

ComposerAction.LINK ->
linkDialogAction = state.linkAction
ComposerAction.UNDO -> state.undo()
ComposerAction.REDO -> state.redo()
ComposerAction.ORDERED_LIST -> state.toggleList(ordered = true)
ComposerAction.UNORDERED_LIST -> state.toggleList(
ordered = false
)

ComposerAction.UNDO -> state.undo()
ComposerAction.REDO -> state.redo()
ComposerAction.ORDERED_LIST -> state.toggleList(ordered = true)
ComposerAction.UNORDERED_LIST -> state.toggleList(ordered = false)
ComposerAction.INDENT -> state.indent()
ComposerAction.UNINDENT -> state.unindent()
ComposerAction.CODE_BLOCK -> state.toggleCodeBlock()
ComposerAction.QUOTE -> state.toggleQuote()
ComposerAction.INDENT -> state.indent()
ComposerAction.UNINDENT -> state.unindent()
ComposerAction.CODE_BLOCK -> state.toggleCodeBlock()
ComposerAction.QUOTE -> state.toggleQuote()
}
}
}
)
Expand Down
2 changes: 1 addition & 1 deletion platforms/android/example-view/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
android {
namespace = "io.element.android.wysiwyg.poc"

compileSdk 33
compileSdk 34

defaultConfig {
applicationId "io.element.android.wysiwyg.poc"
Expand Down
8 changes: 6 additions & 2 deletions platforms/android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ activity-compose = "1.7.2"
androidx-junit = "1.1.5"
core-ktx = "1.10.1"
coroutines = "1.7.2"
compose-bom = "2023.06.01"
compose-bom = "2023.09.00"
lifecycle-runtime-ktx = "2.6.1"
timber = "5.0.1"
tagsoup = "1.2"
Expand All @@ -22,9 +22,10 @@ hamcrest = "2.2"
android-junit = "1.1.3"
espresso = "3.5.1"
agp = "8.1.0"
kotlin-android = "1.9.0"
kotlin-android = "1.9.10"
jacoco = "0.8.8"
maven-publish = "0.25.3"
molecule = "1.2.1"

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down Expand Up @@ -56,12 +57,15 @@ google-material = { module="com.google.android.material:material", version.ref="
# Misc
timber = { module="com.jakewharton.timber:timber", version.ref="timber" }
tagsoup = { module="org.ccil.cowan.tagsoup:tagsoup", version.ref="tagsoup" }
molecule-runtime = { module = "app.cash.molecule:molecule-runtime", version.ref = "molecule" }

# Test
test-junit = { module="junit:junit", version.ref="junit" }
test-robolectric = { module="org.robolectric:robolectric", version.ref="robolectric" }
test-mockk = { module="io.mockk:mockk", version.ref="mockk" }
test-hamcrest = { module="org.hamcrest:hamcrest", version.ref="hamcrest" }
test-kotlin-coroutines = { module="org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref="coroutines" }
test-turbine = { module="app.cash.turbine:turbine", version="1.0.0" }

# Android Test
test-androidx-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-junit" }
Expand Down
10 changes: 8 additions & 2 deletions platforms/android/library-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ android {
namespace 'io.element.android.wysiwyg.compose'
testNamespace 'io.element.android.wysiwyg.compose.test'

compileSdk 33
compileSdk 34

defaultConfig {
minSdk 23
Expand All @@ -53,11 +53,14 @@ android {
testCoverage {
jacocoVersion = "0.8.8"
}
testOptions {
unitTests.returnDefaultValues = true
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.5.1'
kotlinCompilerExtensionVersion '1.5.3'
}
packagingOptions {
resources.excludes += 'META-INF/LICENSE.md'
Expand Down Expand Up @@ -87,6 +90,9 @@ dependencies {

testImplementation libs.test.junit
testImplementation libs.test.mockk
testImplementation libs.test.kotlin.coroutines
testImplementation libs.test.turbine
testImplementation libs.molecule.runtime
androidTestImplementation libs.test.androidx.junit
androidTestImplementation libs.test.androidx.espresso
androidTestImplementation libs.test.mockk.android
Expand Down
Loading

0 comments on commit 3444608

Please sign in to comment.