Skip to content

Commit

Permalink
Merge pull request #374 from uhooi/feature/refactor_widgets
Browse files Browse the repository at this point in the history
Refactor widgets
  • Loading branch information
uhooi authored Oct 1, 2023
2 parents a707755 + d0a27c9 commit 13bea29
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
</EnvironmentVariable>
<EnvironmentVariable
key = "_XCWidgetFamily"
value = "small"
value = "systemSmall"
isEnabled = "YES">
</EnvironmentVariable>
<EnvironmentVariable
Expand Down
14 changes: 9 additions & 5 deletions App/UhooiPicBookWidgets/Monster/MonsterConfigurableWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import MonsterWidgets
import MonstersRepository
import ImageLoader

private struct MonsterProvider {
typealias Entry = MonsterEntry
typealias Intent = SelectMonsterIntent
}
// MARK: MonsterConfigurableWidget

struct MonsterConfigurableWidget: Widget {
var body: some WidgetConfiguration {
Expand All @@ -31,6 +28,13 @@ struct MonsterConfigurableWidget: Widget {
}
}

// MARK: - MonsterProvider

private struct MonsterProvider {
typealias Entry = MonsterEntry
typealias Intent = SelectMonsterIntent
}

extension MonsterProvider: IntentTimelineProvider {
func placeholder(in context: Context) -> Entry {
.placeholder()
Expand All @@ -56,6 +60,6 @@ extension MonsterProvider: IntentTimelineProvider {
return nil
}
let description = dto.description.replacingOccurrences(of: "\\n", with: "\n")
return Entry(date: Date(), name: dto.name, description: description, icon: icon)
return Entry(date: .now, name: dto.name, description: description, icon: icon)
}
}
2 changes: 1 addition & 1 deletion Sources/MonsterWidgets/MonsterEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct MonsterEntry: TimelineEntry {

public static func placeholder() -> Self {
.init(
date: Date(),
date: .now,
name: "uhooi",
description: "ゆかいな みどりの せいぶつ。\nわるそうに みえるが むがい。",
icon: UIImage(resource: .uhooi)
Expand Down
62 changes: 30 additions & 32 deletions Sources/MonsterWidgets/MonsterEntryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,14 @@ public struct MonsterEntryView: View {
public var body: some View {
switch family {
case .systemSmall:
ZStack {
Color(.systemBackground)
VStack {
icon
Spacer(minLength: 8.0)
name
}
iconAndNameView
.padding()
}
case .systemMedium:
ZStack {
Color(.systemBackground)
HStack {
VStack {
icon
Spacer(minLength: 8.0)
name
}
Spacer(minLength: 16.0)
description
}
.padding()
HStack(spacing: 16) {
iconAndNameView
descriptionText
}
.padding()
case .systemLarge, .systemExtraLarge:
EmptyView()
case .accessoryCircular, .accessoryRectangular, .accessoryInline:
Expand All @@ -48,35 +33,48 @@ public struct MonsterEntryView: View {
}
}

private var icon: some View {
public init(entry: MonsterEntry) {
self.entry = entry
}
}

// MARK: - Privates

private extension MonsterEntryView {
var iconAndNameView: some View {
VStack(spacing: 8) {
iconImage
nameText
}
}

var iconImage: some View {
Image(uiImage: entry.icon)
.resizable()
.aspectRatio(contentMode: .fit)
.scaledToFit()
.accessibilityLabel(Text(entry.name))
}

private var name: some View {
var nameText: some View {
Text(entry.name)
.font(.headline)
}

private var description: some View {
var descriptionText: some View {
Text(entry.description)
.font(.body)
}

public init(entry: MonsterEntry) {
self.entry = entry
}
}

// MARK: - Previews

struct MonsterEntryView_Previews: PreviewProvider {
typealias Entry = MonsterEntry

static var previews: some View {
ForEach(0..<families.count, id: \.self) { index in
ForEach(families, id: \.self) { family in
previewEntryViewGroup
.previewContext(WidgetPreviewContext(family: families[index]))
.previewContext(WidgetPreviewContext(family: family))
}
}

Expand All @@ -96,7 +94,7 @@ struct MonsterEntryView_Previews: PreviewProvider {

private static func shortEntry() -> Entry {
.init(
date: Date(),
date: .now,
name: "1",
description: "1",
icon: UIImage(resource: .uhooi)
Expand All @@ -105,7 +103,7 @@ struct MonsterEntryView_Previews: PreviewProvider {

private static func longEntry() -> Entry {
.init(
date: Date(),
date: .now,
name: "123456789012345678901234567890",
description: "12345678901234567890\n12345678901234567890\n12345678901234567890",
icon: UIImage(resource: .uhooi)
Expand Down
2 changes: 1 addition & 1 deletion Sources/MonsterWidgets/MonsterWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extension MonsterProvider: TimelineProvider {
var entries: [Entry] = []
do {
let monsters = try await monstersRepository.monsters()
let currentDate = Date()
let currentDate = Date.now
var hourOffset = 0
for monster in monsters.sorted(by: { $0.order < $1.order }) {
guard let iconURL = URL(string: monster.iconURLString),
Expand Down

0 comments on commit 13bea29

Please sign in to comment.