Source code for the WriteFreely SwiftUI app for iOS, iPadOS, and macOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

64 line
1.7 KiB

  1. import SwiftUI
  2. enum PostStatus {
  3. case draft
  4. case edited
  5. case published
  6. }
  7. struct PostStatusBadge: View {
  8. @State var postStatus: PostStatus
  9. var body: some View {
  10. let (badgeLabel, badgeColor) = setupBadgeProperties(for: postStatus)
  11. Text(badgeLabel)
  12. .font(.caption)
  13. .fontWeight(.bold)
  14. .foregroundColor(.white)
  15. .textCase(.uppercase)
  16. .lineLimit(1)
  17. .padding(EdgeInsets(top: 2.5, leading: 7.5, bottom: 2.5, trailing: 7.5))
  18. .background(badgeColor)
  19. .clipShape(RoundedRectangle(cornerRadius: 5.0, style: .circular))
  20. }
  21. func setupBadgeProperties(for status: PostStatus) -> (String, Color) {
  22. var badgeLabel: String
  23. var badgeColor: Color
  24. switch status {
  25. case .draft:
  26. badgeLabel = "draft"
  27. badgeColor = Color(red: 0.75, green: 0.5, blue: 0.85, opacity: 1.0)
  28. case .edited:
  29. badgeLabel = "edited"
  30. badgeColor = Color(red: 0.75, green: 0.7, blue: 0.1, opacity: 1.0)
  31. case .published:
  32. badgeLabel = "published"
  33. badgeColor = .gray
  34. }
  35. return (badgeLabel, badgeColor)
  36. }
  37. }
  38. struct PostStatusBadge_DraftPreviews: PreviewProvider {
  39. static var previews: some View {
  40. PostStatusBadge(postStatus: .draft)
  41. }
  42. }
  43. struct PostStatusBadge_EditedPreviews: PreviewProvider {
  44. static var previews: some View {
  45. Group {
  46. PostStatusBadge(postStatus: .edited)
  47. }
  48. }
  49. }
  50. struct PostStatusBadge_PublishedPreviews: PreviewProvider {
  51. static var previews: some View {
  52. PostStatusBadge(postStatus: .published)
  53. }
  54. }