From e6b65c0d9965fa176fbe468a73ae7d3f7a5f9f2f Mon Sep 17 00:00:00 2001 From: Kviatkovskii Sergei Date: Mon, 20 Feb 2023 12:15:47 +0300 Subject: [PATCH] corrections --- Sources/KVKLogger/KVKLogDetailVM.swift | 54 ------------------ .../KVKLogger/KVKLogNetworkDetailView.swift | 56 +++++-------------- Sources/KVKLogger/KVKLogger.swift | 22 +++++--- 3 files changed, 27 insertions(+), 105 deletions(-) delete mode 100644 Sources/KVKLogger/KVKLogDetailVM.swift diff --git a/Sources/KVKLogger/KVKLogDetailVM.swift b/Sources/KVKLogger/KVKLogDetailVM.swift deleted file mode 100644 index 2c98ba7..0000000 --- a/Sources/KVKLogger/KVKLogDetailVM.swift +++ /dev/null @@ -1,54 +0,0 @@ -// -// KVKLogDetailVM.swift -// -// -// Created by Sergei Kviatkovskii on 2/18/23. -// - -import SwiftUI -import CoreData - -final class KVKLogDetailVM: ObservableObject { - - @Published var text = "" - @Published var size: String? - @Published var json = "" - @Published var isLoading = false - @Published var title = "" - - private let id: NSManagedObjectID - private let backgroundContext = KVKPersistenceСontroller.shared.backgroundContext - private var dataIsLoaded = false - private var copyTxt = "" - - init(id: NSManagedObjectID) { - self.id = id - } - - func copy() { - // in progress - UIPasteboard.general.string = copyTxt - } - - func fetchObjectIfNeeded() { - guard !dataIsLoaded else { return } - - isLoading = true - backgroundContext.perform { [weak self] in - if let ID = self?.id, let item = try? self?.backgroundContext.existingObject(with: ID) as? ItemLog { - self?.text = item.items - self?.size = item.size - self?.title = item.formattedShortCreatedAt - self?.copyTxt = item.copyTxt - do { - self?.json = try item.getNetworkJson() - } catch { - self?.json = error.localizedDescription - } - } - self?.isLoading = false - self?.dataIsLoaded = true - } - } - -} diff --git a/Sources/KVKLogger/KVKLogNetworkDetailView.swift b/Sources/KVKLogger/KVKLogNetworkDetailView.swift index 8ab90e6..5e5b718 100644 --- a/Sources/KVKLogger/KVKLogNetworkDetailView.swift +++ b/Sources/KVKLogger/KVKLogNetworkDetailView.swift @@ -12,10 +12,18 @@ struct KVKLogNetworkDetailView: View { private let maxLimitCountInText = 50 @State private var isCopied = false - @State private var resultLinelimit: Int? = 10 - @State private var requestLinelimit: Int? = 10 + @State private var text: String @ObservedObject var log: ItemLog + init(log: ItemLog) { + self.log = log + var txtDetails = log.items + if let response = try? log.getNetworkJson(), !response.isEmpty { + txtDetails += "\n\n[RESULT]:\n\n\(response)" + } + text = txtDetails + } + var body: some View { VStack(alignment: .leading, spacing: 10) { if let size = log.size { @@ -30,46 +38,10 @@ struct KVKLogNetworkDetailView: View { Spacer() } } - ScrollView { - VStack(alignment: .leading, spacing: 10) { - Text("REQUEST:") - .foregroundColor(.gray) - .multilineTextAlignment(.leading) - Text(log.items) - .lineLimit(log.items.count <= maxLimitCountInText ? nil : requestLinelimit) - .multilineTextAlignment(.leading) - if log.items.count > maxLimitCountInText && requestLinelimit != nil { - HStack { - Spacer() - Button("Expand Request") { - requestLinelimit = nil - } - Spacer() - } - .padding() - } - if let json = try? log.getNetworkJson(), !json.isEmpty { - VStack(alignment: .leading) { - Text("RESULT:") - .foregroundColor(.gray) - .multilineTextAlignment(.leading) - Text(json) - .lineLimit(json.count <= maxLimitCountInText ? nil : resultLinelimit) - .multilineTextAlignment(.leading) - if json.count > maxLimitCountInText && resultLinelimit != nil { - HStack { - Spacer() - Button("Expand Result") { - resultLinelimit = nil - } - Spacer() - } - .padding() - } - } - } - } - } + Text("REQUEST:") + .foregroundColor(.gray) + .multilineTextAlignment(.leading) + TextEditor(text: $text) } .padding(.leading) .navigationTitle(log.formattedShortCreatedAt) diff --git a/Sources/KVKLogger/KVKLogger.swift b/Sources/KVKLogger/KVKLogger.swift index d30756a..c094a1a 100644 --- a/Sources/KVKLogger/KVKLogger.swift +++ b/Sources/KVKLogger/KVKLogger.swift @@ -17,6 +17,8 @@ open class KVKLogger { /// if #DEBUG isn't setup in a project public var isDebugMode: Bool? + public var isEnableSaveIntoDB: Bool = true + @ObservedObject var vm = KVKLoggerVM() public init() {} @@ -60,15 +62,17 @@ open class KVKLogger { logType: KVKLogType, details: String?) { let date = Date() - let item = ItemLog(context: store.backgroundContext) - item.createdAt = date - item.status_ = status?.rawValue - item.logType = logType - item.type = type - item.details = details - item.items = items - item.data = data - store.save() + if isEnableSaveIntoDB { + let item = ItemLog(context: store.backgroundContext) + item.createdAt = date + item.status_ = status?.rawValue + item.logType = logType + item.type = type + item.details = details + item.items = items + item.data = data + store.save() + } if isDebugMode != false { printLog(items, details: details, status: status, type: logType, date: date)