Files
openchamber-android-native/internal/appui/sessions_actions.go
T

165 lines
4.1 KiB
Go
Raw Normal View History

package appui
import (
"context"
"time"
"github.com/egoist/mygo/ui"
"github.com/egoist/mygo/ui/mobile"
"github.com/openchamber/openchamber-native/internal/ocapi"
)
// new session -----------------------------------------------------------
func (a *App) newSessionForm() {
a.newTitle = ""
a.newOpen = true
}
func (a *App) newSessionSheet(c *ui.Context) {
if !a.newOpen {
return
}
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
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
sess, err := a.client.CreateSession(ctx, ocapi.CreateSessionOptions{Title: title, Directory: dir})
a.post(func() {
if err != nil {
a.errorText = friendlyError(err)
return
}
a.sessions = append([]ocapi.Session{sess}, a.sessions...)
a.openChat(sess)
})
}()
}
// 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 {
return
}
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) }},
)
}
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 {
return
}
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
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := a.client.UpdateSession(ctx, s.ID, a.dir(), ocapi.SessionPatch{Title: &title})
a.post(func() {
if err != nil {
a.errorText = friendlyError(err)
return
}
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()
}
})
}()
}