Skip to content

Commit

Permalink
chore: project cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelsalaja committed Sep 30, 2023
1 parent 92793c0 commit cbdb0bc
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 104 deletions.
6 changes: 0 additions & 6 deletions Layers/Components/LayerBackground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,3 @@ struct LayerBackground: View {
}
}
}

#Preview {
LayerBackground {
LayerPlayground()
}
}
6 changes: 3 additions & 3 deletions Layers/Components/LayerButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ struct LayerButton: View {
.transition(.scale(scale: 1.0))
.matchedGeometryEffect(
id: "layer.button.text.\(id)",
in: LayerConstants.Animations.Namespaces.namespace
in: Constants.Animations.Namespaces.namespace
)
.foregroundColor(color.isDark ? Color.white : Color.black)

Expand Down Expand Up @@ -145,7 +145,7 @@ struct LayerButton: View {
}
.matchedGeometryEffect(
id: "layer.button.select.\(id)",
in: LayerConstants.Animations.Namespaces.namespace
in: Constants.Animations.Namespaces.namespace
)

if selected {
Expand All @@ -166,7 +166,7 @@ struct LayerButton: View {
)
.matchedGeometryEffect(
id: "layer.button.select.icon.\(id)",
in: LayerConstants.Animations.Namespaces.namespace
in: Constants.Animations.Namespaces.namespace
)
}
}
Expand Down
34 changes: 16 additions & 18 deletions Layers/Components/LayerStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ struct LayerStack: View {
VStack(alignment: .leading, spacing: 24) {
content
}
.padding(32)
.background(
Color(.layerBackground)
.matchedGeometryEffect(
id: LayerConstants.Animations.Namespaces.background,
in: LayerConstants.Animations.Namespaces.namespace
)
)
.mask {
RoundedRectangle(cornerRadius: 32, style: .continuous)
.matchedGeometryEffect(
id: LayerConstants.Animations.Namespaces.mask,
in: LayerConstants.Animations.Namespaces.namespace
)
}
.padding(32)
.background(
Color(Constants.Colors.background)
.matchedGeometryEffect(
id: Constants.Animations.Namespaces.background,
in: Constants.Animations.Namespaces.namespace
)
)
.mask {
RoundedRectangle(cornerRadius: 32, style: .continuous)
.matchedGeometryEffect(
id: Constants.Animations.Namespaces.mask,
in: Constants.Animations.Namespaces.namespace
)
}
}
}
.layerPadding()
Expand All @@ -49,9 +49,7 @@ struct LayerStack: View {
Color(.black.opacity(0.25)).ignoresSafeArea()

LayerStack {
VStack {
Text("Hello, World!")
}
VStack {}
}
}
}
8 changes: 4 additions & 4 deletions Layers/Components/LayerText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ struct LayerTitle: View {
Text(title).layerTitleStyle()
}
.matchedGeometryEffect(
id: LayerConstants.Animations.Namespaces.title,
in: LayerConstants.Animations.Namespaces.namespace
id: Constants.Animations.Namespaces.title,
in: Constants.Animations.Namespaces.namespace
)
.transition(.opacity.combined(with: .scale(scale: 1.0)))
}
Expand All @@ -47,8 +47,8 @@ struct LayerDescription: View {
Text(description).layerDescriptionStyle()
}
.matchedGeometryEffect(
id: LayerConstants.Animations.Namespaces.description,
in: LayerConstants.Animations.Namespaces.namespace
id: Constants.Animations.Namespaces.description,
in: Constants.Animations.Namespaces.namespace
)
.transition(.opacity.combined(with: .scale(scale: 1.0)))
}
Expand Down
140 changes: 69 additions & 71 deletions Layers/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,86 +8,84 @@
import SwiftUI

