Skip to content

Commit

Permalink
Merge pull request #4 from kvyatkovskys/feature/v_0.2.15
Browse files Browse the repository at this point in the history
Feature/v 0.2.15
  • Loading branch information
kvyatkovskys authored Feb 18, 2024
2 parents 913a7bf + b1e9488 commit 04def1d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 80 deletions.
4 changes: 2 additions & 2 deletions DemoLogger/DemoLogger.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions KVKLogger.podspec
Original file line number Diff line number Diff line change
@@ -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" => "[email protected]" }
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
9 changes: 4 additions & 5 deletions Sources/KVKLogger/KVKDatePopoverView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,8 +73,8 @@ struct KVKDatePopoverView: View {
dismiss()
} label: {
Text("Apply")
.foregroundStyle(.white)
.frame(maxWidth: .infinity)
.foregroundColor(.white)
.padding(10)
}
.background(.blue)
Expand All @@ -85,8 +84,8 @@ struct KVKDatePopoverView: View {
dismiss()
} label: {
Text("Clear")
.foregroundStyle(.white)
.frame(maxWidth: .infinity)
.foregroundColor(.white)
.padding(10)
}
.background(.red)
Expand Down
8 changes: 0 additions & 8 deletions Sources/KVKLogger/KVKLogger+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
45 changes: 4 additions & 41 deletions Sources/KVKLogger/KVKLoggerProxyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemLog>
Expand Down Expand Up @@ -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<String, ItemLog>

// 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")
// }
15 changes: 15 additions & 0 deletions Sources/KVKLogger/KVKModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 14 additions & 13 deletions Sources/KVKLogger/KVKPersistenceСontroller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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.")
}
}

Expand Down Expand Up @@ -162,13 +165,12 @@ extension NSManagedObjectContext {
func fetchLastRecord() -> ItemLog? {
let request = NSFetchRequest<ItemLog>(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
}
}
Expand All @@ -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)")
}
}
}
Expand Down
15 changes: 4 additions & 11 deletions Sources/KVKLogger/KVKSharedData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -25,7 +25,6 @@ final class KVKSharedData {
var clearBy: SettingSubItem {
get {
guard let item = clearBy_ else { return .everyWeek }

return SettingSubItem(rawValue: item) ?? .everyWeek
}
set {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 04def1d

Please sign in to comment.