Skip to content

Commit

Permalink
fix: sync block thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Panl committed May 18, 2023
1 parent 5c8187d commit 85aa38f
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 21 deletions.
4 changes: 2 additions & 2 deletions AICat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@
CODE_SIGN_ENTITLEMENTS = AICat/AICat.entitlements;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 43;
CURRENT_PROJECT_VERSION = 44;
DEVELOPMENT_ASSET_PATHS = "\"AICat/Preview Content\"";
DEVELOPMENT_TEAM = BG63VCU95W;
ENABLE_APP_SANDBOX = NO;
Expand Down Expand Up @@ -629,7 +629,7 @@
CODE_SIGN_ENTITLEMENTS = AICat/AICat.entitlements;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 43;
CURRENT_PROJECT_VERSION = 44;
DEVELOPMENT_ASSET_PATHS = "\"AICat/Preview Content\"";
DEVELOPMENT_TEAM = BG63VCU95W;
ENABLE_APP_SANDBOX = NO;
Expand Down
11 changes: 8 additions & 3 deletions AICat/CloudSync/StoreManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import SwiftUI

let DataStore = StoreManager()

class StoreManager {
class StoreManager: ObservableObject {

@AppStorage("AICat.allRecords.synced") var allLocalRecrodsSynced: Bool = false
@AppStorage("AICat.lastSyncTime") var lastSyncedTime: Int?
@Published var syncError: CKError?

let receiveDataFromiCloud = PassthroughSubject<Void, Never>()

Expand Down Expand Up @@ -75,6 +77,9 @@ class StoreManager {
if moreComing {
try await pullChangesAndSaveToDB()
}
if records.count > 0 {
receiveDataFromiCloud.send(())
}
}

func sync(complete: ((CKError?) -> Void)?) {
Expand All @@ -85,9 +90,11 @@ class StoreManager {
} else {
try await syncAllRecords()
}
lastSyncedTime = Date.now.timeInSecond
complete?(nil)
} catch {
complete?(error as? CKError)
syncError = error as? CKError
debugPrint("CloudKit push failed: \(error.localizedDescription)")
}
}
Expand All @@ -97,15 +104,13 @@ class StoreManager {
try await prepare()
try await pushAllRecords()
try await pullChangesAndSaveToDB()
receiveDataFromiCloud.send(())
allLocalRecrodsSynced = true
}

private func sync() async throws {
try await prepare()
try await pushChangesAndDeleteInDB()
try await pullChangesAndSaveToDB()
receiveDataFromiCloud.send(())
}

private func pushAllRecords() async throws {
Expand Down
22 changes: 14 additions & 8 deletions AICat/Extension/Date+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import Foundation

extension Date {

var timeInSecond: Int {
let timeStamp = Int(timeIntervalSince1970)
return timeStamp
}
var timeInSecond: Int {
let timeStamp = Int(timeIntervalSince1970)
return timeStamp
}

var timeInMillSecond: Int {
let millisecond = Int(round(timeIntervalSince1970*1000))
return millisecond
}
var timeInMillSecond: Int {
let millisecond = Int(round(timeIntervalSince1970*1000))
return millisecond
}

func toFormat(_ format: String = "yyyy.MM.dd HH:mm") -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}
}
11 changes: 10 additions & 1 deletion AICat/Pages/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,21 @@ struct MainView: View {
CompactView(store: store)
}
}
.tint(Color.primaryColor)
.onAppear {
#if os(iOS)
cancelable = NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)
.sink { _ in
print("App will enter forground")
print("App will enter foreground")
DataStore.sync(complete: nil)
}
#elseif os(macOS)
cancelable = NotificationCenter.default.publisher(for: NSApplication.willBecomeActiveNotification)
.sink { _ in
print("App will enter foreground")
DataStore.sync(complete: nil)
}
#endif
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion AICat/Pages/PremiumView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct PremiumPage: View {
FeatureView(title: "Answers from GPT Model", description: "Get accurate and relevant answers directly from the GPT Model.")
FeatureView(title: "Higher token limit for dialogues", description: "Engage in dialogues with a higer token limit")
FeatureView(title: "Unlimited custom prompts", description: "Enjoy different conversations without any restrictions")
FeatureView(title: "Get GPT4 access first", description: "Be the first to access GPT4, try out new and upcoming features.")
FeatureView(title: "iCloud Sync", description: "Sync all conversations and messages across different devices.")
}
.padding(.vertical, 20)
.padding(.horizontal, 40)
Expand Down
27 changes: 27 additions & 0 deletions AICat/Pages/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ struct SettingsView: View {

@State var isPurcahsing = false
@State var toast: Toast?
@ObservedObject var store = DataStore

var syncedText: LocalizedStringKey {
if let syncedTime = store.lastSyncedTime {
return LocalizedStringKey(Date(timeIntervalSince1970: Double(syncedTime)).toFormat())
}
return LocalizedStringKey("Not synchronized yet.")
}

var body: some View {
NavigationView {
Expand All @@ -32,6 +40,25 @@ struct SettingsView: View {
Label("Custom API", systemImage: "hammer")
.labelStyle(.titleAndIcon)
}.tint(.primaryColor)
HStack {
Image(systemName: "arrow.clockwise.icloud")
Text("iCloud Sync")
Spacer()
if let error = store.syncError {
Button(action: {
toast = .init(type: .error, message: error.localizedDescription)
}, label: {
Image(systemName: "exclamationmark.icloud.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 16, height: 16)
.foregroundColor(.red)
})
}
Text(syncedText)
.font(.manrope(size: 10, weight: .regular))
.opacity(0.4)
}
}.tint(.primaryColor)
Section("Donate") {
Button(action: { Task { await buyCatFood() } }) {
Expand Down
2 changes: 1 addition & 1 deletion AICat/Views/MessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ struct NewSessionMessageView: View {
)
.frame(height: 1)
}
.font(.manrope(size: 10, weight: .regular))
.font(.manrope(size: 10, weight: .medium))
.opacity(0.6)
.padding(.horizontal, 20)
.padding(.vertical, 6)
Expand Down
6 changes: 3 additions & 3 deletions AICat/Views/ParamsEditView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ let contextCounts: [Int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50, 100]

struct ParamsEditView: View {

var isPremium: Bool {
UserDefaults.openApiKey != nil || Apphud.hasActiveSubscription()
var hasOwnKey: Bool {
UserDefaults.openApiKey != nil
}

var models: [String] {
if isPremium {
if hasOwnKey {
return [
"gpt-3.5-turbo",
"gpt-3.5-turbo-0301",
Expand Down
4 changes: 4 additions & 0 deletions AICat/ja.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@
"Export your messages" = "あなたのメッセージをエクスポートする";
"Choose a folder and a name to store the Markdown file." = "Markdownファイルを保存するフォルダと名前を選択してください。";
"Messages file name:" = "メッセージファイル名:";
"Not synchronized yet." = "まだ同期されていません。";
"iCloud Sync" = "iCloud同期";
"NEW SESSION" = "新しいセッション";
"Sync all conversations and messages across different devices." = "異なるデバイス間で会話やメッセージをすべて同期します。";
4 changes: 4 additions & 0 deletions AICat/ko.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@
"Export your messages" = "당신의 메시지 내보내기";
"Choose a folder and a name to store the Markdown file." = "Markdown 파일을 저장할 폴더와 이름을 선택하세요.";
"Messages file name:" = "메시지 파일 이름:";
"Not synchronized yet." = "아직 동기화되지 않음";
"iCloud Sync" = "iCloud 동기화";
"NEW SESSION" = "새로운 세션";
"Sync all conversations and messages across different devices." = "다른 장치 간 모든 대화 및 메시지 동기화";
4 changes: 4 additions & 0 deletions AICat/zh-HK.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@
"Export your messages" = "匯出你的訊息";
"Choose a folder and a name to store the Markdown file." = "選擇資料夾及Markdown檔案名稱";
"Messages file name:" = "訊息檔案名稱:";
"Not synchronized yet." = "尚未同步";
"iCloud Sync" = "iCloud 同步";
"NEW SESSION" = "新會話";
"Sync all conversations and messages across different devices." = "在不同設備間同步會話和消息";
4 changes: 4 additions & 0 deletions AICat/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@
"Choose a folder and a name to store the Markdown file." = "选择文件夹";
"Messages file name:" = "消息文件名:";
"😿 Please use the App Store build" = "😿 请下载苹果商店版购买";
"Not synchronized yet." = "尚未同步";
"iCloud Sync" = "iCloud 同步";
"NEW SESSION" = "新会话";
"Sync all conversations and messages across different devices." = "在不同设备间同步会话和消息";
4 changes: 4 additions & 0 deletions AICat/zh-Hant.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@
"Export your messages" = "匯出你的訊息";
"Choose a folder and a name to store the Markdown file." = "選擇資料夾及Markdown檔案名稱";
"Messages file name:" = "訊息檔案名稱:";
"Not synchronized yet." = "尚未同步";
"iCloud Sync" = "iCloud 同步";
"NEW SESSION" = "新會話";
"Sync all conversations and messages across different devices." = "在不同設備間同步會話和消息";
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [AppCenter](https://appcenter.ms/)
- [AppHud](https://apphud.com/)

<a href="https://www.buymeacoffee.com/rego" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>
### Features

- [x] [Features of ChatGPT Web App](https://chat.openai.com/chat)
Expand All @@ -31,7 +32,7 @@
- [x] Share message as image
- [x] Export messages as Markdown
- [x] New session in Conversation when ContextMessages count more than 0
- [ ] iCloud Sync
- [x] iCloud Sync
- [ ] Copy code block


Expand Down Expand Up @@ -62,4 +63,4 @@

- Join [TestFlight](https://testflight.apple.com/join/ow799Vvb)
- Join [AICat News](https://t.me/aicatevents)
- [App Store](https://apps.apple.com/us/app/aicat-ultimate-ai-assistant/id6446479308)
- [App Store](https://apps.apple.com/us/app/aicat-ultimate-ai-assistant/id6446479308)

0 comments on commit 85aa38f

Please sign in to comment.