2026-11-16 09:09:09 +08:00
|
|
|
package appui
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
|
"github.com/egoist/mygo/ui/mobile"
|
2026-11-16 09:09:09 +08:00
|
|
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
// new session -----------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
func (a *App) newSessionForm() {
|
|
|
|
|
a.newTitle = ""
|
|
|
|
|
a.newOpen = true
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
func (a *App) newSessionSheet(c *ui.Context) {
|
|
|
|
|
if !a.newOpen {
|
2026-11-16 09:09:09 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
mobile.Sheet(c, &a.newOpen, mobile.SheetOptions{Detents: []float32{0.4}}, func() {
|
|
|
|
|
ui.Column(c).FillWidth().Padding(8, 16, 24, 16).Gap(10).Children(func() {
|
|
|
|
|
ui.Text(c, "新建会话").FontSize(20).Bold()
|
|
|
|
|
ui.TextInput(c, &a.newTitle).Placeholder("标题(可选)").AutoFocus()
|
|
|
|
|
if ui.PrimaryButton(c, "创建").FillWidth().Clicked() {
|
|
|
|
|
a.newOpen = false
|
|
|
|
|
a.createSession()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) createSession() {
|
|
|
|
|
dir := a.dir()
|
|
|
|
|
title := a.newTitle
|
2026-11-16 09:09:09 +08:00
|
|
|
go func() {
|
2026-11-18 13:00:20 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
2026-11-16 09:09:09 +08:00
|
|
|
defer cancel()
|
2026-11-18 13:00:20 +08:00
|
|
|
sess, err := a.client.CreateSession(ctx, ocapi.CreateSessionOptions{Title: title, Directory: dir})
|
|
|
|
|
a.post(func() {
|
2026-11-16 09:09:09 +08:00
|
|
|
if err != nil {
|
2026-11-18 13:00:20 +08:00
|
|
|
a.errorText = friendlyError(err)
|
2026-11-16 09:09:09 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.sessions = append([]ocapi.Session{sess}, a.sessions...)
|
|
|
|
|
a.openChat(sess)
|
2026-11-16 09:09:09 +08:00
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
// session actions -------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
func (a *App) openSessionActions(s ocapi.Session) {
|
|
|
|
|
a.sessionActionOpen = true
|
|
|
|
|
a.sessionActionTarget = s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) sessionActionSheet(c *ui.Context) {
|
|
|
|
|
if !a.sessionActionOpen {
|
2026-11-16 09:09:09 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
s := a.sessionActionTarget
|
|
|
|
|
mobile.ActionSheet(c, &a.sessionActionOpen, mobile.ActionSheetOptions{Title: s.Title, Cancel: "取消"},
|
|
|
|
|
mobile.Action{Label: "重命名", OnTap: func() { a.renameSession(s) }},
|
|
|
|
|
mobile.Action{Label: "归档", OnTap: func() { a.setArchived(s, true) }},
|
|
|
|
|
mobile.Action{Label: "删除", Destructive: true, OnTap: func() { a.deleteSession(s) }},
|
2026-11-16 10:31:20 +08:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
func (a *App) renameSession(s ocapi.Session) {
|
|
|
|
|
a.renameOpen = true
|
|
|
|
|
a.renameTarget = s
|
|
|
|
|
a.renameText = s.Title
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) renameSheet(c *ui.Context) {
|
|
|
|
|
if !a.renameOpen {
|
2026-11-16 10:31:20 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
mobile.Sheet(c, &a.renameOpen, mobile.SheetOptions{Detents: []float32{0.3}}, func() {
|
|
|
|
|
ui.Column(c).FillWidth().Padding(8, 16, 24, 16).Gap(10).Children(func() {
|
|
|
|
|
ui.Text(c, "重命名").FontSize(20).Bold()
|
|
|
|
|
ui.TextInput(c, &a.renameText).AutoFocus()
|
|
|
|
|
if ui.PrimaryButton(c, "保存").FillWidth().Clicked() {
|
|
|
|
|
a.renameOpen = false
|
|
|
|
|
a.doRename()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) doRename() {
|
|
|
|
|
title := a.renameText
|
|
|
|
|
s := a.renameTarget
|
2026-11-16 10:31:20 +08:00
|
|
|
go func() {
|
2026-11-18 13:00:20 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
2026-11-16 10:31:20 +08:00
|
|
|
defer cancel()
|
2026-11-18 13:00:20 +08:00
|
|
|
err := a.client.UpdateSession(ctx, s.ID, a.dir(), ocapi.SessionPatch{Title: &title})
|
|
|
|
|
a.post(func() {
|
2026-11-16 10:31:20 +08:00
|
|
|
if err != nil {
|
2026-11-18 13:00:20 +08:00
|
|
|
a.errorText = friendlyError(err)
|
2026-11-16 10:31:20 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
a.patchSession(s.ID, func(x *ocapi.Session) { x.Title = title })
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) setArchived(s ocapi.Session, on bool) {
|
|
|
|
|
go func() {
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
err := a.client.UpdateSession(ctx, s.ID, a.dir(), ocapi.SessionPatch{Archived: &on})
|
|
|
|
|
a.post(func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
a.errorText = friendlyError(err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) deleteSession(s ocapi.Session) {
|
|
|
|
|
a.sessionDeleteOpen = true
|
|
|
|
|
target := s
|
|
|
|
|
a.sessionDeleteTarget = &target
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) sessionDeleteConfirm(c *ui.Context) {
|
|
|
|
|
if !a.sessionDeleteOpen {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
title := ""
|
|
|
|
|
if a.sessionDeleteTarget != nil {
|
|
|
|
|
title = a.sessionDeleteTarget.Title
|
|
|
|
|
}
|
|
|
|
|
buttons := ui.AlertDialog(c, &a.sessionDeleteOpen, "删除会话?",
|
|
|
|
|
"“"+title+"”及其子会话将被删除,且无法恢复。", "取消", "删除")
|
|
|
|
|
if buttons == 1 {
|
|
|
|
|
a.doDeleteSession()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) doDeleteSession() {
|
|
|
|
|
if a.sessionDeleteTarget == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
s := *a.sessionDeleteTarget
|
|
|
|
|
go func() {
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
err := a.client.DeleteSession(ctx, s.ID, a.dir())
|
|
|
|
|
a.post(func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
a.errorText = friendlyError(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
a.removeSession(s.ID)
|
|
|
|
|
if a.chat != nil && a.chat.id == s.ID {
|
|
|
|
|
a.closeChat()
|
2026-11-16 10:31:20 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
}
|