Add App.
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
//
|
||||
// ContentView.swift
|
||||
// ChatMasterMind
|
||||
//
|
||||
// Created by Oleksandr Kozachuk on 2023-06-24.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
import MarkdownView
|
||||
|
||||
struct ContentView: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@Query private var chatHistoryList: [ChatHistory]
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
List {
|
||||
ForEach(chatHistoryList) { chatHistory in
|
||||
NavigationLink(destination: ChatHistoryDetailView(chatHistory: chatHistory)) {
|
||||
Text(chatHistory.name)
|
||||
}
|
||||
}
|
||||
.onDelete(perform: deleteItems)
|
||||
}
|
||||
.toolbar {
|
||||
#if os(iOS)
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
EditButton()
|
||||
}
|
||||
#endif
|
||||
ToolbarItem {
|
||||
Button(action: newChat) {
|
||||
Label("New chat", systemImage: "plus")
|
||||
}
|
||||
}
|
||||
}
|
||||
Text("Select an chat")
|
||||
}
|
||||
}
|
||||
|
||||
private func newChat() {
|
||||
withAnimation {
|
||||
let newChatHistory = ChatHistory(name: "test1")
|
||||
modelContext.insert(newChatHistory)
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteItems(offsets: IndexSet) {
|
||||
withAnimation {
|
||||
for index in offsets {
|
||||
modelContext.delete(chatHistoryList[index])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ChatHistoryDetailView: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
var chatHistory: ChatHistory
|
||||
@State private var newQuestion: String = ""
|
||||
@State private var pairToEdit: ChatPair? = nil
|
||||
@State private var newAnswer: String = ""
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
List {
|
||||
ForEach(chatHistory.chatPairs) { chatPair in
|
||||
VStack(alignment: .leading) {
|
||||
MarkdownView(text: chatPair.question)
|
||||
if let answer = chatPair.answer {
|
||||
MarkdownView(text: answer)
|
||||
.tint(.secondary)
|
||||
}
|
||||
Button(action: {
|
||||
pairToEdit = chatPair
|
||||
newAnswer = chatPair.answer ?? ""
|
||||
}) {
|
||||
Text("Edit")
|
||||
.foregroundColor(.blue)
|
||||
}
|
||||
}
|
||||
Text("Timestamp: \(chatPair.timestamp)")
|
||||
.foregroundColor(.secondary)
|
||||
.font(.footnote)
|
||||
}
|
||||
}
|
||||
HStack {
|
||||
TextEditor(text: $newQuestion)
|
||||
.frame(height: 100)
|
||||
.overlay(RoundedRectangle(cornerRadius: 5).stroke(Color.gray))
|
||||
Button(action: {
|
||||
addChatPair()
|
||||
}) {
|
||||
Text("Add")
|
||||
}
|
||||
Button(action: {
|
||||
cancelEdit()
|
||||
}) {
|
||||
Text("Cancel")
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
.navigationTitle(chatHistory.name)
|
||||
.sheet(item: $pairToEdit) { pairToEdit in
|
||||
VStack {
|
||||
Text(pairToEdit.question)
|
||||
TextEditor(text: $newAnswer)
|
||||
.overlay(RoundedRectangle(cornerRadius: 5).stroke(Color.gray))
|
||||
.frame(maxHeight: .infinity)
|
||||
Button(action: {
|
||||
editChatPair(pairToEdit)
|
||||
}) {
|
||||
Text("Save")
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
private func addChatPair() {
|
||||
guard !newQuestion.isEmpty else { return }
|
||||
withAnimation {
|
||||
let newPair = ChatPair(question: newQuestion)
|
||||
chatHistory.chatPairs.append(newPair)
|
||||
newQuestion = ""
|
||||
do {
|
||||
try modelContext.save()
|
||||
} catch {
|
||||
print("Error saving model context: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func editChatPair(_ chatPair: ChatPair) {
|
||||
guard !newAnswer.isEmpty else { return }
|
||||
withAnimation {
|
||||
chatHistory.editChatPair(withId: chatPair.id, question: nil, answer: newAnswer)
|
||||
newAnswer = ""
|
||||
pairToEdit = nil
|
||||
do {
|
||||
try modelContext.save()
|
||||
} catch {
|
||||
print("Error saving model context: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func cancelEdit() {
|
||||
newAnswer = ""
|
||||
pairToEdit = nil
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ContentView()
|
||||
.modelContainer(for: ChatHistory.self, inMemory: true)
|
||||
}
|
||||
Reference in New Issue
Block a user