From 0d301c7f721881704711c15bde11747380cb1cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin?= Date: Sun, 2 Jun 2024 05:43:38 +0200 Subject: [PATCH] Clear button has been added to INCommons package --- INCommons.xcodeproj/project.pbxproj | 8 +++ .../SwiftUI/ClearButtonModifier.swift | 50 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 Sources/INCommons/SwiftUI/ClearButtonModifier.swift diff --git a/INCommons.xcodeproj/project.pbxproj b/INCommons.xcodeproj/project.pbxproj index b3999c5..f0957bf 100644 --- a/INCommons.xcodeproj/project.pbxproj +++ b/INCommons.xcodeproj/project.pbxproj @@ -97,6 +97,8 @@ 26FCE7A227D276BF00C229DA /* ExampleViewFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26FCE79A27D276BF00C229DA /* ExampleViewFactory.swift */; }; 26FCE7A327D276BF00C229DA /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26FCE79B27D276BF00C229DA /* HomeView.swift */; }; 26FCE7A727D2783000C229DA /* Config.plist in Resources */ = {isa = PBXBuildFile; fileRef = 26FCE7A627D2783000C229DA /* Config.plist */; }; + 53C33F662C0C209C0086F634 /* ClearButtonModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53C33F652C0C209C0086F634 /* ClearButtonModifier.swift */; }; + 53C33F682C0C21700086F634 /* OverlayButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53C33F672C0C21700086F634 /* OverlayButton.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -216,6 +218,8 @@ 26FCE79A27D276BF00C229DA /* ExampleViewFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExampleViewFactory.swift; sourceTree = ""; }; 26FCE79B27D276BF00C229DA /* HomeView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = ""; }; 26FCE7A627D2783000C229DA /* Config.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Config.plist; sourceTree = ""; }; + 53C33F652C0C209C0086F634 /* ClearButtonModifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClearButtonModifier.swift; sourceTree = ""; }; + 53C33F672C0C21700086F634 /* OverlayButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OverlayButton.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -509,6 +513,8 @@ 26FCE75127D2698B00C229DA /* SwiftUI */ = { isa = PBXGroup; children = ( + 53C33F672C0C21700086F634 /* OverlayButton.swift */, + 53C33F652C0C209C0086F634 /* ClearButtonModifier.swift */, 26FCE75227D2698B00C229DA /* View+Conditions.swift */, 26B02EA12916324A00F06105 /* View+OnFirstAppear.swift */, 26FCE75327D2698B00C229DA /* View+Frame.swift */, @@ -912,6 +918,7 @@ 26B02EA22916324A00F06105 /* View+OnFirstAppear.swift in Sources */, 26FCE76927D2699E00C229DA /* With.swift in Sources */, 2627A1A929BFA5320006834F /* AnyEquatable.swift in Sources */, + 53C33F662C0C209C0086F634 /* ClearButtonModifier.swift in Sources */, 2627A1AE29BFA5320006834F /* Locale+TestableCurrent.swift in Sources */, 268B60CD27DCD6EE002D6C84 /* UIDeviceProvider.swift in Sources */, 26FCE76A27D2699E00C229DA /* Double+Time.swift in Sources */, @@ -936,6 +943,7 @@ 268B60D627DCF981002D6C84 /* Optional+Stringified.swift in Sources */, 26FCE75727D2698B00C229DA /* UIColor.swift in Sources */, 26FCE75B27D2698B00C229DA /* View+Conditions.swift in Sources */, + 53C33F682C0C21700086F634 /* OverlayButton.swift in Sources */, 2627A1AD29BFA5320006834F /* ProcessInfo+IsRunningInTestMode.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Sources/INCommons/SwiftUI/ClearButtonModifier.swift b/Sources/INCommons/SwiftUI/ClearButtonModifier.swift new file mode 100644 index 0000000..fff21e7 --- /dev/null +++ b/Sources/INCommons/SwiftUI/ClearButtonModifier.swift @@ -0,0 +1,50 @@ +import CoreGraphics +import Foundation +import SwiftUI + +public struct ClearButtonModifier: ViewModifier { + let tintColor: Color + let baseSize: CGSize + let tappableSize: CGSize + let image: Image + @Binding var text: String + + @ViewBuilder + public func body(content: Content) -> some View { + HStack { + content + if !text.isEmpty { + OverlayButton { + text = .empty + } baseShape: { + image + .frame(size: baseSize) + } decoration: { shape in + shape + .frame(size: tappableSize) + } + .foregroundStyle(tintColor) + } + } + } +} + +public extension View { + func clearButton( + text: Binding, + tintColor: Color, + baseSize: CGSize, + tappableSize: CGSize, + image: Image + ) -> some View { + modifier( + ClearButtonModifier( + tintColor: tintColor, + baseSize: baseSize, + tappableSize: tappableSize, + image: image, + text: text + ) + ) + } +}