Skip to content

Commit 393150b

Browse files
author
zcc
committed
no message
1 parent bc7df5f commit 393150b

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

WeatherSwiftUIDemo/WeatherSwiftUIDemo/Macro/Macro.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ let kNavBarHeight: CGFloat = 44.0 + (kWindow?.safeAreaInsets.top ?? 20.0)
1616

1717
let kTopSafeH: CGFloat = 20.0
1818

19+
let kImageFilePath = NSHomeDirectory() + "/Documents/images"

WeatherSwiftUIDemo/WeatherSwiftUIDemo/Sections/Main/View/WeatherMainView.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ struct WeatherMainView: View {
2020
Text(self._vm.model?.now?.address ?? "").font(Font.system(size: 20.0)).fontWeight(.semibold)
2121
Button(action: {
2222
print("aaa")
23+
2324
}) {
2425
Image(systemName: "plus.circle.fill")
2526
}
26-
}.frame(height: kNavBarHeight).offset(x: 0.0, y: 10.0)
27+
}.frame(height: kNavBarHeight).offset(y: 10.0)
2728

2829
ScrollView {
2930
VStack(alignment: .leading) {
@@ -44,16 +45,7 @@ struct WeatherMainView: View {
4445
}
4546
}
4647

47-
// Button(action: {
48-
// self._isLoading.toggle()
49-
// if self._isLoading {
50-
// self._vm.loadData()
51-
// }
52-
// }) {
53-
// Text(self._isLoading ? "加载" : "刷新")
54-
// }.frame(width: 60.0, height: 60.0, alignment: .center).background(Color.black.opacity(0.6)).cornerRadius(30.0)
55-
56-
}.foregroundColor(Color.white).background(WebImage(self._vm.model?.now?.skin, configuration: { $0.resizable() }).scaledToFill().edgesIgnoringSafeArea(.all)).edgesIgnoringSafeArea(.all).statusBar(hidden: false).onAppear {
48+
}.foregroundColor(Color.black).background(WebImage(self._vm.model?.now?.skin, configuration: { $0.resizable() }).scaledToFill().edgesIgnoringSafeArea(.all)).edgesIgnoringSafeArea(.all).statusBar(hidden: false).onAppear {
5749

5850
self._vm.loadData()
5951
NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: OperationQueue.current) { (_) in
@@ -96,10 +88,12 @@ struct AqiView: View {
9688
Circle().fill(Color(r: 76, g: 60, b: 134))
9789
}
9890
}.frame(width: 30.0, height: 30.0)
99-
WebImage(self.vm.model?.now?.aqiIcon).frame(width: 14.0, height: 14.0).scaledToFit()
91+
WebImage(self.vm.model?.now?.aqiIcon) { (img) -> Image in
92+
return img.resizable()
93+
}.frame(width: 15.0, height: 15.0)
10094
}
101-
Text("\(self.vm.model?.now?.aqi ?? 0)").foregroundColor(Color.white).fontWeight(.semibold)
102-
Text(self.vm.model?.now?.aqiDesc ?? "").foregroundColor(Color.white).fontWeight(.semibold)
95+
Text("\(self.vm.model?.now?.aqi ?? 0)").fontWeight(.semibold)
96+
Text(self.vm.model?.now?.aqiDesc ?? "").fontWeight(.semibold)
10397
}.padding(.init(top: 0.0, leading: 8.0, bottom: 0.0, trailing: 16.0)).frame(height: 42.0).background(Color.black.opacity(0.3)).cornerRadius(26.0)
10498
}
10599
}
@@ -115,7 +109,7 @@ struct TipsView: View {
115109
}.background(Color.black.opacity(0.3)).cornerRadius(15.0)
116110

117111
Text(self.vm.model?.now?.tips ?? "").fontWeight(.semibold)
118-
}.foregroundColor(Color.white).font(Font.system(size: 16.0))
112+
}.font(Font.system(size: 16.0))
119113
}
120114
}
121115

@@ -168,7 +162,7 @@ struct FutureView: View {
168162
// MARK: -文字内容样式
169163
struct ContentTextModifier: ViewModifier {
170164
func body(content: Content) -> some View {
171-
content.font(Font.system(size: 16.0)).foregroundColor(Color.white)
165+
content.font(Font.system(size: 16.0))
172166
}
173167
}
174168

WeatherSwiftUIDemo/WeatherSwiftUIDemo/Sections/Main/ViewModel/WeatherViewModel.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ class WeatherViewModel: ObservableObject {
1414
@Published var model: WeatherModel?
1515

1616
func loadData() {
17-
18-
APIManager.start(.weatherThreeDays, modelT: WeatherModel.self) { [weak self] (model) in
19-
if model != self?.model {
20-
DispatchQueue.main.async {
21-
self?.model = model
22-
}
23-
}
24-
}
17+
let filePath = Bundle.main.path(forResource: "test", ofType: "json")!
18+
let d = try! Data(contentsOf: URL(fileURLWithPath: filePath))
19+
let model = try? JSONDecoder().decode(BaseModel<WeatherModel>.self, from: d)
20+
self.model = model?.data
21+
// APIManager.start(.weatherThreeDays, modelT: WeatherModel.self) { [weak self] (model) in
22+
// if model != self?.model {
23+
// DispatchQueue.main.async {
24+
// self?.model = model
25+
// }
26+
// }
27+
// }
2528
}
2629
}

WeatherSwiftUIDemo/WeatherSwiftUIDemo/Tools/WebImage.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct WebImage: View {
1515
/// 展位
1616
private var _placeholder: Text?
1717
private let _configuration: (Image) -> Image
18-
18+
1919
init(_ imgPath: String?, placeholder: Text? = Text("加载中..."), configuration: @escaping (Image) -> Image = { $0 }) {
2020
self._placeholder = placeholder
2121
self._configuration = configuration
@@ -62,8 +62,21 @@ class ImageLoader: ObservableObject {
6262
if self._isLoading { return }
6363
guard let path = self.loadPath, let url = URL(string: path) else { return }
6464
self._isLoading = true
65+
let filePath = kImageFilePath + (path.components(separatedBy: "/").last ?? "1.png")
66+
let file = FileManager.default
67+
if file.isReadableFile(atPath: filePath) {
68+
if let img = UIImage(contentsOfFile: filePath) {
69+
self._isLoading = false
70+
DispatchQueue.main.async {
71+
self.image = img
72+
}
73+
return
74+
}
75+
}
6576
self._task = URLSession.shared.dataTask(with: url) { (data, _, error) in
6677
if let d = data, let img = UIImage(data: d) {
78+
// 缓存到本地
79+
NSData(data: d).write(toFile: filePath, atomically: true)
6780
DispatchQueue.main.async {
6881
self.image = img
6982
}

0 commit comments

Comments
 (0)