Skip to content

Commit

Permalink
Merge pull request #11 from maiqingqiang/update-20240927
Browse files Browse the repository at this point in the history
Switch Core Data & Fix Bug
  • Loading branch information
johnmai-dev authored Oct 3, 2024
2 parents f1c099d + e2b133e commit ffbb516
Show file tree
Hide file tree
Showing 56 changed files with 1,127 additions and 575 deletions.
83 changes: 68 additions & 15 deletions ChatMLX.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "ec0ae960c62380f78719a325a372403e15367161b18d5538115cf8b27ab586fb",
"originHash" : "91755e46d4857336740696612733433e7fa7ef978bc35290de8f756037756422",
"pins" : [
{
"identity" : "alamofire",
Expand Down Expand Up @@ -51,8 +51,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/maiqingqiang/Jinja",
"state" : {
"revision" : "eee42768cb951fa1baa7dce0202da5b53ab49f15",
"version" : "1.0.3"
"revision" : "4ffa95ce02e013c992287e19e3bbd620b6cc233a",
"version" : "1.0.4"
}
},
{
Expand All @@ -78,8 +78,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/ml-explore/mlx-swift-examples/",
"state" : {
"revision" : "fb5ee82860107a0f3bcb83be57e8a3cb67a72c6b",
"version" : "1.16.0"
"branch" : "main",
"revision" : "caa5caf4ca64e79c3ad8f64e2a49f9b85ef1bc19"
}
},
{
Expand Down Expand Up @@ -109,24 +109,6 @@
"version" : "1.5.0"
}
},
{
"identity" : "swift-async-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-async-algorithms",
"state" : {
"revision" : "6ae9a051f76b81cc668305ceed5b0e0a7fd93d20",
"version" : "1.0.1"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "9bf03ff58ce34478e66aaee630e491823326fd06",
"version" : "1.1.3"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -159,8 +141,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/huggingface/swift-transformers",
"state" : {
"revision" : "104e8ceb1bb714c4aa1619fa20bdfeb537433492",
"version" : "0.1.11"
"revision" : "0f2306713d48a75b862026ebb291926793773f52",
"version" : "0.1.12"
}
},
{
Expand Down
91 changes: 91 additions & 0 deletions ChatMLX.xcodeproj/xcshareddata/xcschemes/ChatMLX.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "526675412C85EDCB001EF113"
BuildableName = "ChatMLX.app"
BlueprintName = "ChatMLX"
ReferencedContainer = "container:ChatMLX.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "526675412C85EDCB001EF113"
BuildableName = "ChatMLX.app"
BlueprintName = "ChatMLX"
ReferencedContainer = "container:ChatMLX.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "-com.apple.CoreData.ConcurrencyDebug 1"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
<EnvironmentVariables>
<EnvironmentVariable
key = "CORESVG_VERBOSE"
value = "1"
isEnabled = "NO">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "526675412C85EDCB001EF113"
BuildableName = "ChatMLX.app"
BlueprintName = "ChatMLX"
ReferencedContainer = "container:ChatMLX.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
<key>ChatMLX.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>3</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>526675412C85EDCB001EF113</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
Expand Down
27 changes: 27 additions & 0 deletions ChatMLX/Assets.xcassets/AccentColor.colorset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "1.000",
"green" : "1.000",
"red" : "1.000"
}
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "1.000",
"green" : "1.000",
"red" : "1.000"
}
},
"idiom" : "universal"
}
],
Expand Down
21 changes: 0 additions & 21 deletions ChatMLX/Assets.xcassets/clear1.imageset/Contents.json

This file was deleted.

1 change: 0 additions & 1 deletion ChatMLX/Assets.xcassets/clear1.imageset/clear.svg

This file was deleted.

Binary file not shown.
21 changes: 0 additions & 21 deletions ChatMLX/Assets.xcassets/mlx-logo.imageset/Contents.json

This file was deleted.

37 changes: 32 additions & 5 deletions ChatMLX/ChatMLXApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
//

import Defaults
import SwiftData
import SwiftUI

@main
struct ChatMLXApp: App {
@State private var conversationViewModel: ConversationView.ViewModel = .init()
@State private var settingsViewModel: SettingsView.ViewModel = .init()
@Environment(\.scenePhase) private var scenePhase

@State private var conversationViewModel: ConversationViewModel = .init()
@State private var settingsViewModel: SettingsViewModel = .init()

@Default(.language) var language

@State private var runner = LLMRunner()

let persistenceController = PersistenceController.shared

var body: some Scene {
WindowGroup {
ConversationView()
Expand All @@ -26,8 +30,26 @@ struct ChatMLXApp: App {
)
.environment(runner)
.frame(minWidth: 900, minHeight: 580)
.errorAlert(
isPresented: $conversationViewModel.showErrorAlert,
title: $settingsViewModel.errorTitle,
error: $conversationViewModel.error
)
}
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.onChange(of: scenePhase) { _, newValue in
if newValue == .background {
let context = persistenceController.container.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
logger.error(
"scenePhase.background save error: \(error.localizedDescription)")
}
}
}
}
.modelContainer(for: [Conversation.self, Message.self])

Settings {
SettingsView()
Expand All @@ -38,7 +60,12 @@ struct ChatMLXApp: App {
)
.environment(runner)
.frame(width: 620, height: 480)
.errorAlert(
isPresented: $settingsViewModel.showErrorAlert,
title: $settingsViewModel.errorTitle,
error: $settingsViewModel.error
)
}
.modelContainer(for: [Conversation.self, Message.self])
.environment(\.managedObjectContext, persistenceController.container.viewContext)
}
}
42 changes: 42 additions & 0 deletions ChatMLX/Components/ErrorAlertModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// ErrorAlertModifier.swift
// ChatMLX
//
// Created by John Mai on 2024/10/3.
//

import SwiftUI

struct ErrorAlertModifier: ViewModifier {
@Binding var showErrorAlert: Bool
@Binding var errorTitle: String?
@Binding var error: Error?

func body(content: Content) -> some View {
content
.alert(
errorTitle ?? "Error", isPresented: $showErrorAlert,
actions: {
Button("OK") {
error = nil
}

Button("Feedback") {
error = nil
NSWorkspace.shared.open(
URL(string: "https://github.com/maiqingqiang/ChatMLX/issues")!)
}
},
message: {
Text(error?.localizedDescription ?? "An unknown error occurred.")
})
}
}

extension View {
func errorAlert(isPresented: Binding<Bool>, title: Binding<String?>, error: Binding<Error?>)
-> some View
{
modifier(ErrorAlertModifier(showErrorAlert: isPresented, errorTitle: title, error: error))
}
}
1 change: 1 addition & 0 deletions ChatMLX/Components/UltramanNavigationSplitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ struct UltramanNavigationSplitView<Sidebar: View, Detail: View>: View {
}
}

@MainActor
@ViewBuilder
func header() -> some View {
VStack(spacing: 0) {
Expand Down
15 changes: 15 additions & 0 deletions ChatMLX/Extensions/Binding+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Binding+Extensions.swift
// ChatMLX
//
// Created by John Mai on 2024/10/2.
//

import Foundation
import SwiftUI

extension Binding {
func toUnwrapped<T>(defaultValue: T) -> Binding<T> where Value == T? {
Binding<T>(get: { self.wrappedValue ?? defaultValue }, set: { self.wrappedValue = $0 })
}
}
Loading

0 comments on commit ffbb516

Please sign in to comment.