Files
work_cation/views/otherUsers.go
T

126 lines
3.4 KiB
Go

package views
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"time"
"work_cation/global"
"work_cation/models"
"work_cation/repo"
"work_cation/service"
)
func otherUser(w fyne.Window) fyne.CanvasObject {
gridWrap := container.NewGridWrap(fyne.NewSize(200, 190))
findService, err := service.Zeroconf.FindService()
if err != nil {
return widget.NewLabel("网络异常请稍后尝试")
}
go func() {
for _, online := range findService {
var (
onlineCopy = online
)
baseCardV, err := itemOnlineUserView(w, &onlineCopy)
if err != nil {
continue
}
gridWrap.Add(baseCardV)
}
}()
scroll := container.NewScroll(gridWrap)
return container.NewBorder(nil, nil, nil, nil, scroll)
}
func itemOnlineUserView(w fyne.Window, data *models.Online) (fyne.CanvasObject, error) {
u := repo.UserFollow.GetUser(global.DB, data.ID)
var followLabel *widget.Label
const (
noFollow = "未关注"
isFollow = "已关注"
)
if u == nil {
followLabel = widget.NewLabel(noFollow)
} else {
followLabel = widget.NewLabel(isFollow)
}
user, err := service.Client.GetUser(data)
if err != nil {
return nil, err
}
followButton := widget.NewButton("关注", func() {
if followLabel.Text == isFollow {
dialog.ShowInformation("结果", "已关注成功", w)
return
}
err = repo.UserFollow.Follow(global.DB, &models.UserFollows{Users: *user})
if err != nil {
dialog.ShowInformation("关注失败", err.Error(), w)
return
}
dialog.ShowInformation("结果", "关注成功", w)
followLabel.SetText(isFollow)
})
showErrButton := widget.NewButton("查看主页", func() {
otherUserIndexWin(user, data)
})
card := widget.NewCard(user.Name, user.Ip, container.NewVBox(followLabel, showErrButton, followButton))
return card, nil
}
func otherUserIndexWin(user *models.Users, data *models.Online) {
myApp := fyne.CurrentApp()
myWindow := myApp.NewWindow(user.Name)
myWindow.CenterOnScreen()
myWindow.Resize(fyne.NewSize(620, 500))
myWindow.SetContent(otherUserIndexView(myWindow, data))
myWindow.Show()
}
func otherUserIndexView(w fyne.Window, online *models.Online) fyne.CanvasObject {
gridWrap := container.NewGridWrap(fyne.NewSize(200, 200))
cards, _ := service.Client.GetCards(online)
for _, baseCard := range cards {
var baseCardCopy = baseCard
baseCardV := baseOtherCardView(w, baseCardCopy, online)
gridWrap.Add(baseCardV)
}
scroll := container.NewScroll(gridWrap)
return container.NewBorder(nil, nil, nil, nil, scroll)
}
func baseOtherCardView(w fyne.Window, baseCardCopy models.BaseCard, online *models.Online) *widget.Card {
saveCard := repo.BaseCard.Find(global.DB, baseCardCopy.UUID)
infoWid := container.NewVBox(
widget.NewLabel(baseCardCopy.ToolType),
widget.NewLabel(fmt.Sprintf("本地:%s", saveCard.UpdateTx.Format(time.DateTime))),
widget.NewLabel(fmt.Sprintf("作者:%s", baseCardCopy.UpdateTx.Format(time.DateTime))),
)
baseCardV := widget.NewCard(baseCardCopy.Title, baseCardCopy.Text, container.NewBorder(nil, widget.NewToolbar(
widget.NewToolbarAction(theme.DownloadIcon(), func() {
b := saveCard.UpdateTx.Unix() == baseCardCopy.UpdateTx.Unix()
if b {
dialog.ShowInformation("结果", "已下载本地", w)
return
}
// 下载到本地 TODO 若没关注自动关注
service.BaseCard.DownloadCard(w, baseCardCopy, online)
}),
), nil, nil, infoWid))
return baseCardV
}