Skip to content

Add ViewModifier and storyRowType to iOS code #345

@RyshchukDmytro

Description

@RyshchukDmytro

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)")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions