Skip to content

Commit

Permalink
Created app groups for widget extension and app, Fetching favorite co…
Browse files Browse the repository at this point in the history
…ins from AppStorage added.
  • Loading branch information
alieren97 authored and bahaadesso committed Oct 4, 2023
1 parent 42e01f3 commit a0c33d3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 20 deletions.
10 changes: 10 additions & 0 deletions CoinWidgetExtension/CoinWidgetExtension.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.adesso.SampleAppSwiftUI</string>
</array>
</dict>
</plist>
77 changes: 58 additions & 19 deletions CoinWidgetExtension/CoinWidgetExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,57 @@ import WidgetKit
import SwiftUI

struct Provider: TimelineProvider {
@AppStorage("favoriteCoins", store: UserDefaults(suiteName: "group.com.adesso.SampleAppSwiftUI")) var favoriteCoins: [CoinData] = []

func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date())
SimpleEntry(favoriteCoins: favoriteCoins)
}

func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date())
let entry = SimpleEntry(favoriteCoins: favoriteCoins)
completion(entry)
}

func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []

// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate) ?? Date()
let entry = SimpleEntry(date: entryDate)
entries.append(entry)
}

let timeline = Timeline(entries: entries, policy: .atEnd)
let timeline = Timeline(entries: [SimpleEntry(favoriteCoins: favoriteCoins)], policy: .atEnd)
completion(timeline)
}
}

struct SimpleEntry: TimelineEntry {
let date: Date
let date = Date()
var favoriteCoins: [CoinData]
}

struct CoinWidgetExtensionEntryView : View {
struct CoinWidgetExtensionEntryView: View {
@Environment(\.widgetFamily) var widgetFamily
var entry: Provider.Entry

@ViewBuilder
var emptyView: some View {
Text("There are no favorite coins")
}

@ViewBuilder
var coinsView: some View {
VStack {
ForEach(entry.favoriteCoins) { val in
HStack {
Text(val.coinInfo?.title ?? "")
}
}
}
}

var body: some View {
Text(entry.date, style: .time)
switch widgetFamily {
case .systemSmall:
entry.favoriteCoins.isEmpty ? AnyView(emptyView) : AnyView(coinsView)
case .systemMedium:
entry.favoriteCoins.isEmpty ? AnyView(emptyView) : AnyView(coinsView)
default:
Text(entry.date, style: .time)
}
}
}

Expand All @@ -55,13 +72,35 @@ struct CoinWidgetExtension: Widget {
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
.supportedFamilies([.systemSmall, .systemMedium])
}
}

struct CoinWidgetExtension_Previews: PreviewProvider {
static var previews: some View {
VStack {
CoinWidgetExtensionEntryView(entry: SimpleEntry(date: Date()))
}.previewContext(WidgetPreviewContext(family: .systemSmall))

CoinWidgetExtensionEntryView(entry: SimpleEntry(favoriteCoins: [])).previewContext(WidgetPreviewContext(family: .systemSmall))
CoinWidgetExtensionEntryView(entry: SimpleEntry(favoriteCoins: [])).previewContext(WidgetPreviewContext(family: .systemMedium))

}
}

extension Array: RawRepresentable where Element: Codable {
public init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8),
let result = try? JSONDecoder().decode([Element].self, from: data)
else {
return nil
}
self = result
}

public var rawValue: String {
guard let data = try? JSONEncoder().encode(self),
let result = String(data: data, encoding: .utf8)
else {
return "[]"
}
return result
}
}
10 changes: 10 additions & 0 deletions SampleAppSwiftUI/SampleAppSwiftUI.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.adesso.SampleAppSwiftUI</string>
</array>
</dict>
</plist>
4 changes: 3 additions & 1 deletion SampleAppSwiftUI/Utility/Managers/StorageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
//

import SwiftUI
import WidgetKit

final class StorageManager: ObservableObject {

static let shared = StorageManager()

@AppStorage("favoriteCoins") var favoriteCoins: [CoinData] = [] {
@AppStorage("favoriteCoins", store: UserDefaults(suiteName: "group.com.adesso.SampleAppSwiftUI")) var favoriteCoins: [CoinData] = [] {
didSet {
objectWillChange.send()
WidgetCenter.shared.reloadAllTimelines()
}
}

Expand Down

0 comments on commit a0c33d3

Please sign in to comment.