110 lines
2.8 KiB
Go
110 lines
2.8 KiB
Go
package views
|
|
|
|
import (
|
|
"fmt"
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/container"
|
|
"fyne.io/fyne/v2/dialog"
|
|
"fyne.io/fyne/v2/widget"
|
|
"slices"
|
|
"work_cation/global"
|
|
"work_cation/models"
|
|
"work_cation/repo"
|
|
"work_cation/service"
|
|
)
|
|
|
|
func followUsers(w fyne.Window) fyne.CanvasObject {
|
|
gridWrap := container.NewGridWrap(fyne.NewSize(200, 190))
|
|
|
|
findService, err := service.Zeroconf.FindService()
|
|
if err != nil {
|
|
return widget.NewLabel(err.Error())
|
|
}
|
|
users, err := repo.UserFollow.All(global.DB)
|
|
if err != nil {
|
|
return widget.NewLabel(err.Error())
|
|
}
|
|
slices.SortFunc(users, func(a, b models.UserFollows) int {
|
|
return b.Sort - a.Sort
|
|
})
|
|
for _, user := range users {
|
|
index := slices.IndexFunc(findService, indexOnline2User(user.ID))
|
|
var onlineCopy *models.Online
|
|
if index != -1 {
|
|
onlineCopy = &findService[index]
|
|
}
|
|
var baseCardV fyne.CanvasObject
|
|
del := func() { gridWrap.Remove(baseCardV) }
|
|
var userCopy = user
|
|
baseCardV, err = itemFollowUserView(w, onlineCopy, &userCopy, del)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
gridWrap.Add(baseCardV)
|
|
}
|
|
|
|
scroll := container.NewScroll(gridWrap)
|
|
return container.NewBorder(nil, nil, nil, nil, scroll)
|
|
}
|
|
|
|
func indexOnline2User(id string) func(models.Online) bool {
|
|
return func(item models.Online) bool {
|
|
return item.ID == id
|
|
}
|
|
}
|
|
|
|
func itemFollowUserView(w fyne.Window, data *models.Online, user *models.UserFollows, del func()) (fyne.CanvasObject, error) {
|
|
|
|
onlineShow := widget.NewLabel("离线")
|
|
|
|
go func() {
|
|
defer func() { _ = recover() }()
|
|
if data == nil {
|
|
data = &models.Online{ID: user.ID, Ip: user.Ip, Port: user.Port}
|
|
}
|
|
newUser, err := service.Client.GetUser(data)
|
|
if err != nil {
|
|
data = nil
|
|
return
|
|
}
|
|
if err == nil {
|
|
onlineShow.SetText("在线")
|
|
}
|
|
if err == nil && newUser.UpdateTx != user.UpdateTx {
|
|
// 更新用户信息
|
|
fmt.Println("更新用户信息:", user, newUser)
|
|
repo.UserFollow.UnFollow(global.DB, user)
|
|
repo.UserFollow.Follow(global.DB, &models.UserFollows{Users: *newUser})
|
|
}
|
|
}()
|
|
|
|
followButton := widget.NewButton("取消关注", func() {
|
|
err := repo.UserFollow.UnFollow(global.DB, user)
|
|
if err != nil {
|
|
dialog.ShowInformation("取消失败", err.Error(), w)
|
|
return
|
|
}
|
|
dialog.ShowInformation("", "取消成功", w)
|
|
del()
|
|
})
|
|
showErrButton := widget.NewButton("查看主页", func() {
|
|
if data == nil {
|
|
dialog.ShowInformation("查看失败", "用户未在线", w)
|
|
return
|
|
}
|
|
otherUserIndexWin(&user.Users, data)
|
|
})
|
|
maishuiButton := widget.NewButton("聊天", func() {
|
|
//if data == nil {
|
|
// dialog.ShowInformation("查看失败", "用户未在线", w)
|
|
// return
|
|
//}
|
|
OpenChat(user.Users)
|
|
})
|
|
|
|
card := widget.NewCard(user.Name, user.Ip, container.NewVBox(onlineShow,
|
|
container.NewHBox(showErrButton, followButton),
|
|
maishuiButton))
|
|
return card, nil
|
|
}
|