Skip to content

Commit

Permalink
feat: added layer background styling
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelsalaja committed Oct 2, 2023
1 parent f6e9502 commit e0e0253
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 141 deletions.
24 changes: 20 additions & 4 deletions Layers.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
1111504A2AC8CD5A00C0D356 /* LayerActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 111150392AC8CD5A00C0D356 /* LayerActions.swift */; };
1111504B2AC8CD5A00C0D356 /* Layer+Example.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1111503A2AC8CD5A00C0D356 /* Layer+Example.swift */; };
1111504C2AC8CD5A00C0D356 /* Layer+Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1111503C2AC8CD5A00C0D356 /* Layer+Constants.swift */; };
1111504E2AC8CD5A00C0D356 /* Layer+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1111503E2AC8CD5A00C0D356 /* Layer+Extensions.swift */; };
111150512AC96F4500C0D356 /* Layer+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 111150502AC96F4500C0D356 /* Layer+Helpers.swift */; };
111150532AC99EAA00C0D356 /* Layer+Animations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 111150522AC99EAA00C0D356 /* Layer+Animations.swift */; };
111150562AC9B66F00C0D356 /* ConfettiSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 111150552AC9B66F00C0D356 /* ConfettiSwiftUI */; };
111150592AC9BD2300C0D356 /* FluidGradient in Frameworks */ = {isa = PBXBuildFile; productRef = 111150582AC9BD2300C0D356 /* FluidGradient */; };
118DD3D62ACB0F6300B03E88 /* Modifiers+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 118DD3D52ACB0F6300B03E88 /* Modifiers+Extensions.swift */; };
118DD3D82ACB101000B03E88 /* ButtonStyle+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 118DD3D72ACB101000B03E88 /* ButtonStyle+Extensions.swift */; };
118DD3DA2ACB105E00B03E88 /* Color+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 118DD3D92ACB105E00B03E88 /* Color+Extensions.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -48,9 +50,11 @@
111150392AC8CD5A00C0D356 /* LayerActions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LayerActions.swift; sourceTree = "<group>"; };
1111503A2AC8CD5A00C0D356 /* Layer+Example.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Layer+Example.swift"; sourceTree = "<group>"; };
1111503C2AC8CD5A00C0D356 /* Layer+Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Layer+Constants.swift"; sourceTree = "<group>"; };
1111503E2AC8CD5A00C0D356 /* Layer+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Layer+Extensions.swift"; sourceTree = "<group>"; };
111150502AC96F4500C0D356 /* Layer+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Layer+Helpers.swift"; sourceTree = "<group>"; };
111150522AC99EAA00C0D356 /* Layer+Animations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Layer+Animations.swift"; sourceTree = "<group>"; };
118DD3D52ACB0F6300B03E88 /* Modifiers+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Modifiers+Extensions.swift"; sourceTree = "<group>"; };
118DD3D72ACB101000B03E88 /* ButtonStyle+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ButtonStyle+Extensions.swift"; sourceTree = "<group>"; };
118DD3D92ACB105E00B03E88 /* Color+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Color+Extensions.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -85,6 +89,7 @@
111150202AC8CCDF00C0D356 /* Layers */ = {
isa = PBXGroup;
children = (
118DD3D42ACB0F4D00B03E88 /* Helpers */,
1111502F2AC8CD5A00C0D356 /* Components */,
1111503B2AC8CD5A00C0D356 /* Utilities */,
111150212AC8CCDF00C0D356 /* LayersApp.swift */,
Expand Down Expand Up @@ -125,13 +130,22 @@
children = (
1111503A2AC8CD5A00C0D356 /* Layer+Example.swift */,
1111503C2AC8CD5A00C0D356 /* Layer+Constants.swift */,
1111503E2AC8CD5A00C0D356 /* Layer+Extensions.swift */,
111150502AC96F4500C0D356 /* Layer+Helpers.swift */,
111150522AC99EAA00C0D356 /* Layer+Animations.swift */,
);
path = Utilities;
sourceTree = "<group>";
};
118DD3D42ACB0F4D00B03E88 /* Helpers */ = {
isa = PBXGroup;
children = (
118DD3D52ACB0F6300B03E88 /* Modifiers+Extensions.swift */,
118DD3D72ACB101000B03E88 /* ButtonStyle+Extensions.swift */,
118DD3D92ACB105E00B03E88 /* Color+Extensions.swift */,
);
path = Helpers;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -218,11 +232,13 @@
111150472AC8CD5A00C0D356 /* LayerText.swift in Sources */,
111150242AC8CCDF00C0D356 /* ContentView.swift in Sources */,
111150222AC8CCDF00C0D356 /* LayersApp.swift in Sources */,
118DD3D62ACB0F6300B03E88 /* Modifiers+Extensions.swift in Sources */,
1111504A2AC8CD5A00C0D356 /* LayerActions.swift in Sources */,
111150512AC96F4500C0D356 /* Layer+Helpers.swift in Sources */,
1111504E2AC8CD5A00C0D356 /* Layer+Extensions.swift in Sources */,
111150532AC99EAA00C0D356 /* Layer+Animations.swift in Sources */,
1111504C2AC8CD5A00C0D356 /* Layer+Constants.swift in Sources */,
118DD3D82ACB101000B03E88 /* ButtonStyle+Extensions.swift in Sources */,
118DD3DA2ACB105E00B03E88 /* Color+Extensions.swift in Sources */,
111150432AC8CD5A00C0D356 /* LayerButton.swift in Sources */,
111150442AC8CD5A00C0D356 /* LayerHeader.swift in Sources */,
111150422AC8CD5A00C0D356 /* LayerImagePicker.swift in Sources */,
Expand Down
18 changes: 15 additions & 3 deletions Layers/Components/LayerBackground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@
import SwiftUI

struct LayerBackground: View {
@State private var z: Double = 0
@State private var opacity: Double
@State private var color: Color

internal init(z: Double = 0,
opacity: Double = 0.25,
color: Color = .black)
{
self.z = z
self.opacity = opacity
self.color = color
}

var body: some View {
ZStack {
Color(.black.opacity(0.25))
.ignoresSafeArea()
}
Color(color.opacity(opacity)).ignoresSafeArea()
}.zIndex(z)
}
}
16 changes: 13 additions & 3 deletions Layers/Components/LayerStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ struct LayerStack: View {

var content: AnyView

public init<Content>(@ViewBuilder content: @escaping () -> Content) where Content: View {
public init<Content>(
@ViewBuilder content: @escaping () -> Content) where Content: View
{
self.content = AnyView(content())
}

var body: some View {
VStack {
Spacer()

ZStack {
VStack(alignment: .leading, spacing: 24) {
content
Expand All @@ -41,13 +44,20 @@ struct LayerStack: View {
}
}
}
.layerPadding()
.padding(
EdgeInsets(
top: 0,
leading: 16,
bottom: 0,
trailing: 16
)
)
}
}

#Preview {
ZStack {
Color(.black.opacity(0.25)).ignoresSafeArea()
LayerBackground()

LayerStack {
VStack {}
Expand Down
2 changes: 1 addition & 1 deletion Layers/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct ContentView: View {
show.toggle()
}
}
}y
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions Layers/Helpers/ButtonStyle+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// ButtonStyle+Extensions.swift
// Layers
//
// Created by Raphael Salaja on 02/10/2023.
//

import SwiftUI

struct ScaleButtonStyle: ButtonStyle {
public init() {}

public func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.scaleEffect(configuration.isPressed ? Constants.Scale.pressed : 1)
.transition(.scale(scale: 1.0))
}
}

extension ButtonStyle where Self == ScaleButtonStyle {
static var scale: ScaleButtonStyle {
ScaleButtonStyle()
}
}
30 changes: 30 additions & 0 deletions Layers/Helpers/Color+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Color+Extensions.swift
// Layers
//
// Created by Raphael Salaja on 02/10/2023.
//

import SwiftUI

extension Color {

/// Returns a Boolean value indicating whether the color is dark.
var isDark: Bool {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0

guard UIColor(self).getRed(&red, green: &green, blue: &blue, alpha: nil)
else {
return false
}

let luminance =
Constants.Luminance.red * red +
Constants.Luminance.green * green +
Constants.Luminance.blue * blue

return luminance < Constants.Luminance.threshold
}
}
68 changes: 68 additions & 0 deletions Layers/Helpers/Modifiers+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Modifiers+Extensions.swift
// Layers
//
// Created by Raphael Salaja on 02/10/2023.
//

import SwiftUI

// MARK: - Transition

struct SimpleTransition: ViewModifier {
func body(content: Content) -> some View {
content
.transition(.scale(scale: 1.0))
}
}

extension View {
func simpleTransition() -> some View {
modifier(SimpleTransition())
}
}

// MARK: - Layer

struct LayerTitleStyle: ViewModifier {
func body(content: Content) -> some View {
content
.font(.system(.title3, design: .rounded, weight: .bold))
}
}

struct LayerDescriptionStyle: ViewModifier {
func body(content: Content) -> some View {
content
.font(.system(.headline, design: .rounded, weight: .medium))
.minimumScaleFactor(0.1)
.foregroundColor(Color(.systemGray))
}
}

struct LayerButtonStyle: ViewModifier {
var color: Color

func body(content: Content) -> some View {
content
.fixedSize()
.font(.body)
.fontWeight(.bold)
.fontDesign(.rounded)
.foregroundColor(color.isDark ? Color.white : Color.black)
}
}

extension View {
func layerTitleStyle() -> some View {
modifier(LayerTitleStyle())
}

func layerDescriptionStyle() -> some View {
modifier(LayerDescriptionStyle())
}

func layerButtonStyle(color: Color) -> some View {
modifier(LayerButtonStyle(color: color))
}
}
4 changes: 0 additions & 4 deletions Layers/Utilities/Layer+Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public enum Constants {
static let brightness: CGFloat = 0.85
}

enum Styling {
static let padding: EdgeInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
}

enum Animations {
enum Namespaces {
static let namespace: Namespace.ID = Namespace().wrappedValue
Expand Down
30 changes: 22 additions & 8 deletions Layers/Utilities/Layer+Example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,28 @@ struct Layer_Example: View {
)
}
}

LayerButton(
text: $action,
color: Color(.systemBlue)
) {
withAnimation(.smooth) {
index = (index + 1) % 4
action = actions[index][0]
if index == 3 {
LayerButton(
text: $action,
color: Color(.systemBlue),
icon: "photo"
) {
withAnimation(.smooth) {
index = (index + 1) % 4
action = actions[index][0]
}
}
}
else {
LayerButton(
text: $action,
color: Color(.systemBlue),
icon: ""
) {
withAnimation(.smooth) {
index = (index + 1) % 4
action = actions[index][0]
}
}
}
}
Expand Down
Loading

0 comments on commit e0e0253

Please sign in to comment.