Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ViewModifier and storyRowType to iOS code #345

Open
RyshchukDmytro opened this issue Jan 30, 2025 · 1 comment
Open

Add ViewModifier and storyRowType to iOS code #345

RyshchukDmytro opened this issue Jan 30, 2025 · 1 comment

Comments

@RyshchukDmytro
Copy link

Hi guys, I wanted to add ViewModifier for Image and storyRowType function do reduce duplication in code, but found out that your repository is required access.

  1. So basically my idea was next:
import SwiftUI

struct ImageModifier: ViewModifier {
    let customFont: Font
    let color: Color

    func body(content: Content) -> some View {
        content
            .font(customFont)
            .foregroundColor(color)
    }
}

extension Image {
    func imageStyle(font: Font = .system(size: 12), color: Color = .primary) -> some View {
        self.modifier(ImageModifier(customFont: font, color: color))
    }
}

So you can use:

 Image(systemName: "arrow.up.right")
  .imageStyle()

 Image(systemName: "arrow.up.right")
  .imageStyle(color: .purple)

 Image(systemName: "arrow.up.right")
  .imageStyle(font: theme.userSansFont(size: 12), color: .green)

instead of

 Image(systemName: "arrow.up.right")
  .font(.system(size: 12))

 Image(systemName: "arrow.up.right")
  .font(.system(size: 12))
  .foregroundStyle(.purple)

 Image(systemName: "arrow.up.right")
  .font(theme.userSansFont(size: 12))
  .foregroundStyle(.green)
  1. To add next code to HackerNewsHomeWidgetEntryView
private enum StoryRowSize {
    case small, medium, basic
}

private func storyRowType(_ prefixAmount: Int, size: StoryRowSize) -> some View {
      ForEach(entry.stories.prefix(prefixAmount), id: \.id) { story in
          switch size {
          case .small: smallStoryRow(story)
          case .medium: mediumStoryRow(story)
          case .basic: storyRow(story)
          }
          if story.id != entry.stories.prefix(prefixAmount).last?.id {
              Divider()
          }
      }
  }

It can help you to eliminate duplication:

switch family {
      case .systemSmall:
        storyRowType(2, size: .small)

      case .systemMedium:
        storyRowType(2, size: .medium)

      case .systemLarge, .systemExtraLarge:
        storyRowType(5, size: .basic)

      case .accessoryCircular, .accessoryRectangular, .accessoryInline:
        fatalError("Unsupported family \(family)")

      @unknown default:
        fatalError("Unsupported family \(family)")
}
@trevor-e
Copy link
Collaborator

trevor-e commented Feb 3, 2025

Thanks for the suggestions! Some parts of the UI is still changing as we try to get this on the app store, so we will likely hold off on factoring out shared code for now until the dust settles, but these are good ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants