Skip to content

Commit

Permalink
Update to Compose SNAPSHOT 6192339
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes committed Feb 10, 2020
1 parent b0bb05d commit 4faeb29
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 94 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Expand Up @@ -61,7 +61,7 @@ allprojects {
google()
mavenCentral()
jcenter()
maven { url 'https://ci.android.com/builds/submitted/6178998/androidx_snapshot/latest/ui/repository/' }
maven { url 'https://ci.android.com/builds/submitted/6192339/androidx_snapshot/latest/ui/repository/' }
maven { url 'https://jitpack.io' }
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
Expand All @@ -87,7 +87,6 @@ subprojects {
// Enable experimental coroutines APIs, including Flow
freeCompilerArgs += "-Xuse-experimental=kotlinx.coroutines.ExperimentalCoroutinesApi"
freeCompilerArgs += "-Xuse-experimental=kotlinx.coroutines.FlowPreview"
freeCompilerArgs += "-Xuse-experimental=kotlinx.coroutines.FlowPreview"
freeCompilerArgs += "-Xuse-experimental=kotlin.Experimental"

// Set JVM target to 1.8
Expand Down
Expand Up @@ -16,11 +16,11 @@

package app.tivi.common.compose

import androidx.animation.AnimatedFloat
import androidx.compose.Composable
import androidx.ui.core.Layout
import androidx.ui.core.LayoutCoordinates
import androidx.ui.core.RepaintBoundary
import androidx.ui.foundation.ValueHolder
import androidx.ui.unit.IntPx
import androidx.ui.unit.PxPosition
import androidx.ui.unit.min
Expand All @@ -31,11 +31,13 @@ import androidx.ui.unit.px
*/
@Composable
fun WithOffset(
xOffset: ValueHolder<Float>? = null,
yOffset: ValueHolder<Float>? = null,
xOffset: AnimatedFloat? = null,
yOffset: AnimatedFloat? = null,
child: @Composable() () -> Unit
) {
Layout(children = { RepaintBoundary(children = child) }) { measurables, constraints ->
Layout(children = {
RepaintBoundary(children = child)
}) { measurables, constraints ->
if (measurables.size > 1) {
throw IllegalStateException("Only one child is allowed")
}
Expand Down
Expand Up @@ -18,10 +18,10 @@ package app.tivi.common.compose

import androidx.compose.Composable
import androidx.compose.state
import androidx.ui.animation.animatedFloat
import androidx.ui.core.DensityAmbient
import androidx.ui.core.OnChildPositioned
import androidx.ui.core.OnPositioned
import androidx.ui.core.WithDensity
import androidx.ui.foundation.animation.animatedDragValue
import androidx.ui.foundation.gestures.DragDirection
import androidx.ui.foundation.gestures.Draggable
import androidx.ui.layout.Container
Expand All @@ -45,64 +45,60 @@ fun SwipeToDismiss(
backgroundChildren: @Composable() (swipeProgress: Float, wouldCompleteOnRelease: Boolean) -> Unit,
swipeChildren: @Composable() (swipeProgress: Float, wouldCompleteOnRelease: Boolean) -> Unit
) = Stack {
val min = state { 0f }
val max = state { 0f }
val position = animatedFloat(0f)

OnPositioned(onPositioned = { coordinates ->
var min = position.min
var max = position.max

if (SwipeDirection.LEFT in swipeDirections) {
min.value = -coordinates.size.width.value.toFloat()
min = -coordinates.size.width.value.toFloat()
}
if (SwipeDirection.RIGHT in swipeDirections) {
max.value = coordinates.size.width.value.toFloat()
max = coordinates.size.width.value.toFloat()
}
position.setBounds(min, max)
})

val position = animatedDragValue(0f, min.value, max.value)
val swipeChildrenSize = state { IntPxSize(IntPx.Zero, IntPx.Zero) }
var swipeChildrenSize by state { IntPxSize(IntPx.Zero, IntPx.Zero) }
val progress = state { 0f }

if (position.value != 0f) {
// Container only accepts Dp values for it's size, whereas we have Px values. So we
// need to use WithDensity to convert them back.
// TODO: raise FR so that Container can accept Px/PxSize values
WithDensity {
with(DensityAmbient.current) {
Container(
width = swipeChildrenSize.value.width.toDp(),
height = swipeChildrenSize.value.height.toDp(),
children = {
backgroundChildren(
progress.value,
progress.value.absoluteValue >= swipeCompletePercentage
)
}
)
width = swipeChildrenSize.width.toDp(),
height = swipeChildrenSize.height.toDp()
) {
backgroundChildren(
progress.value,
progress.value.absoluteValue >= swipeCompletePercentage
)
}
}
}

OnChildPositioned(onPositioned = { coords ->
swipeChildrenSize.value = coords.size
}) {
OnChildPositioned(onPositioned = { coords -> swipeChildrenSize = coords.size }) {
Draggable(
dragDirection = DragDirection.Horizontal,
dragValue = position,
onDragStopped = {
// We can't reference AnimatedValueHolder.animatedFloat.min/max so we have to keep
// our own state /sadface
// TODO: raise FR to open up AnimatedFloat.min/max
if (max.value > 0f && position.value / max.value >= swipeCompletePercentage) {
if (position.max > 0f && position.value / position.max >= swipeCompletePercentage) {
onSwipeComplete(SwipeDirection.RIGHT)
} else if (min.value < 0f && position.value / min.value >= swipeCompletePercentage) {
} else if (position.min < 0f && position.value / position.min >= swipeCompletePercentage) {
onSwipeComplete(SwipeDirection.LEFT)
}
position.animatedFloat.animateTo(0f)
position.animateTo(0f)
},
onDragValueChangeRequested = { dragValue ->
// Update the position using snapTo
position.animatedFloat.snapTo(dragValue)
position.snapTo(dragValue)

progress.value = when {
dragValue < 0f && min.value < 0f -> dragValue / min.value
dragValue > 0f && max.value > 0f -> dragValue / max.value
dragValue < 0f && position.min < 0f -> dragValue / position.min
dragValue > 0f && position.max > 0f -> dragValue / position.max
else -> 0f
}

Expand Down
97 changes: 49 additions & 48 deletions common-ui-compose/src/main/java/app/tivi/common/compose/theme.kt
Expand Up @@ -24,7 +24,7 @@ import androidx.compose.Composable
import androidx.core.content.res.getResourceIdOrThrow
import androidx.core.content.res.use
import androidx.core.graphics.ColorUtils
import androidx.ui.core.WithDensity
import androidx.ui.core.DensityAmbient
import androidx.ui.geometry.Offset
import androidx.ui.graphics.Color
import androidx.ui.graphics.Shadow
Expand All @@ -38,7 +38,6 @@ import androidx.ui.text.font.FontStyle
import androidx.ui.text.font.FontWeight
import androidx.ui.text.font.asFontFamily
import androidx.ui.text.font.font
import androidx.ui.unit.DensityScope
import androidx.ui.unit.TextUnit
import androidx.ui.unit.em
import androidx.ui.unit.px
Expand All @@ -53,7 +52,7 @@ private const val DEFAULT_COLOR = android.graphics.Color.MAGENTA
fun MaterialThemeFromAndroidTheme(
context: Context,
children: @Composable() () -> Unit
) = WithDensity {
) {
context.obtainStyledAttributes(R.styleable.ComposeTheme).use { ta ->
/* First we'll read the Material color palette */
val primary = ta.getColor(R.styleable.ComposeTheme_colorPrimary, DEFAULT_COLOR)
Expand Down Expand Up @@ -123,7 +122,7 @@ fun MaterialThemeFromAndroidTheme(
}
}

private fun DensityScope.textStyleFromTextAppearance(context: Context, @StyleRes id: Int): TextStyle {
private fun textStyleFromTextAppearance(context: Context, @StyleRes id: Int): TextStyle {
return context.obtainStyledAttributes(id, R.styleable.ComposeTextAppearance).use { a ->
val color = a.getColor(R.styleable.ComposeTextAppearance_android_textColor, DEFAULT_COLOR)
val textSize = a.getDimensionPixelSize(R.styleable.ComposeTextAppearance_android_textSize, -1)
Expand Down Expand Up @@ -158,51 +157,53 @@ private fun DensityScope.textStyleFromTextAppearance(context: Context, @StyleRes
return null
}

TextStyle(
color = if (color != DEFAULT_COLOR) Color(color) else null,
fontSize = textSize.toSp(),
lineHeight = if (lineHeight != -1) lineHeight.toSp() else TextUnit.Inherit,
fontFamily = when {
// FYI, this only works with static font files in assets
a.hasValue(R.styleable.ComposeTextAppearance_android_fontFamily) -> {
readFontFamily(R.styleable.ComposeTextAppearance_android_fontFamily)
}
a.hasValue(R.styleable.ComposeTextAppearance_fontFamily) -> {
readFontFamily(R.styleable.ComposeTextAppearance_fontFamily)
with(DensityAmbient.current) {
TextStyle(
color = if (color != DEFAULT_COLOR) Color(color) else null,
fontSize = textSize.toSp(),
lineHeight = if (lineHeight != -1) lineHeight.toSp() else TextUnit.Inherit,
fontFamily = when {
// FYI, this only works with static font files in assets
a.hasValue(R.styleable.ComposeTextAppearance_android_fontFamily) -> {
readFontFamily(R.styleable.ComposeTextAppearance_android_fontFamily)
}
a.hasValue(R.styleable.ComposeTextAppearance_fontFamily) -> {
readFontFamily(R.styleable.ComposeTextAppearance_fontFamily)
}
// Values below are from frameworks/base attrs.xml
typeface == 1 -> FontFamily.SansSerif
typeface == 2 -> FontFamily.Serif
typeface == 3 -> FontFamily.Monospace
else -> null
},
fontStyle = if ((textStyle and Typeface.ITALIC) != 0) FontStyle.Italic else FontStyle.Normal,
fontWeight = when {
textFontWeight in 0..199 -> FontWeight.W100
textFontWeight in 200..299 -> FontWeight.W200
textFontWeight in 300..399 -> FontWeight.W300
textFontWeight in 400..499 -> FontWeight.W400
textFontWeight in 500..599 -> FontWeight.W500
textFontWeight in 600..699 -> FontWeight.W600
textFontWeight in 700..799 -> FontWeight.W700
textFontWeight in 800..899 -> FontWeight.W800
textFontWeight in 900..999 -> FontWeight.W900
// else, check the text style
(textStyle and Typeface.BOLD) != 0 -> FontWeight.Bold
else -> null
},
fontFeatureSettings = featureSettings,
shadow = if (shadowColor != DEFAULT_COLOR) {
Shadow(Color(shadowColor), Offset(shadowDx, shadowDy), shadowRadius.px)
} else {
null
},
letterSpacing = if (a.hasValue(R.styleable.ComposeTextAppearance_android_letterSpacing)) {
a.getFloat(R.styleable.ComposeTextAppearance_android_letterSpacing, 0f).em
} else {
TextUnit.Inherit
}
// Values below are from frameworks/base attrs.xml
typeface == 1 -> FontFamily.SansSerif
typeface == 2 -> FontFamily.Serif
typeface == 3 -> FontFamily.Monospace
else -> null
},
fontStyle = if ((textStyle and Typeface.ITALIC) != 0) FontStyle.Italic else FontStyle.Normal,
fontWeight = when {
textFontWeight in 0..199 -> FontWeight.W100
textFontWeight in 200..299 -> FontWeight.W200
textFontWeight in 300..399 -> FontWeight.W300
textFontWeight in 400..499 -> FontWeight.W400
textFontWeight in 500..599 -> FontWeight.W500
textFontWeight in 600..699 -> FontWeight.W600
textFontWeight in 700..799 -> FontWeight.W700
textFontWeight in 800..899 -> FontWeight.W800
textFontWeight in 900..999 -> FontWeight.W900
// else, check the text style
(textStyle and Typeface.BOLD) != 0 -> FontWeight.Bold
else -> null
},
fontFeatureSettings = featureSettings,
shadow = if (shadowColor != DEFAULT_COLOR) {
Shadow(Color(shadowColor), Offset(shadowDx, shadowDy), shadowRadius.px)
} else {
null
},
letterSpacing = if (a.hasValue(R.styleable.ComposeTextAppearance_android_letterSpacing)) {
a.getFloat(R.styleable.ComposeTextAppearance_android_letterSpacing, 0f).em
} else {
TextUnit.Inherit
}
)
)
}
}
}

Expand Down
Expand Up @@ -19,7 +19,6 @@ package app.tivi.common.compose
import androidx.annotation.DrawableRes
import androidx.compose.Composable
import androidx.ui.core.Modifier
import androidx.ui.core.WithDensity
import androidx.ui.foundation.contentColor
import androidx.ui.graphics.Color
import androidx.ui.graphics.vector.DrawVector
Expand All @@ -34,11 +33,9 @@ fun VectorImage(
tint: Color = contentColor()
) {
val vector = vectorResource(id)
WithDensity {
Container(
modifier = modifier + LayoutSize(vector.defaultWidth, vector.defaultHeight)
) {
DrawVector(vector, tint)
}
Container(
modifier = modifier + LayoutSize(vector.defaultWidth, vector.defaultHeight)
) {
DrawVector(vector, tint)
}
}
Expand Up @@ -28,11 +28,11 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.ui.animation.ColorPropKey
import androidx.ui.animation.Transition
import androidx.ui.core.DensityAmbient
import androidx.ui.core.DrawModifier
import androidx.ui.core.Modifier
import androidx.ui.core.OnChildPositioned
import androidx.ui.core.Text
import androidx.ui.core.WithDensity
import androidx.ui.foundation.Clickable
import androidx.ui.foundation.VerticalScroller
import androidx.ui.foundation.background
Expand Down Expand Up @@ -168,8 +168,8 @@ private fun EpisodeDetails(
}
}

val insets = InsetsAmbient.current
WithDensity {
with(DensityAmbient.current) {
val insets = InsetsAmbient.current
WatchButton(
modifier = LayoutGravity.BottomRight +
LayoutPadding(horizontal = 16.dp, bottom = 16.dp + insets.bottom.toDp()),
Expand Down

0 comments on commit 4faeb29

Please sign in to comment.