Skip to content

Commit

Permalink
Subscriptions and such, rich text view
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Aug 8, 2024
1 parent 936a4ab commit dba8c1a
Show file tree
Hide file tree
Showing 21 changed files with 482 additions and 33 deletions.
2 changes: 1 addition & 1 deletion example-app-ios/KiteUI Example App/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
AppIos.root(self)
App_iosKt.root(viewController: self)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.lightningkite.kiteui.views.ViewWriter
import com.lightningkite.kiteui.ViewWrapper
import com.lightningkite.kiteui.models.*
import com.lightningkite.kiteui.navigation.Screen
import com.lightningkite.kiteui.viewDebugTarget
import com.lightningkite.kiteui.views.*
import com.lightningkite.kiteui.views.direct.*

Expand All @@ -20,6 +21,7 @@ fun ViewWriter.example(
action: ViewWriter.()->Unit
) {
row {
beforeNextElementSetup { viewDebugTarget = this }
expanding - scrollsHorizontally - code - text(codeText)
separator()
expanding - action()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ object DocSearchScreen : Screen {
val query = Property<String>("")

val docsPages = Property(listOf(
{ ThemeScreen },
{ TextElementScreen },
{ DataScreen },
{ NavigationScreen },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ object TextElementScreen : DocScreen {
}
}
}
text("It's gross, but you can assign some level of HTML too.")
example(
"""
text {
setBasicHtmlContent(""${'"'}
<h1>Hello!</h1>
<p>Welcome to my website from 2000.</p>
<ol>
<li> Item One</li>
<li> Item Two</li>
<li> Item Three</li>
</ol>
<a href="https://google.com"> Open Google </a>
""${'"'}.trimIndent())
}
""".trimIndent()
) {
text {
setBasicHtmlContent("""
<h1>Hello!</h1>
<p>Welcome to my website from 2000.</p>
<ol>
<li> Item One</li>
<li> Item Two</li>
<li> Item Three</li>
</ol>
<a href="https://google.com"> Open Google </a>
""".trimIndent())
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lightningkite.mppexampleapp.docs

import com.lightningkite.kiteui.views.ViewWriter
import com.lightningkite.kiteui.Routable
import com.lightningkite.kiteui.viewDebugTarget
import com.lightningkite.kiteui.views.*
import com.lightningkite.kiteui.views.direct.*

Expand All @@ -21,7 +22,7 @@ object ThemeScreen: DocScreen {
text("This is the default color")
text("This is a different color") in tweakTheme { it.copy(foreground = Color(0.5f, 0f, 0f, 1f)) }
}
""".trimIndent()) {
""".trimIndent().also { println("'$it'") }) {
card - col {
text("This is the default color")
text("This is a different color")}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lightningkite.mppexampleapp

import com.lightningkite.kiteui.navigation.ScreenNavigator
import com.lightningkite.kiteui.views.setup
import platform.UIKit.UIViewController

fun root(viewController: UIViewController) {
viewController.setup(appTheme) { app(ScreenNavigator { AutoRoutes }, ScreenNavigator { AutoRoutes }) }
}
2 changes: 1 addition & 1 deletion library/library.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Pod::Spec.new do |spec|
fi
set -ev
REPO_ROOT="$PODS_TARGET_SRCROOT"
"$REPO_ROOT/../gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework \
"$REPO_ROOT/../../ilusso-bsa/gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework \
-Pkotlin.native.cocoapods.platform=$PLATFORM_NAME \
-Pkotlin.native.cocoapods.archs="$ARCHS" \
-Pkotlin.native.cocoapods.configuration="$CONFIGURATION"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import android.graphics.Typeface
import android.graphics.text.LineBreakConfig
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.text.Html
import android.text.Layout
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup
Expand Down Expand Up @@ -78,6 +80,10 @@ actual class TextView actual constructor(context: RContext) :
native.isAllCaps = theme.font.allCaps
native.setTextSize(TypedValue.COMPLEX_UNIT_PX, theme.font.size.value)
}
actual fun setBasicHtmlContent(html: String) {
native.movementMethod = LinkMovementMethod.getInstance()
native.text = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY)
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ actual val ViewWriter.scrolls: ViewWrapper
override fun defaultLayoutParams(): ViewGroup.LayoutParams =
FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)

override fun applyForeground(theme: Theme) { /*Do nothing*/
}

override fun applyBackground(theme: Theme, fullyApply: Boolean) { /*Do nothing*/
}
// override fun applyForeground(theme: Theme) { /*Do nothing*/
// }
//
// override fun applyBackground(theme: Theme, fullyApply: Boolean) { /*Do nothing*/
// }
})
return ViewWrapper
}
Expand All @@ -146,11 +146,11 @@ actual val ViewWriter.scrollsHorizontally: ViewWrapper
override fun defaultLayoutParams(): ViewGroup.LayoutParams =
FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)

