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

95 lines
1.7 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(
[
2021-07-07 17:30:08 +08:00
text({
text: `Current language is ${Environment.localeLanguage}`,
}),
text({
text: `Current country is ${Environment.localeCountry}`,
}),
2021-05-13 16:26:48 +08:00
this.number = text({
2021-02-20 17:58:09 +08:00
textSize: 40,
tag: "tvNumber",
}),
2020-01-06 10:43:18 +08:00
2021-05-13 16:26:48 +08:00
this.counter = text({
2021-02-20 17:58:09 +08:00
text: "Click To Count 1",
textSize: 20,
tag: "tvCounter",
}),
],
{
layoutConfig: layoutConfig().most(),
gravity: Gravity.Center,
space: 20,
}
).in(root);
}
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 = () => {
Promise.resolve(this.getState().count).then(count => {
this.updateState((state) => {
state.count = count + 1;
});
})
2021-02-20 17:58:09 +08:00
};
}
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> {
2021-07-07 17:30:08 +08:00
state = {
count: 1,
}
2021-02-20 17:58:09 +08:00
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 {
2021-07-07 17:30:08 +08:00
return this.state;
2021-02-20 17:58:09 +08:00
}
}