Skip to content

Commit

Permalink
Saving nscolors with grace and added default colors based on theme
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibrahimhass committed Jul 19, 2023
1 parent 60947e3 commit d071bed
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 62 deletions.
2 changes: 1 addition & 1 deletion QRCode-Generator/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum ImageConstants {
static let close = "xmark.circle.fill"
static let hide = "eye.slash.fill"
static let history = "clock"
static let noImagePlaceHolder = "xmark.circle"
static let noImagePlaceHolder = "photo.artframe"
static let qrcode = "qrcode"
static let settings = "gearshape.fill"
static let show = "eye"
Expand Down
53 changes: 0 additions & 53 deletions QRCode-Generator/Extensions/NSColor+HexString.swift

This file was deleted.

19 changes: 15 additions & 4 deletions QRCode-Generator/Helpers/ConfigurationHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

import Combine
import SwiftUI
import Foundation

class ConfigurationHelper {
private let colorFileName = "Color.txt"
private let iconImageName = "icon"
private let iconSize = "IconSize.txt"

private var isLight: Bool { NSApp.effectiveAppearance.name == NSAppearance.Name.aqua }

private var colorPath: URL? {
FileManager.sharedContainerURL?.appendingPathComponent(colorFileName)
Expand All @@ -27,19 +30,22 @@ class ConfigurationHelper {

//MARK: - Icon Color
func getColor() -> Color {
var colorString = ""
var colorData: Data
var color: NSColor?

guard let colorPath else {
return Color.black
}

do {
colorString = try String(contentsOf: colorPath)
colorData = try Data(contentsOf: colorPath)
color = try NSKeyedUnarchiver
.unarchiveTopLevelObjectWithData(colorData) as? NSColor
} catch {
print (error)
}

return Color(NSColor(fromHex: colorString))
return Color(color ?? (isLight ? NSColor.black : NSColor.white))
}

func saveColor(_ color: Color) {
Expand All @@ -48,7 +54,12 @@ class ConfigurationHelper {
}

do {
try NSColor(color).hexString.write(to: colorPath, atomically: true, encoding: .utf8)
let data = try NSKeyedArchiver.archivedData(
withRootObject: NSColor(color),
requiringSecureCoding: true
)

try data.write(to: colorPath)
} catch {
print (error)
}
Expand Down
3 changes: 3 additions & 0 deletions QRCode-Generator/Views/QRCodeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct QRCodeView: View {
Image(systemName: ImageConstants.noImagePlaceHolder)
.resizable()
.scaledToFit()
.padding()

Text(Strings.copyValidURL)
.padding()
Expand All @@ -30,6 +31,7 @@ struct QRCodeView: View {
.resizable()
.interpolation(.none)
.scaledToFit()
.padding()
.contextMenu {
Button {
OpenPanelManager.showPanel(image: qrCodeImage, url: viewModel.lastValidURL ?? "")
Expand Down Expand Up @@ -93,6 +95,7 @@ struct QRCodeView: View {
}
.frame(width: 320, height: 400)
.onChange(of: colorScheme) { newValue in
viewModel.forceRefresh()
(NSApplication.shared.delegate as? AppDelegate)?.updateColor()
}
}
Expand Down
4 changes: 0 additions & 4 deletions QRtist.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
EDBB6ED829D2F20400A37F1C /* ConfigurationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6ED729D2F20400A37F1C /* ConfigurationView.swift */; };
EDBB6EDA29D325AF00A37F1C /* ConfigurationHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6ED929D325AF00A37F1C /* ConfigurationHelper.swift */; };
EDBB6EDC29D325DC00A37F1C /* CIImage+GetTintWithIcon.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6EDB29D325DC00A37F1C /* CIImage+GetTintWithIcon.swift */; };
EDBB6EDE29D326E000A37F1C /* NSColor+HexString.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6EDD29D326E000A37F1C /* NSColor+HexString.swift */; };
EDBB6EE029D359F300A37F1C /* NSImage+Resize.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6EDF29D359F300A37F1C /* NSImage+Resize.swift */; };
EDBB6F0229D4082A00A37F1C /* TimerManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBB6F0129D4082A00A37F1C /* TimerManager.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -59,7 +58,6 @@
EDBB6ED729D2F20400A37F1C /* ConfigurationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationView.swift; sourceTree = "<group>"; };
EDBB6ED929D325AF00A37F1C /* ConfigurationHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationHelper.swift; sourceTree = "<group>"; };
EDBB6EDB29D325DC00A37F1C /* CIImage+GetTintWithIcon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CIImage+GetTintWithIcon.swift"; sourceTree = "<group>"; };
EDBB6EDD29D326E000A37F1C /* NSColor+HexString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSColor+HexString.swift"; sourceTree = "<group>"; };
EDBB6EDF29D359F300A37F1C /* NSImage+Resize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSImage+Resize.swift"; sourceTree = "<group>"; };
EDBB6F0129D4082A00A37F1C /* TimerManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerManager.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -133,7 +131,6 @@
EDBB6EDB29D325DC00A37F1C /* CIImage+GetTintWithIcon.swift */,
EDBB6ECD29D2E9C700A37F1C /* FileManager+SharedContainer.swift */,
ED0EC66529DC015600226C3C /* FileManager+TemporaryFilePath.swift */,
EDBB6EDD29D326E000A37F1C /* NSColor+HexString.swift */,
EDBB6EDF29D359F300A37F1C /* NSImage+Resize.swift */,
EDBB6EC929D2E93600A37F1C /* NSImage+SavePNG.swift */,
ED203B372A14A15F00314C9D /* String+GetFileName.swift */,
Expand Down Expand Up @@ -228,7 +225,6 @@
EDBB6EE029D359F300A37F1C /* NSImage+Resize.swift in Sources */,
EDBB6ECA29D2E93600A37F1C /* NSImage+SavePNG.swift in Sources */,
ED0EC66C29DC1BA700226C3C /* ColorViewModel.swift in Sources */,
EDBB6EDE29D326E000A37F1C /* NSColor+HexString.swift in Sources */,
ED203B3A2A14DD9800314C9D /* OpenPanelManager.swift in Sources */,
EDBB6F0229D4082A00A37F1C /* TimerManager.swift in Sources */,
ED0EC66E29DC311800226C3C /* String+IsValidColor.swift in Sources */,
Expand Down
Binary file not shown.

0 comments on commit d071bed

Please sign in to comment.