Add App.
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"colors" : [
|
||||
{
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
},
|
||||
{
|
||||
"idiom" : "mac",
|
||||
"scale" : "1x",
|
||||
"size" : "16x16"
|
||||
},
|
||||
{
|
||||
"idiom" : "mac",
|
||||
"scale" : "2x",
|
||||
"size" : "16x16"
|
||||
},
|
||||
{
|
||||
"idiom" : "mac",
|
||||
"scale" : "1x",
|
||||
"size" : "32x32"
|
||||
},
|
||||
{
|
||||
"idiom" : "mac",
|
||||
"scale" : "2x",
|
||||
"size" : "32x32"
|
||||
},
|
||||
{
|
||||
"idiom" : "mac",
|
||||
"scale" : "1x",
|
||||
"size" : "128x128"
|
||||
},
|
||||
{
|
||||
"idiom" : "mac",
|
||||
"scale" : "2x",
|
||||
"size" : "128x128"
|
||||
},
|
||||
{
|
||||
"idiom" : "mac",
|
||||
"scale" : "1x",
|
||||
"size" : "256x256"
|
||||
},
|
||||
{
|
||||
"idiom" : "mac",
|
||||
"scale" : "2x",
|
||||
"size" : "256x256"
|
||||
},
|
||||
{
|
||||
"idiom" : "mac",
|
||||
"scale" : "1x",
|
||||
"size" : "512x512"
|
||||
},
|
||||
{
|
||||
"idiom" : "mac",
|
||||
"scale" : "2x",
|
||||
"size" : "512x512"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// Item.swift
|
||||
// ChatMasterMind
|
||||
//
|
||||
// Created by Oleksandr Kozachuk on 2023-06-24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class ChatPair: Identifiable {
|
||||
let id: UUID
|
||||
var timestamp: Date
|
||||
var question: String
|
||||
var answer: String?
|
||||
var previousVersions: [ChatPair]
|
||||
|
||||
init(question: String, answer: String? = nil, timestamp: Date = Date(), previousVersions: [ChatPair] = []) {
|
||||
self.id = UUID()
|
||||
self.question = question
|
||||
self.answer = answer
|
||||
self.timestamp = timestamp
|
||||
self.previousVersions = previousVersions
|
||||
}
|
||||
}
|
||||
|
||||
@Model
|
||||
final class ChatHistory: Identifiable {
|
||||
let id: UUID
|
||||
var name: String
|
||||
var chatPairs: [ChatPair]
|
||||
|
||||
init(name: String, chatPairs: [ChatPair] = []) {
|
||||
self.id = UUID()
|
||||
self.name = name
|
||||
self.chatPairs = chatPairs
|
||||
}
|
||||
|
||||
func addChatPair(question: String, answer: String? = nil, timestamp: Date = Date()) {
|
||||
let newPair = ChatPair(question: question, answer: answer, timestamp: timestamp)
|
||||
chatPairs.append(newPair)
|
||||
}
|
||||
|
||||
func editChatPair(withId id: UUID, question: String? = nil, answer: String? = nil) {
|
||||
guard let index = chatPairs.firstIndex(where: { $0.id == id }) else { return }
|
||||
let newChatPair = chatPairs[index]
|
||||
newChatPair.previousVersions.append(chatPairs[index])
|
||||
if let question = question {
|
||||
newChatPair.question = question
|
||||
}
|
||||
if let answer = answer {
|
||||
newChatPair.answer = answer
|
||||
}
|
||||
newChatPair.timestamp = Date()
|
||||
chatPairs[index] = newChatPair
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>aps-environment</key>
|
||||
<string>development</string>
|
||||
<key>com.apple.developer.aps-environment</key>
|
||||
<string>development</string>
|
||||
<key>com.apple.developer.icloud-container-identifiers</key>
|
||||
<array/>
|
||||
<key>com.apple.developer.icloud-services</key>
|
||||
<array>
|
||||
<string>CloudKit</string>
|
||||
</array>
|
||||
<key>com.apple.security.app-sandbox</key>
|
||||
<true/>
|
||||
<key>com.apple.security.files.user-selected.read-only</key>
|
||||
<true/>
|
||||
<key>com.apple.security.network.client</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// ChatMasterMindApp.swift
|
||||
// ChatMasterMind
|
||||
//
|
||||
// Created by Oleksandr Kozachuk on 2023-06-24.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
@main
|
||||
struct ChatMasterMindApp: App {
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
}
|
||||
.modelContainer(for: ChatHistory.self)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>UIBackgroundModes</key>
|
||||
<array>
|
||||
<string>remote-notification</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user