Skip to content

Commit

Permalink
Merge pull request #698 from CruGlobal/stylesParent
Browse files Browse the repository at this point in the history
GT-2450 Refactor stylesParent
  • Loading branch information
frett authored Oct 16, 2024
2 parents 4d82327 + 3c99beb commit 63c4abc
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package org.cru.godtools.shared.tool.parser.model

import kotlin.experimental.ExperimentalObjCRefinement
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport
import kotlin.native.HiddenFromObjC
import org.ccci.gto.support.androidx.annotation.RestrictTo
import org.ccci.gto.support.androidx.annotation.RestrictToScope

@OptIn(ExperimentalJsExport::class, ExperimentalObjCRefinement::class)
interface Base {
val manifest: Manifest
val stylesParent: Styles?
@HiddenFromObjC
@JsExport.Ignore
@get:RestrictTo(RestrictToScope.LIBRARY)
val parent: Base? get() = null
val manifest: Manifest get() = checkNotNull(parent?.manifest) { "No manifest found in model ancestors" }
}

val Base?.manifest get() = this?.manifest
val Base?.stylesParent get() = this?.stylesParent
val Base?.stylesParent: Styles? get() = this?.parent?.let { it as? Styles ?: it.stylesParent }

internal fun Base.getResource(name: String?) = manifest.getResource(name)
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package org.cru.godtools.shared.tool.parser.model

abstract class BaseModel internal constructor(private val parent: Base? = null) : Base {
override val stylesParent get() = parent as? Styles ?: parent?.stylesParent
override val manifest get() = checkNotNull(parent?.manifest) { "No manifest found in model ancestors" }
}
abstract class BaseModel internal constructor(override val parent: Base? = null) : Base
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal val Styles?.cardBackgroundColor get() = this?.cardBackgroundColor ?: Ma

// region Multiselect styles
internal val Styles?.multiselectOptionBackgroundColor
get() = this?.multiselectOptionBackgroundColor ?: manifest.backgroundColor
get() = this?.multiselectOptionBackgroundColor ?: this?.manifest.backgroundColor
// endregion Multiselect styles

// region Text styles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import org.cru.godtools.shared.tool.parser.model.page.Page.Companion.DEFAULT_BAC
import org.cru.godtools.shared.tool.parser.model.page.Page.Companion.DEFAULT_BACKGROUND_IMAGE_SCALE_TYPE
import org.cru.godtools.shared.tool.parser.model.primaryColor
import org.cru.godtools.shared.tool.parser.model.primaryTextColor
import org.cru.godtools.shared.tool.parser.model.stylesParent
import org.cru.godtools.shared.tool.parser.model.textColor
import org.cru.godtools.shared.tool.parser.model.textScale
import org.cru.godtools.shared.tool.parser.model.toColorOrNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.cru.godtools.shared.tool.parser.model.parseContent
import org.cru.godtools.shared.tool.parser.model.parseTextChild
import org.cru.godtools.shared.tool.parser.model.primaryColor
import org.cru.godtools.shared.tool.parser.model.stylesOverride
import org.cru.godtools.shared.tool.parser.model.stylesParent
import org.cru.godtools.shared.tool.parser.xml.XmlPullParser

private const val XML_HEADING = "heading"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import org.cru.godtools.shared.tool.parser.model.XML_TEXT_COLOR
import org.cru.godtools.shared.tool.parser.model.backgroundColor
import org.cru.godtools.shared.tool.parser.model.contentTips
import org.cru.godtools.shared.tool.parser.model.getResource
import org.cru.godtools.shared.tool.parser.model.manifest
import org.cru.godtools.shared.tool.parser.model.page.Page
import org.cru.godtools.shared.tool.parser.model.parseContent
import org.cru.godtools.shared.tool.parser.model.parseTextChild
Expand Down Expand Up @@ -317,6 +316,6 @@ class TractPage : Page {
}

@get:AndroidColorInt
val Card?.backgroundColor get() = this?.backgroundColor ?: manifest.backgroundColor
val Card?.backgroundColor get() = this?.backgroundColor ?: this?.manifest.backgroundColor
val Card?.backgroundImageGravity get() = this?.backgroundImageGravity ?: Card.DEFAULT_BACKGROUND_IMAGE_GRAVITY
val Card?.backgroundImageScaleType get() = this?.backgroundImageScaleType ?: Card.DEFAULT_BACKGROUND_IMAGE_SCALE_TYPE
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.cru.godtools.shared.tool.state.State

