feat:update demo

This commit is contained in:
pengfei.zhou 2020-03-04 13:58:47 +08:00 committed by osborn
parent e7ac263921
commit 9394e8927e

View File

@ -1,4 +1,4 @@
import { Group, text, gravity, Color, LayoutSpec, vlayout, hlayout, layoutConfig, scroller, Text, ViewHolder, VMPanel, ViewModel, network, loge, HLayout, stack, image, Gravity, takeNonNull, Scroller, Image } from "doric"; import { Group, text, gravity, Color, LayoutSpec, vlayout, hlayout, layoutConfig, scroller, Text, ViewHolder, VMPanel, ViewModel, network, loge, HLayout, stack, image, Gravity, takeNonNull, Scroller, Image, View } from "doric";
import { colors } from "./utils"; import { colors } from "./utils";
import MovieData from './movie.json' import MovieData from './movie.json'
@ -67,15 +67,20 @@ interface DoubanModel {
interface MovieModel { interface MovieModel {
doubanModel?: DoubanModel doubanModel?: DoubanModel
selectedIdx: number selectedIdx: number
anchorPos: number
} }
const frameWidth = 200
const frameHeight = 300
const padding = 20
class MovieVH extends ViewHolder { class MovieVH extends ViewHolder {
title!: Text title!: Text
gallery!: HLayout gallery!: HLayout
scrolled!: Scroller scrolled!: Scroller
movieTitle!: Text movieTitle!: Text
movieYear!: Text movieYear!: Text
anchor!: View
build(root: Group) { build(root: Group) {
vlayout( vlayout(
[ [
@ -110,9 +115,20 @@ class MovieVH extends ViewHolder {
}), }),
vlayout( vlayout(
[ [
text({ stack(
text: "↑", [
}), this.anchor = image({
imageBase64: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAANb0lEQVR4Xu2de6wcVR3Hv7+9tyTEQLhzS0zUINg7W6Lwjw9QEF9Ay5vykoeI4ut2ZyExJhqNMaIxJBgjiXRnKSjlTWmhtLQ8W4q8hZqYaATc2SbgH8aE7rkYjRrp3WMutFAovTtzdubMnD3f+++e3+N8fueTvfuaEfCPBEhgnwSEbEiABPZNgILwdJDAPAQoCI8HCVAQngESMCPAZxAzbozyhAAF8WTQ3KYZAQpixo1RnhCgIJ4Mmts0I0BBzLgxyhMCFMSTQX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RU5ErkJggg==",
layoutConfig: layoutConfig().just(),
width: 30,
height: 30,
}),
],
{
layoutConfig: layoutConfig().fit().configWidth(LayoutSpec.MOST),
}
),
this.movieTitle = text({ this.movieTitle = text({
textSize: 20, textSize: 20,
}), }),
@ -140,51 +156,95 @@ class MovieVM extends ViewModel<MovieModel, MovieVH>{
network(context).get("https://douban.uieee.com/v2/movie/top250").then(ret => { network(context).get("https://douban.uieee.com/v2/movie/top250").then(ret => {
this.updateState(state => state.doubanModel = JSON.parse(ret.data) as DoubanModel) this.updateState(state => state.doubanModel = JSON.parse(ret.data) as DoubanModel)
}) })
this.updateState(state => {
state.anchorPos = padding + frameWidth / 2
state.selectedIdx = 0
})
let scrollX = 0
vh.scrolled.onScroll = (offset) => { vh.scrolled.onScroll = (offset) => {
const frameWidth = 270 / 2 * 1.5 if (offset.x < 0 || offset.x > (state.doubanModel?.count || 0) * frameWidth + padding * 2 - Environment.screenWidth) {
const centerX = offset.x + Environment.screenWidth / 2 return
const idx = Math.floor(centerX / frameWidth) }
if (state.selectedIdx != idx) { const dx = offset.x - scrollX
scrollX = offset.x
const idx = Math.floor((offset.x + state.anchorPos - padding) / frameWidth)
if (state.selectedIdx !== idx) {
this.updateState(state => state.selectedIdx = idx) this.updateState(state => state.selectedIdx = idx)
} }
const overOff = Math.abs(centerX - frameWidth * (idx + 0.5)) takeNonNull(this.images.get(idx))(it => {
const scale = overOff / (frameWidth / 2) const scale = (offset.x + state.anchorPos - (idx + 0.5) * frameWidth - padding) / (frameWidth / 2)
takeNonNull(this.images.get(idx))(v => { it.scaleX = it.scaleY = 1.5 - Math.abs(scale * 0.5)
v.scaleX = v.scaleY = 1.5 - 0.5 * scale
}) })
this.updateArrow()
} }
} }
updateArrow() {
takeNonNull(this.images.get(this.getState().selectedIdx))(it => {
it.getLocationOnScreen(context).then(ret => {
this.getViewHolder().anchor.centerX = ret.x + frameWidth / 2
})
})
}
onItemClicked(idx: number) {
takeNonNull(this.images.get(this.getState().selectedIdx)?.superview)(it => {
it.scaleX = it.scaleY = 1
})
takeNonNull(this.images.get(idx)?.superview)(it => {
it.getLocationOnScreen(context).then(ret => {
let anchor = this.getState().anchorPos
if (ret.x < 0) {
this.getViewHolder().scrolled.scrollBy(context, { x: ret.x, y: 0 }, true)
anchor = frameWidth / 2
} else if (ret.x > Environment.screenWidth - frameWidth) {
this.getViewHolder().scrolled.scrollBy(
context,
{ x: ret.x - (Environment.screenWidth - frameWidth), y: 0 },
true)
anchor = Environment.screenWidth - frameWidth / 2
} else {
anchor = ret.x + frameWidth / 2
}
this.updateState(state => {
state.selectedIdx = idx
state.anchorPos = anchor
})
})
})
}
onBind(state: MovieModel, vh: MovieVH) { onBind(state: MovieModel, vh: MovieVH) {
if (state.doubanModel) { if (state.doubanModel) {
vh.title.text = state.doubanModel.title vh.title.text = state.doubanModel.title
vh.gallery.children.length = 0 vh.gallery.children.length = 0
const vm = this
state.doubanModel.subjects.forEach((e, idx) => { state.doubanModel.subjects.forEach((e, idx) => {
vh.gallery.addChild(stack( vh.gallery.addChild(stack(
[ [
image({ image({
layoutConfig: layoutConfig().just().configAlignment(Gravity.Center), layoutConfig: layoutConfig().just().configAlignment(Gravity.Center),
width: 270 / 2, width: frameWidth / 1.5,
height: 400 / 2, height: frameHeight / 1.5,
imageUrl: e.images.large, imageUrl: e.images.large,
onClick: function () { onClick: function () {
const v = (this as Image).superview const v = (this as Image).superview
if (v == undefined) { if (v == undefined) {
return return
} }
v.getLocationOnScreen(context).then(ret => { vm.onItemClicked(idx)
const centerX = ret.x + v.width / 2;
vh.scrolled.scrollBy(context, { x: centerX - Environment.screenWidth / 2, y: 0 }, true)
})
}, },
}).also(it => { }).also(it => {
this.images.set(idx, it) this.images.set(idx, it)
if (state.selectedIdx == idx) {
it.scaleX = it.scaleY = 1.5
}
}) })
], ],
{ {
layoutConfig: layoutConfig().just(), layoutConfig: layoutConfig().just(),
width: 270 / 2 * 1.5, width: frameWidth,
height: 400 / 2 * 1.5, height: frameHeight,
})) }))
}) })
takeNonNull(state.doubanModel.subjects[state.selectedIdx])(it => { takeNonNull(state.doubanModel.subjects[state.selectedIdx])(it => {
@ -192,6 +252,8 @@ class MovieVM extends ViewModel<MovieModel, MovieVH>{
vh.movieYear.text = it.year vh.movieYear.text = it.year
}) })
} }
vh.anchor.centerX = state.anchorPos
this.updateArrow()
} }
} }
@ -205,7 +267,7 @@ class SliderPanel extends VMPanel<MovieModel, MovieVH>{
} }
getState() { getState() {
return { selectedIdx: 0, doubanModel: MovieData } return { selectedIdx: 0, anchorPos: Environment.screenWidth / 2, doubanModel: MovieData }
} }
} }