Skip to content

Commit 66f38d2

Browse files
committed
Move brandlogo logic to ultility
1 parent 1b9bc79 commit 66f38d2

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

SupportCompanion/ContentView.swift

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ struct ContentView: View {
3232
logo
3333
.resizable()
3434
.scaledToFit()
35+
.frame(maxWidth: 230)
36+
.fixedSize(horizontal: false, vertical: true)
3537
.padding(.top, 20) // Minimal padding
3638
.padding(.horizontal, 20)
3739
}
@@ -151,22 +153,12 @@ struct ContentView: View {
151153

152154
private func loadLogoForCurrentColorScheme() {
153155
let base64Logo = colorScheme == .dark ? appState.preferences.brandLogo : appState.preferences.brandLogoLight.isEmpty ? appState.preferences.brandLogo : appState.preferences.brandLogoLight
154-
loadLogo(base64Logo: base64Logo)
155-
}
156-
157-
private func loadLogo(base64Logo: String) {
158-
if base64Logo.isEmpty {
159-
showLogo = false
160-
} else if let decodedImage = base64ToImage(base64Logo) {
161-
brandLogo = decodedImage
162-
showLogo = true
163-
} else {
164-
Logger.shared.logDebug("Invalid Base64 string for brand logo.")
165-
showLogo = false
156+
showLogo = loadLogo(base64Logo: base64Logo)
157+
if showLogo {
158+
brandLogo = base64ToImage(base64Logo)
166159
}
167160
}
168161

169-
170162
@ViewBuilder
171163
private func sidebarItem(for item: SidebarItem) -> some View {
172164
HStack {
@@ -246,17 +238,3 @@ struct HoverEffectModifier: ViewModifier {
246238
}
247239
}
248240
}
249-
250-
func base64ToImage(_ base64String: String) -> Image? {
251-
// Decode Base64 string to Data
252-
guard let imageData = Data(base64Encoded: base64String, options: .ignoreUnknownCharacters),
253-
let cgImageSource = CGImageSourceCreateWithData(imageData as CFData, nil),
254-
let cgImage = CGImageSourceCreateImageAtIndex(cgImageSource, 0, nil) else {
255-
Logger.shared.logDebug("Failed to decode Base64 string to Image")
256-
return nil
257-
}
258-
259-
// Return a SwiftUI Image
260-
return Image(decorative: cgImage, scale: 1.0, orientation: .up)
261-
}
262-
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// ImageUtilities.swift
3+
// SupportCompanion
4+
//
5+
// Created by Tobias Almén on 2024-12-11.
6+
//
7+
8+
import Foundation
9+
import SwiftUI
10+
11+
func base64ToImage(_ base64String: String) -> Image? {
12+
// Decode Base64 string to Data
13+
guard let imageData = Data(base64Encoded: base64String, options: .ignoreUnknownCharacters),
14+
let cgImageSource = CGImageSourceCreateWithData(imageData as CFData, nil),
15+
let cgImage = CGImageSourceCreateImageAtIndex(cgImageSource, 0, nil) else {
16+
Logger.shared.logDebug("Failed to decode Base64 string to Image")
17+
return nil
18+
}
19+
20+
// Return a SwiftUI Image
21+
return Image(decorative: cgImage, scale: 1.0, orientation: .up)
22+
}
23+
24+
func loadLogo(base64Logo: String) -> Bool {
25+
if base64Logo.isEmpty {
26+
return false
27+
} else if let decodedImage = base64ToImage(base64Logo) {
28+
return true
29+
} else {
30+
Logger.shared.logDebug("Invalid Base64 string for brand logo.")
31+
return false
32+
}
33+
}

0 commit comments

Comments
 (0)