Skip to content

Commit

Permalink
Use background color unscoped modifier in box test (#1895)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton authored Mar 30, 2024
1 parent 37c7652 commit 10a016a
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,48 @@ package app.cash.redwood.layout.view

import android.content.Context
import android.view.View
import android.widget.Space
import android.view.View.MeasureSpec.AT_MOST
import android.view.View.MeasureSpec.EXACTLY
import app.cash.redwood.Modifier
import app.cash.redwood.layout.widget.Spacer
import app.cash.redwood.ui.Density
import app.cash.redwood.ui.Dp
import kotlin.math.min

internal class ViewSpacer(
context: Context,
) : Spacer<View> {
) : View(context), Spacer<View> {
private val density = Density(context.resources)

override val value = Space(context)
override val value get() = this

override var modifier: Modifier = Modifier

override fun width(width: Dp) {
value.minimumWidth = with(density) { width.toPxInt() }
invalidate()
value.requestLayout()
}

override fun height(height: Dp) {
value.minimumHeight = with(density) { height.toPxInt() }
invalidate()
value.requestLayout()
}

private fun invalidate() {
value.invalidate()
value.requestLayout()
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
setMeasuredDimension(
getDefaultSizeSpace(suggestedMinimumWidth, widthMeasureSpec),
getDefaultSizeSpace(suggestedMinimumHeight, heightMeasureSpec),
)
}

/** Replicates the behavior of [android.widget.Space]. */
private fun getDefaultSizeSpace(size: Int, measureSpec: Int): Int {
val specMode = MeasureSpec.getMode(measureSpec)
val specSize = MeasureSpec.getSize(measureSpec)
return when (specMode) {
AT_MOST -> min(size, specSize)
EXACTLY -> specSize
else -> size
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.example.redwood.testing.modifier.BackgroundColor
import com.example.redwood.testing.modifier.Reuse
import com.example.redwood.testing.widget.Button
import com.example.redwood.testing.widget.Button2
import com.example.redwood.testing.widget.Rectangle
import com.example.redwood.testing.widget.ScopedTestRow
import com.example.redwood.testing.widget.Split
import com.example.redwood.testing.widget.TestRow
Expand All @@ -41,7 +40,6 @@ open class EmptyTestSchemaWidgetFactory : TestSchemaWidgetFactory<Unit> {
}
override fun Button2(): Button2<Unit> = TODO()
override fun TextInput(): TextInput<Unit> = TODO()
override fun Rectangle(): Rectangle<Unit> = TODO()
override fun BackgroundColor(value: Unit, modifier: BackgroundColor) {
}
override fun Split(): Split<Unit> = TODO()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import android.widget.TextView
import com.example.redwood.testing.modifier.BackgroundColor
import com.example.redwood.testing.modifier.Reuse
import com.example.redwood.testing.widget.Button
import com.example.redwood.testing.widget.Rectangle
import com.example.redwood.testing.widget.Split
import com.example.redwood.testing.widget.TestSchemaWidgetFactory
import com.example.redwood.testing.widget.Text
Expand All @@ -36,9 +35,8 @@ class AndroidTestSchemaWidgetFactory(
override fun Button(): Button<View> = ViewButton(ButtonWidget(context))
override fun Button2() = TODO()
override fun TextInput() = TODO()
override fun Rectangle(): Rectangle<View> = ViewRectangle(context)
override fun BackgroundColor(value: View, modifier: BackgroundColor) {
value.setBackgroundColor(modifier.color)
value.setBackgroundColor(modifier.color.toInt())
}
override fun Split(): Split<View> = TODO()
override fun Reuse(value: View, modifier: Reuse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import app.cash.zipline.loader.withDevelopmentServerPush
import com.example.redwood.testing.launcher.TestAppSpec
import com.example.redwood.testing.protocol.host.TestSchemaProtocolFactory
import com.example.redwood.testing.treehouse.TestAppPresenter
import com.example.redwood.testing.treehouse.testAppSerializersModule
import com.example.redwood.testing.widget.TestSchemaWidgetSystem
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.snackbar.Snackbar.LENGTH_INDEFINITE
Expand Down Expand Up @@ -128,7 +129,9 @@ class TestAppActivity : ComponentActivity() {
embeddedDir = "/".toPath(),
embeddedFileSystem = applicationContext.assets.asFileSystem(),
stateStore = FileStateStore(
json = Json,
json = Json {
serializersModule = testAppSerializersModule
},
fileSystem = FileSystem.SYSTEM,
directory = applicationContext.getDir("TreehouseState", MODE_PRIVATE).toOkioPath(),
),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class HtmlWidgetFactory(

override fun TextInput() = TODO()

override fun Rectangle() = TODO()

override fun Button2() = TODO()

override fun ScopedTestRow() = throw UnsupportedOperationException()
Expand Down
4 changes: 0 additions & 4 deletions test-app/ios-uikit/TestApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
635661DA21F12B7E00DD7240 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 635661D821F12B7E00DD7240 /* Main.storyboard */; };
635661DC21F12B8000DD7240 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 635661DB21F12B8000DD7240 /* Assets.xcassets */; };
635661DF21F12B8000DD7240 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 635661DD21F12B8000DD7240 /* LaunchScreen.storyboard */; };
96C741A32ACCAB13007C41FE /* RectangleBinding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C741A22ACCAB13007C41FE /* RectangleBinding.swift */; };
CB85C0B725AFE61A007A2CC7 /* TestAppViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB85C0B625AFE61A007A2CC7 /* TestAppViewController.swift */; };
CB9E3E822AB379C4007A87CD /* ButtonBinding.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB9E3E812AB379C4007A87CD /* ButtonBinding.swift */; };
CB9F76562810A8A8008CF457 /* IosHostApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB9F76552810A8A8008CF457 /* IosHostApi.swift */; };
Expand All @@ -29,7 +28,6 @@
635661DE21F12B8000DD7240 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
635661E021F12B8000DD7240 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
63E90CF521FEBBB700449E04 /* main.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = main.framework; path = "../ios-shared/build/xcode-frameworks/main.framework"; sourceTree = "<group>"; };
96C741A22ACCAB13007C41FE /* RectangleBinding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RectangleBinding.swift; sourceTree = "<group>"; };
CB85C0B625AFE61A007A2CC7 /* TestAppViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestAppViewController.swift; sourceTree = "<group>"; };
CB9E3E812AB379C4007A87CD /* ButtonBinding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonBinding.swift; sourceTree = "<group>"; };
CB9F76552810A8A8008CF457 /* IosHostApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IosHostApi.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -76,7 +74,6 @@
10AA3D4B28C03D32006F125E /* IosTestSchemaWidgetFactory.swift */,
10AA3D4D28C0EA40006F125E /* TextBinding.swift */,
CB9E3E812AB379C4007A87CD /* ButtonBinding.swift */,
96C741A22ACCAB13007C41FE /* RectangleBinding.swift */,
);
path = TestApp;
sourceTree = "<group>";
Expand Down Expand Up @@ -176,7 +173,6 @@
files = (
10AA3D4C28C03D32006F125E /* IosTestSchemaWidgetFactory.swift in Sources */,
CB9E3E822AB379C4007A87CD /* ButtonBinding.swift in Sources */,
96C741A32ACCAB13007C41FE /* RectangleBinding.swift in Sources */,
CB85C0B725AFE61A007A2CC7 /* TestAppViewController.swift in Sources */,
10AA3D4E28C0EA40006F125E /* TextBinding.swift in Sources */,
635661D521F12B7E00DD7240 /* AppDelegate.swift in Sources */,
Expand Down
4 changes: 0 additions & 4 deletions test-app/ios-uikit/TestApp/IosTestSchemaWidgetFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ class IosTestSchemaWidgetFactory: TestSchemaWidgetFactory {
fatalError()
}

func Rectangle() -> Rectangle {
return RectangleBinding()
}

func ScopedTestRow() -> ScopedTestRow {
fatalError()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.redwood.testing.treehouse

import Foundation
import TestAppKt
import UIKit
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.modules.SerializersModule

class RectangleBinding: Rectangle {

private let root: UIView = {
let view = UIView()
return view
}()

var modifier: Modifier = ExposedKt.modifier()
var value: Any { root }

func backgroundColor(backgroundColor: UInt32) {
root.backgroundColor = UIColor(argb: UInt(backgroundColor))
}

func cornerRadius(cornerRadius: Float) {
root.layer.cornerRadius = CGFloat(cornerRadius)
}
val testAppSerializersModule = SerializersModule {
contextual(UInt::class, UInt.serializer())
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package com.example.redwood.testing.treehouse

import app.cash.zipline.Zipline

private val zipline by lazy { Zipline.get() }
private val zipline by lazy { Zipline.get(testAppSerializersModule) }

@OptIn(ExperimentalJsExport::class)
@JsExport
Expand Down
Loading

0 comments on commit 10a016a

Please sign in to comment.