129 lines
3.2 KiB
Go
129 lines
3.2 KiB
Go
package appui
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/egoist/asuka/signal"
|
||
|
|
"github.com/egoist/asuka/ui"
|
||
|
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (c *controller) sessionMenu(s ocapi.Session) {
|
||
|
|
ui.Alert(ui.AlertProps{
|
||
|
|
Title: "会话",
|
||
|
|
ActionSheet: true,
|
||
|
|
Actions: []ui.Action{
|
||
|
|
{Title: ui.Val("重命名"), Icon: "pencil", OnPress: func() { c.renameSession(s) }},
|
||
|
|
{Title: ui.Val("归档"), Icon: "archivebox", OnPress: func() { c.archiveSession(s) }},
|
||
|
|
{Title: ui.Val("删除"), Icon: ui.IconTrash, Role: ui.ActionDestructive,
|
||
|
|
OnPress: func() { c.confirmDelete(s) }},
|
||
|
|
},
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *controller) renameSession(s ocapi.Session) {
|
||
|
|
name := signal.New(s.Title)
|
||
|
|
ui.Alert(ui.AlertProps{
|
||
|
|
Title: "重命名会话",
|
||
|
|
Message: "标题留空时,服务端不会自动生成新标题。",
|
||
|
|
Fields: []ui.AlertField{{Text: name, Placeholder: "标题"}},
|
||
|
|
Actions: []ui.Action{
|
||
|
|
{Title: ui.Val("取消"), Role: ui.ActionCancel},
|
||
|
|
{Title: ui.Val("保存"), Role: ui.ActionDefault,
|
||
|
|
Disabled: signal.Func(func() bool { return strings.TrimSpace(name.Get()) == "" }),
|
||
|
|
OnPress: func() { c.patchSessionOnServer(s, name.Get()) }},
|
||
|
|
},
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *controller) patchSessionOnServer(s ocapi.Session, title string) {
|
||
|
|
client := c.client
|
||
|
|
if client == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
dir := sessionDir(s)
|
||
|
|
go func() {
|
||
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||
|
|
defer cancel()
|
||
|
|
t := strings.TrimSpace(title)
|
||
|
|
err := client.UpdateSession(ctx, s.ID, dir, ocapi.SessionPatch{Title: &t})
|
||
|
|
ui.Post(func() {
|
||
|
|
if err != nil {
|
||
|
|
c.status.Set(friendlyError(err))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.sessions.Update(func(list []ocapi.Session) []ocapi.Session {
|
||
|
|
next := make([]ocapi.Session, len(list))
|
||
|
|
copy(next, list)
|
||
|
|
for i := range next {
|
||
|
|
if next[i].ID == s.ID {
|
||
|
|
next[i].Title = t
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return next
|
||
|
|
})
|
||
|
|
})
|
||
|
|
}()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *controller) archiveSession(s ocapi.Session) {
|
||
|
|
client := c.client
|
||
|
|
if client == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
dir := sessionDir(s)
|
||
|
|
go func() {
|
||
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||
|
|
defer cancel()
|
||
|
|
archived := true
|
||
|
|
err := client.UpdateSession(ctx, s.ID, dir, ocapi.SessionPatch{Archived: &archived})
|
||
|
|
ui.Post(func() {
|
||
|
|
if err != nil {
|
||
|
|
c.status.Set(friendlyError(err))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.removeSession(s.ID)
|
||
|
|
})
|
||
|
|
}()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *controller) confirmDelete(s ocapi.Session) {
|
||
|
|
ui.Alert(ui.AlertProps{
|
||
|
|
Title: "删除会话?",
|
||
|
|
Message: "会话及其子会话会被永久删除,无法撤销。",
|
||
|
|
Actions: []ui.Action{
|
||
|
|
{Title: ui.Val("取消"), Role: ui.ActionCancel},
|
||
|
|
{Title: ui.Val("删除"), Role: ui.ActionDestructive,
|
||
|
|
OnPress: func() { c.deleteSession(s) }},
|
||
|
|
},
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *controller) deleteSession(s ocapi.Session) {
|
||
|
|
client := c.client
|
||
|
|
if client == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
dir := sessionDir(s)
|
||
|
|
go func() {
|
||
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||
|
|
defer cancel()
|
||
|
|
err := client.DeleteSession(ctx, s.ID, dir)
|
||
|
|
ui.Post(func() {
|
||
|
|
if err != nil {
|
||
|
|
c.status.Set(friendlyError(err))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.removeSession(s.ID)
|
||
|
|
})
|
||
|
|
}()
|
||
|
|
}
|
||
|
|
|
||
|
|
func sessionDir(s ocapi.Session) string {
|
||
|
|
if s.Location != nil {
|
||
|
|
return s.Location.Directory
|
||
|
|
}
|
||
|
|
return ""
|
||
|
|
}
|