This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-demo/src/Counter.ts

88 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-02-20 17:58:09 +08:00
import {
text,
vlayout,
ViewHolder,
VMPanel,
ViewModel,
Gravity,
NativeCall,
Text,
Color,
log,
logw,
loge,
Group,
LayoutSpec,
layoutConfig,
modal,
Panel,
} from "doric";
2019-12-04 14:07:14 +08:00
interface CountModel {
2021-02-20 17:58:09 +08:00
count: number;
2019-12-04 14:07:14 +08:00
}
2019-12-06 11:41:51 +08:00
class CounterView extends ViewHolder {
2021-02-20 17:58:09 +08:00
number!: Text;
counter!: Text;
build(root: Group) {
vlayout(
[
text({
textSize: 40,
tag: "tvNumber",
}),
2020-01-06 10:43:18 +08:00
2021-02-20 17:58:09 +08:00
text({
text: "Click To Count 1",
textSize: 20,
tag: "tvCounter",
}),
],
{
layoutConfig: layoutConfig().most(),
gravity: Gravity.Center,
space: 20,
}
).in(root);
this.number = root.findViewByTag("tvNumber")!;
this.counter = root.findViewByTag("tvCounter")!;
}
2021-02-24 15:35:44 +08:00
}
2019-12-04 14:07:14 +08:00
class CounterVM extends ViewModel<CountModel, CounterView> {
2021-02-20 17:58:09 +08:00
onAttached(s: CountModel, vh: CounterView) {
vh.counter.onClick = () => {
this.updateState((state) => {
state.count++;
});
};
}
onBind(s: CountModel, vh: CounterView) {
vh.number.text = `${s.count}`;
log(`Current count is ${s.count}`);
logw(`Current count is ${s.count}`);
loge(`Current count is ${s.count}`);
2021-02-20 17:58:09 +08:00
}
2019-12-04 14:07:14 +08:00
}
@Entry
2021-02-20 17:58:09 +08:00
export class CounterPage extends VMPanel<CountModel, CounterView> {
constructor() {
super();
log("Constructor");
}
getViewHolderClass() {
return CounterView;
}
2019-12-04 14:07:14 +08:00
2021-02-20 17:58:09 +08:00
getViewModelClass() {
return CounterVM;
}
2019-12-04 14:07:14 +08:00
2021-02-20 17:58:09 +08:00
getState(): CountModel {
return {
count: 1,
};
}
}