Skip to content

Commit 2d2bf89

Browse files
committed
fix UserDefauts by group
1 parent 72cba38 commit 2d2bf89

File tree

7 files changed

+80
-19
lines changed

7 files changed

+80
-19
lines changed

TrackerWidget/TrackerWidget.swift

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ struct BalanceWidgetEntry: TimelineEntry {
1515

1616
struct BalanceWidgetProvider: TimelineProvider {
1717
typealias Entry = BalanceWidgetEntry
18-
private var viewModel = BalanceViewModel()
19-
18+
2019

2120
func placeholder(in context: Context) -> BalanceWidgetEntry {
2221
BalanceWidgetEntry(date: Date(), balance: 10000.0)
@@ -25,7 +24,7 @@ struct BalanceWidgetProvider: TimelineProvider {
2524
func getSnapshot(in context: Context, completion: @escaping (BalanceWidgetEntry) -> Void) {
2625
let currentDate = Date()
2726

28-
viewModel.fetchBalance{ totalUsd in
27+
fetchBalance{ totalUsd in
2928
let entry = BalanceWidgetEntry(date: currentDate, balance: totalUsd)
3029

3130
completion(entry)
@@ -38,15 +37,43 @@ struct BalanceWidgetProvider: TimelineProvider {
3837
let currentDate = Date()
3938
let refreshDate = Calendar.current.date(byAdding: .hour, value: 1, to: currentDate)!
4039

41-
viewModel.fetchBalance{ totalUsd in
42-
43-
let entry = BalanceWidgetEntry(date: currentDate, balance: viewModel.balance)
44-
40+
fetchBalance{ totalUsd in
41+
let entry = BalanceWidgetEntry(date: currentDate, balance: totalUsd)
4542
let timeline = Timeline(entries: [entry], policy: .after(refreshDate))
4643
completion(timeline)
4744
}
4845

4946
}
47+
48+
49+
private func fetchBalance(completion: @escaping (Double) -> Void) {
50+
let wallet = UserDefaults(suiteName:"group.org.onout")?.string(forKey: "wallet") ?? "0x873351e707257C28eC6fAB1ADbc850480f6e0633"
51+
print("fetch balance")
52+
print(wallet)
53+
54+
55+
guard let url = URL(string: "https://dashapi.onout.org/debank?address=\(wallet)&app=itracker") else {
56+
return
57+
}
58+
59+
URLSession.shared.dataTask(with: url) { data, response, error in
60+
guard let data = data else {
61+
return
62+
}
63+
64+
do {
65+
let json = try JSONSerialization.jsonObject(with: data, options: [])
66+
if let dict = json as? [String: Any], let totalUsd = dict["total_usd"] as? Double {
67+
DispatchQueue.main.async {
68+
UserDefaults.standard.set(totalUsd, forKey: "balance")
69+
completion(totalUsd)
70+
}
71+
}
72+
} catch {
73+
print(error.localizedDescription)
74+
}
75+
}.resume()
76+
}
5077
}
5178

5279
struct BalanceWidgetView: View {
@@ -74,7 +101,7 @@ struct BalanceWidgetView: View {
74101

75102
}
76103

77-
func formatInt(number: Int) -> String{
104+
private func formatInt(number: Int) -> String{
78105
let formatter = NumberFormatter()
79106
formatter.numberStyle = .decimal
80107
formatter.maximumFractionDigits = 1
@@ -91,6 +118,8 @@ struct BalanceWidgetView: View {
91118
let result = "\(formattedNumber)\(suffix)"
92119
return result
93120
}
121+
122+
94123
}
95124

96125
@main

TrackerWidgetExtension.entitlements

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>com.apple.security.application-groups</key>
6+
<array>
7+
<string>group.org.onout</string>
8+
</array>
9+
</dict>
10+
</plist>

apple-watch-portfolio-tracker Watch App/BalanceViewModel.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
//
77

88
import Foundation
9+
import WidgetKit
10+
11+
912

1013
class BalanceViewModel: ObservableObject {
1114
@Published var balance = 0.0
@@ -17,20 +20,21 @@ class BalanceViewModel: ObservableObject {
1720

1821

1922
init() {
20-
self.wallet = UserDefaults.standard.string(forKey: "wallet") ?? "0x873351e707257C28eC6fAB1ADbc850480f6e0633"
21-
self.balance = UserDefaults.standard.double(forKey: "balance")
23+
self.wallet = UserDefaults(suiteName:"group.org.onout")?.string(forKey: "wallet") ?? "0x873351e707257C28eC6fAB1ADbc850480f6e0633"
24+
self.balance = UserDefaults(suiteName:"group.org.onout")?.double(forKey: "balance") ?? Double(0)
2225
fetchBalance()
2326
}
2427

2528
func updateWallet(wallet: String) {
26-
UserDefaults.standard.set(wallet, forKey: "wallet")
29+
UserDefaults(suiteName:"group.org.onout")?.set(wallet, forKey: "wallet")
2730
self.wallet = wallet
2831
fetchBalance()
32+
WidgetCenter.shared.reloadAllTimelines()
2933
}
3034

3135

3236
func fetchBalance() {
33-
self.wallet = UserDefaults.standard.string(forKey: "wallet") ?? "0x873351e707257C28eC6fAB1ADbc850480f6e0633"
37+
self.wallet = UserDefaults(suiteName:"group.org.onout")?.string(forKey: "wallet") ?? "0x873351e707257C28eC6fAB1ADbc850480f6e0633"
3438

3539
guard let url = URL(string: "https://dashapi.onout.org/debank?address=\(self.wallet)&app=itracker") else {
3640
return
@@ -46,7 +50,7 @@ class BalanceViewModel: ObservableObject {
4650
if let dict = json as? [String: Any], let totalUsd = dict["total_usd"] as? Double {
4751
DispatchQueue.main.async {
4852
self.balance = totalUsd
49-
UserDefaults.standard.set(totalUsd, forKey: "balance")
53+
UserDefaults(suiteName:"group.org.onout")?.set(totalUsd, forKey: "balance")
5054

5155

5256
}
@@ -58,8 +62,8 @@ class BalanceViewModel: ObservableObject {
5862
}
5963

6064
func fetchBalance(completion: @escaping (Double) -> Void) {
61-
62-
self.wallet = UserDefaults.standard.string(forKey: "wallet") ?? "0x873351e707257C28eC6fAB1ADbc850480f6e0633"
65+
self.wallet = UserDefaults(suiteName:"group.org.onout")?.string(forKey: "wallet")
66+
?? "0x873351e707257C28eC6fAB1ADbc850480f6e0633"
6367

6468

6569
guard let url = URL(string: "https://dashapi.onout.org/debank?address=\(self.wallet)&app=itracker") else {
@@ -76,7 +80,7 @@ class BalanceViewModel: ObservableObject {
7680
if let dict = json as? [String: Any], let totalUsd = dict["total_usd"] as? Double {
7781
DispatchQueue.main.async {
7882
self.balance = totalUsd
79-
UserDefaults.standard.set(totalUsd, forKey: "balance")
83+
UserDefaults(suiteName:"group.org.onout")?.set(totalUsd, forKey: "balance")
8084
completion(totalUsd)
8185
}
8286
}
@@ -111,7 +115,7 @@ class BalanceViewModel: ObservableObject {
111115
do {
112116
let decoder = JSONDecoder()
113117
let wallet = try decoder.decode(Wallet.self, from: data)
114-
UserDefaults.standard.set(wallet.address, forKey: "wallet")
118+
UserDefaults(suiteName:"group.org.onout")?.set(wallet.address, forKey: "wallet")
115119
self.wallet = wallet.address
116120
completion(wallet)
117121
} catch {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>com.apple.security.application-groups</key>
6+
<array>
7+
<string>group.org.onout</string>
8+
</array>
9+
</dict>
10+
</plist>

apple-watch-portfolio-tracker.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
A8488A632AE82FD600ECDFA1 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
8585
A8488A652AE82FD600ECDFA1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8686
A8488A6F2AE832D900ECDFA1 /* WalletInfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletInfoView.swift; sourceTree = "<group>"; };
87+
A89B03CF2AEED35E00F4E824 /* TrackerWidgetExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = TrackerWidgetExtension.entitlements; sourceTree = "<group>"; };
88+
A89B03D02AEED38000F4E824 /* apple-watch-portfolio-tracker Watch App.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "apple-watch-portfolio-tracker Watch App.entitlements"; sourceTree = "<group>"; };
8789
A8B88CD72AE859BA00252351 /* WalletModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletModel.swift; sourceTree = "<group>"; };
8890
/* End PBXFileReference section */
8991

@@ -111,6 +113,7 @@
111113
A8488A2C2AE82EC800ECDFA1 = {
112114
isa = PBXGroup;
113115
children = (
116+
A89B03CF2AEED35E00F4E824 /* TrackerWidgetExtension.entitlements */,
114117
A8488A3D2AE82EC800ECDFA1 /* apple-watch-portfolio-tracker Watch App */,
115118
A8488A5E2AE82FD500ECDFA1 /* TrackerWidget */,
116119
A8488A592AE82FD500ECDFA1 /* Frameworks */,
@@ -131,6 +134,7 @@
131134
A8488A3D2AE82EC800ECDFA1 /* apple-watch-portfolio-tracker Watch App */ = {
132135
isa = PBXGroup;
133136
children = (
137+
A89B03D02AEED38000F4E824 /* apple-watch-portfolio-tracker Watch App.entitlements */,
134138
A8488A3E2AE82EC800ECDFA1 /* apple_watch_portfolio_trackerApp.swift */,
135139
A8488A402AE82EC800ECDFA1 /* ContentView.swift */,
136140
A8488A6F2AE832D900ECDFA1 /* WalletInfoView.swift */,
@@ -460,6 +464,7 @@
460464
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
461465
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
462466
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
467+
CODE_SIGN_ENTITLEMENTS = "apple-watch-portfolio-tracker Watch App/apple-watch-portfolio-tracker Watch App.entitlements";
463468
CODE_SIGN_STYLE = Automatic;
464469
CURRENT_PROJECT_VERSION = 3;
465470
DEVELOPMENT_ASSET_PATHS = "\"apple-watch-portfolio-tracker Watch App/Preview Content\"";
@@ -490,6 +495,7 @@
490495
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
491496
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
492497
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
498+
CODE_SIGN_ENTITLEMENTS = "apple-watch-portfolio-tracker Watch App/apple-watch-portfolio-tracker Watch App.entitlements";
493499
CODE_SIGN_STYLE = Automatic;
494500
CURRENT_PROJECT_VERSION = 3;
495501
DEVELOPMENT_ASSET_PATHS = "\"apple-watch-portfolio-tracker Watch App/Preview Content\"";
@@ -553,6 +559,7 @@
553559
buildSettings = {
554560
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
555561
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
562+
CODE_SIGN_ENTITLEMENTS = TrackerWidgetExtension.entitlements;
556563
CODE_SIGN_STYLE = Automatic;
557564
CURRENT_PROJECT_VERSION = 1;
558565
DEVELOPMENT_TEAM = 36TMC58V7J;
@@ -583,6 +590,7 @@
583590
buildSettings = {
584591
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
585592
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
593+
CODE_SIGN_ENTITLEMENTS = TrackerWidgetExtension.entitlements;
586594
CODE_SIGN_STYLE = Automatic;
587595
CURRENT_PROJECT_VERSION = 1;
588596
DEVELOPMENT_TEAM = 36TMC58V7J;

apple-watch-portfolio-tracker.xcodeproj/xcuserdata/nikitaivanov.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<key>TrackerWidgetExtension.xcscheme_^#shared#^_</key>
7171
<dict>
7272
<key>orderHint</key>
73-
<integer>1</integer>
73+
<integer>0</integer>
7474
</dict>
7575
<key>User Guide (Playground) 1.xcscheme</key>
7676
<dict>
@@ -96,7 +96,7 @@
9696
<key>apple-watch-portfolio-tracker Watch App.xcscheme_^#shared#^_</key>
9797
<dict>
9898
<key>orderHint</key>
99-
<integer>0</integer>
99+
<integer>1</integer>
100100
</dict>
101101
</dict>
102102
</dict>

0 commit comments

Comments
 (0)