struct ContentView: View {
@Namespace private var namespace

@State var index = 0

let headers: [String] = [
"Heading 1",
"Heading 2",
"Heading 3",
"Heading 4"
]

let descriptions: [String] = [
"""
Ad elit do deserunt elit Lorem consectetur ipsum cillum labore id.
""",
"""
Consequat cillum amet sint elit ut ut exercitation.
""",
"""
Excepteur nostrud anim mollit magna sunt aliqua aliqua do esse.
""",
"""
Eu laborum consequat laboris eiusmod dolor.
"""
]

let contents: [LayerContent] = [
LayerContent(),
LayerContent(),
LayerContent {
LayerImagePicker()
},
LayerContent()
]

var body: some View {
@Namespace private var namespace

@State var index = 0

let headers: [String] = [
"Heading 1",
"Heading 2",
"Heading 3",
"Heading 4"
]

let descriptions: [String] = [
"""
Ad elit do deserunt elit Lorem consectetur ipsum cillum labore id.
""",
"""
Consequat cillum amet sint elit ut ut exercitation.
""",
"""
Excepteur nostrud anim mollit magna sunt aliqua aliqua do esse.
""",
"""
Eu laborum consequat laboris eiusmod dolor.
"""
]

let contents: [LayerContent] = [
LayerContent(),
LayerContent(),
LayerContent {
LayerImagePicker()
},
LayerContent()
]

var body: some View {
LayerStack {
HStack {
LayerTitle(title: headers[index]).id(headers[index])
LayerStack {
HStack {
LayerTitle(title: headers[index]).id(headers[index])

LayerIcon(icon: "xmark")
}
LayerIcon(icon: "xmark")
}

LayerDescription(description: descriptions[index]).id(descriptions[index])
LayerDescription(description: descriptions[index]).id(descriptions[index])

ForEach(contents.indices) { position in
if position == self.index {
self.contents[index]
.matchedGeometryEffect(
id: LayerConstants.Animations.Namespaces.content + String(index),
in: namespace
)
.transition(
.asymmetric(
insertion:
.opacity.combined(with: .scale(scale: 0.9)),
ForEach(contents.indices) { position in
if position == self.index {
self.contents[index]
.matchedGeometryEffect(
id: Constants.Animations.Namespaces.content + String(index),
in: namespace
)
.transition(
.asymmetric(
insertion:
.opacity.combined(with: .scale(scale: 0.9)),

removal:
.opacity.combined(with: .scale(scale: 1.0))
)
removal:
.opacity.combined(with: .scale(scale: 1.0))
)
}
)
}
}

LayerActions {
LayerButton(
id: "cancel",
text: "Cancel",
color: Color(.systemOrange)
)
LayerActions {
LayerButton(
id: "cancel",
text: "Cancel",
color: Color(.systemOrange)
)

LayerButton(
id: "continue",
text: "Continue",
color: Color(.systemBlue)
) {
withAnimation(.snappy) {
index = (index + 1) % 4
}
LayerButton(
id: "continue",
text: "Continue",
color: Color(.systemBlue)
) {
withAnimation(.snappy) {
index = (index + 1) % 4
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion Layers/Utilities/Layer+Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@

import SwiftUI

public enum LayerConstants {
public enum Constants {
enum Luminance {
static let red: CGFloat = 0.2126
static let green: CGFloat = 0.7152
static let blue: CGFloat = 0.0722
static let threshold: CGFloat = 0.7
}

enum Scale {
static let pressed: CGFloat = 0.85
static let brightness: CGFloat = 0.85
}

enum Styling {
static let padding: EdgeInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
}
Expand All @@ -30,4 +42,8 @@ public enum LayerConstants {
)
}
}

enum Colors {
static let background: Color = .init(.systemBackground)
}
}
50 changes: 49 additions & 1 deletion Layers/Utilities/Layer+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@

import SwiftUI

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

struct LayerPadding: ViewModifier {
func body(content: Content) -> some View {
content
.padding(LayerConstants.Styling.padding)
.padding(Constants.Styling.padding)
}
}

Expand All @@ -29,7 +36,48 @@ struct LayerDescriptionStyle: ViewModifier {
}
}

struct ScaleButtonStyle: ButtonStyle {
public init() {}

public func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.scaleEffect(configuration.isPressed ? Constants.Scale.pressed : 1)
// .brightness(configuration.isPressed ? Constants.Scale.brightness : 0)
.animation(.easeOut, value: configuration.isPressed)
}
}

extension ButtonStyle where Self == ScaleButtonStyle {
static var scale: ScaleButtonStyle {
ScaleButtonStyle()
}
}

extension Color {
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
}
}

extension View {
func plainTransition() -> some View {
modifier(Plain())
}

func layerPadding() -> some View {
modifier(LayerPadding())
}
Expand Down

0 comments on commit cbdb0bc

Please sign in to comment.