go_dreamfactory/cmd/v2/ui/toy_userinfo.go
2022-08-10 19:31:21 +08:00

129 lines
4.0 KiB
Go

package ui
import (
"fmt"
"go_dreamfactory/cmd/v2/lib/common"
"go_dreamfactory/cmd/v2/model"
"go_dreamfactory/cmd/v2/service"
"go_dreamfactory/cmd/v2/service/observer"
"go_dreamfactory/comm"
"go_dreamfactory/modules/user"
"go_dreamfactory/pb"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/atotto/clipboard"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
)
type toyUserInfo struct {
toyAdapter
dataList *widget.List
titleLabel *widget.Label
userInfo *service.UserInfo
data binding.ExternalStringList
obs observer.Observer
copyBtn *widget.Button
}
func (this *toyUserInfo) Init(obs observer.Observer) error {
this.obs = obs
this.titleLabel = widget.NewLabel(common.USERINFO_TITLE)
this.data = binding.BindStringList(&[]string{})
this.copyBtn = widget.NewButton(common.USERINFO_BTN_COPY, func() {
if this.userInfo != nil && this.userInfo.DbUser != nil {
_ = clipboard.WriteAll(this.userInfo.DbUser.Uid)
}
})
this.copyBtn.Disable()
this.dataList = widget.NewListWithData(this.data,
func() fyne.CanvasObject {
return widget.NewLabel("template")
},
func(i binding.DataItem, o fyne.CanvasObject) {
o.(*widget.Label).Bind(i.(binding.String))
},
)
this.widget = widget.NewCard("", "",
container.NewBorder(container.NewHBox(this.titleLabel, layout.NewSpacer(), this.copyBtn),
nil, nil, nil, container.NewVScroll(this.dataList)))
this.widget.Resize(fyne.NewSize(ToyWidth, 600))
this.dataListener()
return nil
}
func (this *toyUserInfo) dataListener() {
this.obs.AddListener(observer.EVENT_USERINFO, observer.Listener{
OnNotify: func(d interface{}, args ...interface{}) {
time.Sleep(time.Millisecond * 50)
_ = this.data.Set([]string{})
if this.copyBtn.Disabled() {
this.copyBtn.Enable()
}
this.userInfo = d.(*service.UserInfo)
if this.userInfo == nil {
return
}
// _ = this.data.Append(fmt.Sprintf("%-3s\t: %s", common.USERINFO_UID, this.userInfo.DbUser.Uid))
_ = this.data.Append(fmt.Sprintf("%-3s\t: %s", common.USERINFO_ACC, this.userInfo.DbUser.Binduid))
_ = this.data.Append(fmt.Sprintf("%-3s\t: %s", common.USERINFO_NAME, this.userInfo.DbUser.Name))
_ = this.data.Append(fmt.Sprintf("%-3s\t: %s", common.USERINFO_AVATAR, cast.ToString(this.userInfo.DbUser.Avatar)))
_ = this.data.Append(fmt.Sprintf("%-3s\t: %s", common.USERINFO_VIP, cast.ToString(this.userInfo.DbUser.Vip)))
_ = this.data.Append(fmt.Sprintf("%-3s\t: %s", common.USERINFO_LV, cast.ToString(this.userInfo.DbUser.Lv)))
_ = this.data.Append(fmt.Sprintf("%-3s\t: %s", common.USERINFO_GOLD, cast.ToString(this.userInfo.DbUser.Gold)))
_ = this.data.Append(fmt.Sprintf("%-3s\t: %s", common.USERINFO_EXP, cast.ToString(this.userInfo.DbUser.Exp)))
},
})
this.obs.AddListener(observer.EVENT_REQ_RSP, observer.Listener{
OnNotify: func(d interface{}, args ...interface{}) {
data := d.(*pb.UserMessage)
//change name
if data.MainType == string(comm.ModuleUser) &&
data.SubType == user.UserSubTypeModifyName {
rsp := &pb.UserModifynameResp{}
if !comm.ProtoUnmarshal(data, rsp) {
logrus.Error("unmarshal err")
}
//TODO 改此协议 返回昵称
// _=this.data.SetValue(1, fmt.Sprintf("%-3s\t: %s", common.USERINFO_NAME, rsp.Uid))
}
},
})
this.obs.AddListener(observer.EVENT_USER_CHANGE, observer.Listener{
OnNotify: func(d interface{}, args ...interface{}) {
data := d.(*model.PushModel)
// listener gold
if data.Msg.MainType == string(comm.ModuleUser) &&
data.Msg.SubType == "reschange" {
rsp := &pb.UserResChangePush{}
if !comm.ProtoUnmarshal(data.Msg, rsp) {
logrus.Error("unmarshal err")
}
_ = this.data.SetValue(5, fmt.Sprintf("%-3s\t: %s", common.USERINFO_GOLD, cast.ToString(rsp.Gold)))
}
// listener exp
if data.Msg.MainType == string(comm.ModuleUser) &&
data.Msg.SubType == "" {
//TODO change exp
}
//
logrus.WithField("key", "a").Debug(data.Msg.SubType)
},
})
}