override fun applyForeground(theme: Theme) { /*Do nothing*/
}

override fun applyBackground(theme: Theme, fullyApply: Boolean) { /*Do nothing*/
}
// override fun applyForeground(theme: Theme) { /*Do nothing*/
// }
//
// override fun applyBackground(theme: Theme, fullyApply: Boolean) { /*Do nothing*/
// }
})
return ViewWrapper
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package com.lightningkite.kiteui.dom

sealed interface MPNode {
fun secure()

companion object {
val okTags = setOf(
"p",
"ul",
"li",
"div",
"span",
"strong",
"b",
"em",
"cite",
"dfn",
"i",
"big",
"small",
"font",
"blockquote",
"tt",
"a",
"u",
"del",
"s",
"strike",
"sup",
"sub",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"br",
)
val okAttrs = setOf(
"href"
)
}

data class Element(
var tagName: String,
val attributes: MutableMap<String, String> = HashMap(),
val children: MutableList<MPNode> = ArrayList()
) : MPNode {
override fun toString(): String {
if(tagName == "br") return "<br>"
return "<${tagName} ${attributes.entries.joinToString(" ") { "${it.key}=\"${it.value}\"" }}>${
children.joinToString("")
}</${tagName}>"
}

override fun secure() {
if (tagName !in okTags) tagName = "span"
attributes.keys.retainAll(okAttrs)
children.forEach { it.secure() }
}
}

data class Text(val content: String) : MPNode {
override fun toString(): String = content
override fun secure() {}
}
}

fun String.parseMPNodes(): List<MPNode> {
val stack = arrayListOf(MPNode.Element("*"))
starts(
onTag = {
it.analyzeTagInside { tagName, start, end, kvs ->
if (start) {
val newElement = MPNode.Element(tagName, attributes = kvs)
stack.last().children.add(newElement)
stack.add(newElement)
}

if (end || tagName == "br") {
repeat(
stack.size - stack.indexOfLast { it.tagName == tagName }
) {
stack.removeLast()
}
}
}
},
onContent = {
if (it.isNotBlank()) stack.last().children.add(MPNode.Text(it))
}
)
return stack.first().children
}

internal inline fun String.starts(
onTag: (String) -> Unit,
onContent: (String) -> Unit
) {
var current = 0
while (true) {
val nextStart = this.indexOf('<', current)
if (nextStart == -1) {
if(current < length) onContent(substring(current))
break
}
if (current < nextStart) {
onContent(substring(current, nextStart))
}
val nextEnd = this.indexOf('>', nextStart + 1) + 1
onTag(substring(nextStart + 1, nextEnd - 1))
current = nextEnd
}
}

internal inline fun String.analyzeTagInside(out: (tagName: String, start: Boolean, end: Boolean, kvs: MutableMap<String, String>) -> Unit) {

var isStart = true
var hasEnd = false
var lastBuilt = ""
var tagName: String? = null
val kvs = HashMap<String, String>()

var textEncountered = false
var inQuotes = false
var quoteChar = '"'
val builder = StringBuilder()
var nextIsValue = false
var currentIsValue = false
var runFinish = false

for (char in this) {
if (inQuotes) {
if (char == quoteChar) {
inQuotes = false
runFinish = true
} else {
builder.append(char)
}
} else when (char) {
'/' -> {
runFinish = true
hasEnd = true
if (!textEncountered) {
isStart = false
}
}

' ' -> {
runFinish = true
}

'=' -> {
nextIsValue = true
runFinish = true
}

'\'', '"' -> {
runFinish = true
inQuotes = true
quoteChar = char
}

else -> {
textEncountered = true
builder.append(char)
}
}
if (runFinish) {
runFinish = false
if (builder.length != 0) {
val built = builder.toString()
if (tagName == null) {
tagName = built
} else if (currentIsValue) {
kvs[lastBuilt] = built
} else {
kvs[built] = ""
}
if (nextIsValue) {
nextIsValue = false
currentIsValue = true
} else {
currentIsValue = false
}
lastBuilt = built
builder.clear()
}
}
}
if (builder.length != 0) {
val built = builder.toString()
if (tagName == null) {
tagName = built
} else if (currentIsValue) {
kvs[lastBuilt] = built
} else {
kvs[built] = ""
}
builder.clear()
}

out(tagName.toString(), isStart, hasEnd, kvs)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ class KiteUiCss(val dynamicCss: DynamicCss) {
font: inherit;
vertical-align: baseline;
}
p :is(html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video) {
margin: revert;
padding: revert;
border: revert;
font-size: revert;
vertical-align: revert;
}
p li {
margin-inline-start: 1em;
}
p a {
text-decoration: revert;
color: revert;
}
p a:visited {
color: revert;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
Expand Down
Loading

0 comments on commit dba8c1a

Please sign in to comment.