@RunOnAndroidWith(AndroidJUnit4::class)
class ButtonTest : UsesResources() {
private val parent = object : BaseModel(), Styles {
private val parentObj = object : BaseModel(), Styles {
override lateinit var buttonStyle: Button.Style
override var primaryColor = TestColors.BLACK
override var primaryTextColor = TestColors.BLACK
Expand Down Expand Up @@ -151,16 +151,16 @@ class ButtonTest : UsesResources() {
// region Property - style
@Test
fun testButtonStyleUtilizesStylesDefault() {
with(Button(parent)) {
parent.buttonStyle = Button.Style.CONTAINED
with(Button(parentObj)) {
parentObj.buttonStyle = Button.Style.CONTAINED
assertEquals(Button.Style.CONTAINED, style)
parent.buttonStyle = Button.Style.OUTLINED
parentObj.buttonStyle = Button.Style.OUTLINED
assertEquals(Button.Style.OUTLINED, style)
}

parent.buttonStyle = Button.Style.CONTAINED
with(Button(parent, style = Button.Style.OUTLINED)) {
assertNotEquals(parent.buttonStyle, style)
parentObj.buttonStyle = Button.Style.CONTAINED
with(Button(parentObj, style = Button.Style.OUTLINED)) {
assertNotEquals(parentObj.buttonStyle, style)
assertEquals(Button.Style.OUTLINED, style)
}
}
Expand Down Expand Up @@ -194,17 +194,17 @@ class ButtonTest : UsesResources() {
// region Property - text - textAlign
@Test
fun testButtonTextTextAlignFallbackBehavior() {
parent.textAlign = Text.Align.START
parentObj.textAlign = Text.Align.START

// Buttons default to center aligned text
with(Button(parent, text = { Text(it) })) {
assertNotEquals(parent.textAlign, text.textAlign)
with(Button(parentObj, text = { Text(it) })) {
assertNotEquals(parentObj.textAlign, text.textAlign)
assertEquals(Text.Align.CENTER, text.textAlign)
}

// Text Align can still be overridden on the text element
with(Button(parent, text = { Text(it, textAlign = Text.Align.END) })) {
assertNotEquals(parent.textAlign, text.textAlign)
with(Button(parentObj, text = { Text(it, textAlign = Text.Align.END) })) {
assertNotEquals(parentObj.textAlign, text.textAlign)
assertEquals(Text.Align.END, text.textAlign)
}
}
Expand All @@ -213,39 +213,39 @@ class ButtonTest : UsesResources() {
// region Property - text - textColor
@Test
fun testButtonTextColorFallbackBehaviorContained() {
parent.primaryColor = TestColors.RED
parent.primaryTextColor = TestColors.GREEN
parentObj.primaryColor = TestColors.RED
parentObj.primaryTextColor = TestColors.GREEN

with(Button(parent, style = Button.Style.CONTAINED, text = { Text(it) })) {
assertNotEquals(parent.primaryColor, text.textColor)
assertNotEquals(parent.textColor, text.textColor)
with(Button(parentObj, style = Button.Style.CONTAINED, text = { Text(it) })) {
assertNotEquals(parentObj.primaryColor, text.textColor)
assertNotEquals(parentObj.textColor, text.textColor)
assertNotEquals(buttonColor, text.textColor)
assertEquals(parent.primaryTextColor, text.textColor)
assertEquals(parentObj.primaryTextColor, text.textColor)
assertEquals(TestColors.GREEN, text.textColor)
}

with(Button(parent, style = Button.Style.CONTAINED, text = { Text(it, textColor = TestColors.BLUE) })) {
assertNotEquals(parent.primaryTextColor, text.textColor)
with(Button(parentObj, style = Button.Style.CONTAINED, text = { Text(it, textColor = TestColors.BLUE) })) {
assertNotEquals(parentObj.primaryTextColor, text.textColor)
assertEquals(TestColors.BLUE, text.textColor)
}
}

@Test
fun testButtonTextColorFallbackBehaviorOutlined() {
parent.primaryColor = TestColors.RED
parent.primaryTextColor = TestColors.RED
parentObj.primaryColor = TestColors.RED
parentObj.primaryTextColor = TestColors.RED

with(Button(parent, style = Button.Style.OUTLINED, color = TestColors.GREEN, text = { Text(it) })) {
assertNotEquals(parent.primaryColor, text.textColor)
assertNotEquals(parent.primaryTextColor, text.textColor)
assertNotEquals(parent.textColor, text.textColor)
with(Button(parentObj, style = Button.Style.OUTLINED, color = TestColors.GREEN, text = { Text(it) })) {
assertNotEquals(parentObj.primaryColor, text.textColor)
assertNotEquals(parentObj.primaryTextColor, text.textColor)
assertNotEquals(parentObj.textColor, text.textColor)
assertEquals(buttonColor, text.textColor)
assertEquals(TestColors.GREEN, text.textColor)
}

with(
Button(
parent,
parentObj,
style = Button.Style.OUTLINED,
color = TestColors.RED,
text = { Text(it, textColor = TestColors.GREEN) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import kotlin.test.assertNull
class StylesTest {
private val parent by lazy {
object : Styles {
override val stylesParent: Styles? = null
override val manifest get() = TODO()

override var primaryColor = TestColors.RED
override var primaryTextColor = TestColors.RED

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.cru.godtools.shared.tool.parser.model.Paragraph
import org.cru.godtools.shared.tool.parser.model.Tabs
import org.cru.godtools.shared.tool.parser.model.TestColors
import org.cru.godtools.shared.tool.parser.model.Text
import org.cru.godtools.shared.tool.parser.model.stylesParent
import org.cru.godtools.shared.tool.parser.withDeviceType

@RunOnAndroidWith(AndroidJUnit4::class)
Expand Down

0 comments on commit 63c4abc

Please sign in to comment.