diff --git a/DemoLogger/DemoLogger.xcodeproj/project.pbxproj b/DemoLogger/DemoLogger.xcodeproj/project.pbxproj index b0532b0..57218cd 100644 --- a/DemoLogger/DemoLogger.xcodeproj/project.pbxproj +++ b/DemoLogger/DemoLogger.xcodeproj/project.pbxproj @@ -314,7 +314,7 @@ PRODUCT_BUNDLE_IDENTIFIER = SK.DemoLogger; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx"; - SUPPORTS_MACCATALYST = NO; + SUPPORTS_MACCATALYST = YES; SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_VERSION = 5.0; @@ -350,7 +350,7 @@ PRODUCT_BUNDLE_IDENTIFIER = SK.DemoLogger; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx"; - SUPPORTS_MACCATALYST = NO; + SUPPORTS_MACCATALYST = YES; SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_VERSION = 5.0; diff --git a/KVKLogger.podspec b/KVKLogger.podspec new file mode 100644 index 0000000..644b264 --- /dev/null +++ b/KVKLogger.podspec @@ -0,0 +1,19 @@ +Pod::Spec.new do |spec| + spec.name = "KVKLogger" + spec.version = "0.2.15" + spec.summary = "Save logs to local DB on applications" + + spec.description = <<-DESC + Save and manage logs in your applications without Xcode. + DESC + + spec.homepage = "https://github.com/kvyatkovskys/KVKLogger" + spec.license = { :type => 'MIT', :file => 'LICENSE.md' } + spec.author = { "Kviatkovskii Sergei" => "sergejkvyatkovskij@gmail.com" } + spec.source = { :git => "https://github.com/kvyatkovskys/KVKLogger.git", :tag => "#{spec.version}" } + spec.ios.deployment_target = '15.0' + spec.swift_version = '5.0' + spec.source_files = "Sources", "Sources/**/*.swift" + spec.social_media_url = 'https://github.com/kvyatkovskys' + +end diff --git a/Sources/KVKLogger/KVKDatePopoverView.swift b/Sources/KVKLogger/KVKDatePopoverView.swift index 2b2c96d..bceb0cb 100644 --- a/Sources/KVKLogger/KVKDatePopoverView.swift +++ b/Sources/KVKLogger/KVKDatePopoverView.swift @@ -8,15 +8,14 @@ import SwiftUI struct KVKDatePopoverView: View { - - @Environment (\.dismiss) private var dismiss + @Environment(\.dismiss) private var dismiss struct DateContainer: Equatable { var start: Date var end: Date } - @Binding var date: DateContainer? + @Binding private var date: DateContainer? @State private var dateProxyStart: Date @State private var dateProxyEnd: Date @State private var showError: Bool = false @@ -74,8 +73,8 @@ struct KVKDatePopoverView: View { dismiss() } label: { Text("Apply") + .foregroundStyle(.white) .frame(maxWidth: .infinity) - .foregroundColor(.white) .padding(10) } .background(.blue) @@ -85,8 +84,8 @@ struct KVKDatePopoverView: View { dismiss() } label: { Text("Clear") + .foregroundStyle(.white) .frame(maxWidth: .infinity) - .foregroundColor(.white) .padding(10) } .background(.red) diff --git a/Sources/KVKLogger/KVKLogger+Extensions.swift b/Sources/KVKLogger/KVKLogger+Extensions.swift index 639f682..a3e774e 100644 --- a/Sources/KVKLogger/KVKLogger+Extensions.swift +++ b/Sources/KVKLogger/KVKLogger+Extensions.swift @@ -8,14 +8,6 @@ import Foundation import OSLog -public extension Date { - - var kvkConsoleFormattedDate: String { - "" - } - -} - extension Logger { private static var subsystem = Bundle.main.bundleIdentifier ?? "kvk.logger.com" diff --git a/Sources/KVKLogger/KVKLoggerProxyView.swift b/Sources/KVKLogger/KVKLoggerProxyView.swift index 99e8c90..61f25f5 100644 --- a/Sources/KVKLogger/KVKLoggerProxyView.swift +++ b/Sources/KVKLogger/KVKLoggerProxyView.swift @@ -20,8 +20,8 @@ public struct KVKLoggerView: View { struct KVKLoggerProxyView: View { - @Environment (\.managedObjectContext) private var viewContext - @Environment (\.presentationMode) private var presentationMode + @Environment(\.managedObjectContext) private var viewContext + @Environment(\.presentationMode) private var presentationMode @FetchRequest(fetchRequest: ItemLog.fecth(), animation: .default) private var logs: FetchedResults @@ -281,44 +281,7 @@ struct KVKLoggerView_Previews: PreviewProvider { newItem3.logType = .print newItem3.items = "Test description network Test description network Test description network" viewContext.saveContext() - return Group { - KVKLoggerProxyView() - KVKLoggerProxyView() - .preferredColorScheme(.dark) - } - .environment(\.managedObjectContext, viewContext) + return KVKLoggerProxyView() + .environment(\.managedObjectContext, viewContext) } } - - -//@SectionedFetchRequest(sectionIdentifier: \.status.rawValue, sortDescriptors: [SortDescriptor(\.createdAt, order: .reverse)]) -//private var sections: SectionedFetchResults - -// in progress -// Menu { -// ForEach(vm.getCurateItems()) { (item) in -// switch item.item { -// case .groupBy: -// Picker("\(item.item.title) \(vm.selectedGroupBy.title)", -// selection: $vm.selectedGroupBy) { -// ForEach(item.subItems) { (subItem) in -// Text(subItem.title) -// } -// }.pickerStyle(.menu) -// case .filterBy: -// Picker("\(item.item.title) \(vm.selectedFilterBy.title)", -// selection: $vm.selectedFilterBy) { -// ForEach(item.subItems) { (subItem) in -// Text(subItem.title) -// } -// }.pickerStyle(.menu) -// } -// } -// if vm.selectedFilterBy != .none || vm.selectedGroupBy != .none { -// Button("Reset", role: .destructive) { -// vm.selectedFilterBy = .none -// } -// } -// } label: { -// Image(systemName: "line.3.horizontal.decrease.circle") -// } diff --git a/Sources/KVKLogger/KVKModels.swift b/Sources/KVKLogger/KVKModels.swift index c345688..4a3b923 100644 --- a/Sources/KVKLogger/KVKModels.swift +++ b/Sources/KVKLogger/KVKModels.swift @@ -224,6 +224,21 @@ enum SettingSubItem: String, Identifiable, CaseIterable { self } + var daysInLive: Int { + switch self { + case .everyDay: + 1 + case .everyWeek: + 7 + case .everyMonth: + 30 + case .everyYear: + 365 + case .none: + -1 + } + } + var title: String { switch self { case .everyDay: diff --git "a/Sources/KVKLogger/KVKPersistence\320\241ontroller.swift" "b/Sources/KVKLogger/KVKPersistence\320\241ontroller.swift" index 310fe4c..4a0ef6d 100644 --- "a/Sources/KVKLogger/KVKPersistence\320\241ontroller.swift" +++ "b/Sources/KVKLogger/KVKPersistence\320\241ontroller.swift" @@ -39,20 +39,20 @@ final class KVKPersistenceСontroller { container.persistentStoreDescriptions = [store] container.loadPersistentStores { (desc, error) in if let error = error as? NSError { - debugPrint("Unresolved error \(error), \(error.userInfo)") + debugPrint("KVKLogger: Unresolved error \(error), \(error.userInfo)") } } backgroundContext = container.newBackgroundContext() container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump - checkOldRecords() + checkOldRecordsAndDeleteIfNeeded() } func save(log: ItemLogProxy) { // temporary checking a file if let url = cacheDBURL, !FileManager.default.fileExists(atPath: url.path) { - debugPrint("Can't find DB in directory.") + debugPrint("KVKLogger: Can't find DB in directory.") return } @@ -69,15 +69,15 @@ final class KVKPersistenceСontroller { itemLog.type_ = log.type?.rawValue try self.backgroundContext.save() } catch { - debugPrint("Could not save data. \(error), \(error.localizedDescription)") + debugPrint("KVKLogger: Could not save data. \(error), \(error.localizedDescription)") } } } - private func checkOldRecords() { - // temporary checking a file + private func checkOldRecordsAndDeleteIfNeeded() { + debugPrint("KVKLogger: Checking the old records; Last clear date - \(KVKSharedData.shared.lastClearByDate); Auto deleting \(KVKSharedData.shared.clearBy.rawValue).") if let url = cacheDBURL, !FileManager.default.fileExists(atPath: url.path) { - debugPrint("Can't find DB in directory.") + debugPrint("KVKLogger: Can't find DB in directory.") return } @@ -86,6 +86,9 @@ final class KVKPersistenceСontroller { KVKSharedData.shared.needToDeleteOldRecords(from: lastRecord.createdAt) { backgroundContext.deleteAll(onlyOldRecords: true) KVKSharedData.shared.lastClearByDate = Date() + debugPrint("KVKLogger: The old records was successefully deleted.") + } else { + debugPrint("KVKLogger: No need to delete the old records.") } } @@ -162,13 +165,12 @@ extension NSManagedObjectContext { func fetchLastRecord() -> ItemLog? { let request = NSFetchRequest(entityName: ItemLog.entityName) request.fetchLimit = 1 - request.predicate = NSPredicate(format: "createdAt_ < %@", KVKSharedData.shared.lastClearByDate as NSDate) - request.sortDescriptors = [NSSortDescriptor(keyPath: \ItemLog.createdAt_, ascending: false)] + request.sortDescriptors = [NSSortDescriptor(keyPath: \ItemLog.createdAt_, ascending: true)] do { return try fetch(request).first } catch { let nsError = error as NSError - debugPrint("Unresolved error \(nsError), \(nsError.userInfo)") + debugPrint("KVKLogger: Unresolved error \(nsError), \(nsError.userInfo)") return nil } } @@ -190,19 +192,18 @@ extension NSManagedObjectContext { NSManagedObjectContext.mergeChanges(fromRemoteContextSave: deletedObjects, into: [self]) } catch { let nsError = error as NSError - debugPrint("Unresolved error \(nsError), \(nsError.userInfo)") + debugPrint("KVKLogger: Unresolved error \(nsError), \(nsError.userInfo)") } } func saveContext() { guard hasChanges else { return } - performAndWait { [weak self] in do { try self?.save() } catch { let nsError = error as NSError - debugPrint("Unresolved error \(nsError), \(nsError.userInfo)") + debugPrint("KVKLogger: Unresolved error \(nsError), \(nsError.userInfo)") } } } diff --git a/Sources/KVKLogger/KVKSharedData.swift b/Sources/KVKLogger/KVKSharedData.swift index 182df40..533aaf6 100644 --- a/Sources/KVKLogger/KVKSharedData.swift +++ b/Sources/KVKLogger/KVKSharedData.swift @@ -10,7 +10,7 @@ import SwiftUI final class KVKSharedData { static let shared = KVKSharedData() - + @AppStorage("clearBy") private var clearBy_: String? @AppStorage("clearByDate") private var lastClearByDate_: String? @@ -25,7 +25,6 @@ final class KVKSharedData { var clearBy: SettingSubItem { get { guard let item = clearBy_ else { return .everyWeek } - return SettingSubItem(rawValue: item) ?? .everyWeek } set { @@ -56,20 +55,14 @@ final class KVKSharedData { from: fromDate, to: toDate).day ?? 0 switch clearBy { - case .everyDay: - return days >= 1 - case .everyWeek: - return days > 7 - case .everyMonth: - return days > 30 - case .everyYear: - return days > 365 + case .everyDay, .everyWeek, .everyMonth, .everyYear: + return days >= clearBy.daysInLive case .none: return false } } - init() { + private init() { if clearBy_ == nil { clearBy = .everyWeek }