-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
379 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// | ||
// | ||
// DynamicUI.swift | ||
// DynamicUI | ||
// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// Modifiers.swift | ||
// DynamicUI | ||
// | ||
// Created by Wesley de Groot on 25/07/2024. | ||
// https://wesleydegroot.nl | ||
// | ||
// https://github.com/0xWDG/DynamicUI | ||
// MIT LICENCE | ||
|
||
import SwiftUI | ||
|
||
extension View { | ||
/// DynamicUIModifiers | ||
/// | ||
/// This function adds modifiers to a DynamicUIView | ||
/// | ||
/// - Parameter modifiers: The modifiers to apply | ||
/// | ||
/// - Returns: The modified view | ||
public func dynamicUIModifiers(_ modifiers: [String : AnyCodable]?) -> some View { | ||
guard let modifiers = modifiers else { | ||
return AnyView(self) | ||
} | ||
|
||
let helper = DynamicUIHelper() | ||
var tempView = AnyView(self) | ||
|
||
modifiers.forEach { key, value in | ||
switch key { | ||
case "foregroundStyle": | ||
guard let string = value.toString(), | ||
let color = helper.translateColor(string) else { return } | ||
tempView = AnyView(tempView.foregroundStyle(color)) | ||
|
||
case "backgroundStyle": | ||
if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) { | ||
guard let string = value.toString(), | ||
let color = helper.translateColor(string) else { return } | ||
tempView = AnyView(tempView.backgroundStyle(color)) | ||
} | ||
|
||
case "fontWeight": | ||
if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) { | ||
guard let string = value.toString(), | ||
let weight = helper.translateFontWeight(string) else { return } | ||
tempView = AnyView(tempView.fontWeight(.none)) | ||
} | ||
|
||
case "font": | ||
// guard let color: | ||
tempView = AnyView(tempView.font(.none)) | ||
|
||
case "frame": | ||
// guard let color: | ||
// minWidth: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealWidth: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, minHeight: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealHeight: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/) | ||
// width: <#0#> height: <#0#> | ||
tempView = AnyView(tempView) | ||
|
||
case "padding": | ||
if let length = value.toInt() { | ||
tempView = AnyView(tempView.padding(CGFloat(integerLiteral: length))) | ||
} | ||
|
||
case "opacity": | ||
guard let opacity = value.toDouble() else { break } | ||
tempView = AnyView(tempView.opacity(opacity)) | ||
|
||
default: | ||
break | ||
} | ||
} | ||
|
||
return tempView | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// | ||
// DynamicUIHelper.swift | ||
// DynamicUI | ||
// | ||
// Created by Wesley de Groot on 25/07/2024. | ||
// https://wesleydegroot.nl | ||
// | ||
// https://github.com/0xWDG/DynamicUI | ||
// MIT LICENCE | ||
|
||
import SwiftUI | ||
|
||
/// DynamicUIHelper | ||
/// | ||
/// DynamicUIHelper helps to translate Strings to native SwiftUI .context | ||
public class DynamicUIHelper { | ||
|
||
/// Translate string colors to native ``Color``. | ||
/// | ||
/// - Parameter input: Color as string | ||
/// | ||
/// - Returns: SwiftUI ``Color`` | ||
public func translateColor(_ input: String) -> Color? { | ||
switch input.lowercased() { | ||
case "red": | ||
return .red | ||
|
||
case "orange": | ||
return .orange | ||
|
||
case "yellow": | ||
return .yellow | ||
|
||
case "green": | ||
return .green | ||
|
||
case "mint": | ||
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) { | ||
return .mint | ||
} | ||
return .primary | ||
|
||
case "teal": | ||
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) { | ||
return .teal | ||
} | ||
return .primary | ||
|
||
case "cyan": | ||
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) { | ||
return .cyan | ||
} | ||
return .primary | ||
|
||
case "blue": | ||
return .blue | ||
|
||
case "indigo": | ||
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) { | ||
return .indigo | ||
} | ||
return .primary | ||
|
||
case "purple": | ||
return .purple | ||
|
||
case "pink": | ||
return .pink | ||
|
||
case "brown": | ||
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) { | ||
return .brown | ||
} | ||
return .primary | ||
|
||
case "white": | ||
return .white | ||
|
||
case "gray": | ||
return .gray | ||
|
||
case "black": | ||
return .black | ||
|
||
case "clear": | ||
return .clear | ||
|
||
case "primary": | ||
return .primary | ||
|
||
case "secondary": | ||
return .secondary | ||
|
||
default: | ||
return .primary | ||
} | ||
} | ||
|
||
/// Translate a string font weight to a native ``Font.Weight`` | ||
/// | ||
/// - Parameter input: Font weight as string | ||
/// | ||
/// - Returns: Translated ``Font.Weight`` | ||
func translateFontWeight(_ input: String) -> Font.Weight? { | ||
switch input { | ||
case "ultraLight": | ||
return .ultraLight | ||
|
||
case "thin": | ||
return .thin | ||
|
||
case "light": | ||
return .light | ||
|
||
case "regular": | ||
return .regular | ||
|
||
case "medium": | ||
return .medium | ||
|
||
case "semibold": | ||
return .semibold | ||
|
||
case "bold": | ||
return .bold | ||
|
||
case "heavy": | ||
return .heavy | ||
|
||
case "black": | ||
return .black | ||
|
||
default: | ||
return .regular | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.