-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add button types and localize components
- Loading branch information
1 parent
377da61
commit 88c910c
Showing
27 changed files
with
733 additions
and
114 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// Button+Init.swift | ||
// SwiftUIKit | ||
// | ||
// Created by Daniel Saidi on 2024-04-30. | ||
// Copyright © 2024 Daniel Saidi. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public extension Button where Label == SwiftUI.Label<Text, Image> { | ||
|
||
/// This initializer lets you use buttons with less code. | ||
init( | ||
_ text: LocalizedStringKey, | ||
_ icon: Image, | ||
_ bundle: Bundle = .main, | ||
action: @escaping () -> Void | ||
) { | ||
self.init(action: action) { | ||
Label( | ||
title: { Text(text, bundle: bundle) }, | ||
icon: { icon } | ||
) | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
|
||
Button("Preview.Button", .symbol("checkmark"), .module) { | ||
print("Tapped!") | ||
} | ||
} |
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,126 @@ | ||
// | ||
// Button+Standard.swift | ||
// SwiftUIKit | ||
// | ||
// Created by Daniel Saidi on 2024-04-30. | ||
// Copyright © 2024 Daniel Saidi. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public extension Button { | ||
|
||
/// This is a standard add button. | ||
init( | ||
_ type: StandardType, | ||
_ title: LocalizedStringKey? = nil, | ||
_ icon: Image? = nil, | ||
bundle: Bundle? = nil, | ||
action: @escaping () -> Void | ||
) where Label == SwiftUI.Label<Text, Image?> { | ||
self.init(role: type.role, action: action) { | ||
Label( | ||
title: { Text(title ?? type.title, bundle: title == nil ? .module : bundle) }, | ||
icon: { icon ?? type.image } | ||
) | ||
} | ||
} | ||
|
||
/// This enum defines standard button types and provides | ||
/// standard localized texts and icons. | ||
enum StandardType: String, CaseIterable, Identifiable { | ||
case add, addFavorite, addToFavorites, | ||
cancel, call, copy, | ||
delete, deselect, done, | ||
edit, email, | ||
ok, | ||
paste, | ||
removeFavorite, removeFromFavorites, | ||
select, share | ||
} | ||
} | ||
|
||
public extension Button.StandardType { | ||
|
||
var id: String{ rawValue } | ||
|
||
var image: Image? { | ||
guard let imageName else { return nil } | ||
return .symbol(imageName) | ||
} | ||
|
||
var imageName: String? { | ||
switch self { | ||
case .add: "plus" | ||
case .addFavorite: "star.circle" | ||
case .addToFavorites: "star.circle" | ||
case .cancel: "xmark" | ||
case .call: "phone" | ||
case .copy: "doc.on.doc" | ||
case .delete: "trash" | ||
case .deselect: "checkmark.circle.fill" | ||
case .done: "checkmark" | ||
case .edit: "pencil" | ||
case .email: "envelope" | ||
case .ok: "checkmark" | ||
case .paste: "clipboard" | ||
case .removeFavorite: "star.circle.fill" | ||
case .removeFromFavorites: "star.circle.fill" | ||
case .select: "checkmark.circle" | ||
case .share: "square.and.arrow.up" | ||
} | ||
} | ||
|
||
var role: ButtonRole? { | ||
switch self { | ||
case .cancel: .cancel | ||
case .delete: .destructive | ||
default: nil | ||
} | ||
} | ||
|
||
var title: LocalizedStringKey { | ||
switch self { | ||
case .add: "Button.Add" | ||
case .addFavorite: "Button.AddFavorite" | ||
case .addToFavorites: "Button.AddToFavorites" | ||
case .call: "Button.Call" | ||
case .cancel: "Button.Cancel" | ||
case .copy: "Button.Copy" | ||
case .deselect: "Button.Deselect" | ||
case .edit: "Button.Edit" | ||
case .email: "Button.Email" | ||
case .delete: "Button.Delete" | ||
case .done: "Button.Done" | ||
case .ok: "Button.OK" | ||
case .paste: "Button.Paste" | ||
case .removeFavorite: "Button.RemoveFavorite" | ||
case .removeFromFavorites: "Button.RemoveFromFavorites" | ||
case .select: "Button.Select" | ||
case .share: "Button.Share" | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
|
||
@ViewBuilder | ||
func buttons() -> some View { | ||
Section { | ||
ForEach(Button.StandardType.allCases) { | ||
Button($0) {} | ||
} | ||
} | ||
} | ||
|
||
return List { | ||
buttons() | ||
buttons().labelStyle(.titleOnly) | ||
buttons().labelStyle(.iconOnly) | ||
} | ||
.toolbar { | ||
ToolbarItemGroup { | ||
buttons() | ||
} | ||
} | ||
} |
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
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.