Skip to content

Commit e33db63

Browse files
author
Zhouyuankun
committed
Reconstruct code for simplicity and making use of more SwiftUI features
1 parent 39cdbf2 commit e33db63

File tree

13 files changed

+396
-533
lines changed

13 files changed

+396
-533
lines changed

ScreenZ/ScreenZ.xcodeproj/project.pbxproj

+25-36
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 55;
6+
objectVersion = 70;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -12,15 +12,13 @@
1212
B64DC25C28460B0D0063B8CF /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64DC25B28460B0D0063B8CF /* ContentView.swift */; };
1313
B64DC25E28460B0D0063B8CF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B64DC25D28460B0D0063B8CF /* Assets.xcassets */; };
1414
B64DC26128460B0D0063B8CF /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B64DC26028460B0D0063B8CF /* Preview Assets.xcassets */; };
15-
B64DC26328460B0D0063B8CF /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64DC26228460B0D0063B8CF /* Persistence.swift */; };
16-
B64DC26628460B0D0063B8CF /* ScreenZ.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = B64DC26428460B0D0063B8CF /* ScreenZ.xcdatamodeld */; };
1715
B64DC26F284625BA0063B8CF /* demo.png in Resources */ = {isa = PBXBuildFile; fileRef = B64DC26E284625BA0063B8CF /* demo.png */; };
18-
B64DC27428464B920063B8CF /* AddThemeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64DC27328464B920063B8CF /* AddThemeView.swift */; };
19-
B64DC27628464C820063B8CF /* utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64DC27528464C820063B8CF /* utils.swift */; };
2016
B64DC27728464FB80063B8CF /* demo.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = B64DC270284625BF0063B8CF /* demo.mp4 */; };
21-
B64DC2792846738E0063B8CF /* AllThemeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64DC2782846738E0063B8CF /* AllThemeView.swift */; };
22-
B64DC27B284673EA0063B8CF /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64DC27A284673EA0063B8CF /* SettingsView.swift */; };
2317
B64DC2AD284897AC0063B8CF /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = B64DC2AF284897AC0063B8CF /* Localizable.strings */; };
18+
B6D2437E2D14607200267EC4 /* UserDefaults+Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D2437D2D14607200267EC4 /* UserDefaults+Helper.swift */; };
19+
B6D243B52D1477C100267EC4 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64DC27A284673EA0063B8CF /* SettingsView.swift */; };
20+
B6D243B62D1477C600267EC4 /* AddThemeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64DC27328464B920063B8CF /* AddThemeView.swift */; };
21+
B6D243B72D1477CC00267EC4 /* AllThemeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64DC2782846738E0063B8CF /* AllThemeView.swift */; };
2422
/* End PBXBuildFile section */
2523

2624
/* Begin PBXFileReference section */
@@ -30,20 +28,22 @@
3028
B64DC25B28460B0D0063B8CF /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
3129
B64DC25D28460B0D0063B8CF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
3230
B64DC26028460B0D0063B8CF /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
33-
B64DC26228460B0D0063B8CF /* Persistence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = "<group>"; };
34-
B64DC26528460B0D0063B8CF /* ScreenZ.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = ScreenZ.xcdatamodel; sourceTree = "<group>"; };
3531
B64DC26728460B0D0063B8CF /* ScreenZ.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = ScreenZ.entitlements; sourceTree = "<group>"; };
3632
B64DC26D2846199B0063B8CF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
3733
B64DC26E284625BA0063B8CF /* demo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = demo.png; sourceTree = "<group>"; };
3834
B64DC270284625BF0063B8CF /* demo.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = demo.mp4; sourceTree = "<group>"; };
3935
B64DC27328464B920063B8CF /* AddThemeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddThemeView.swift; sourceTree = "<group>"; };
40-
B64DC27528464C820063B8CF /* utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = utils.swift; sourceTree = "<group>"; };
4136
B64DC2782846738E0063B8CF /* AllThemeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AllThemeView.swift; sourceTree = "<group>"; };
4237
B64DC27A284673EA0063B8CF /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
4338
B64DC2AE284897AC0063B8CF /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
4439
B64DC2B02848982A0063B8CF /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
40+
B6D2437D2D14607200267EC4 /* UserDefaults+Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UserDefaults+Helper.swift"; sourceTree = "<group>"; };
4541
/* End PBXFileReference section */
4642

43+
/* Begin PBXFileSystemSynchronizedRootGroup section */
44+
B6D2437F2D1462D600267EC4 /* Model */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = Model; sourceTree = "<group>"; };
45+
/* End PBXFileSystemSynchronizedRootGroup section */
46+
4747
/* Begin PBXFrameworksBuildPhase section */
4848
B64DC25328460B0D0063B8CF /* Frameworks */ = {
4949
isa = PBXFrameworksBuildPhase;
@@ -74,22 +74,21 @@
7474
B64DC25828460B0D0063B8CF /* ScreenZ */ = {
7575
isa = PBXGroup;
7676
children = (
77+
B6D2437F2D1462D600267EC4 /* Model */,
7778
B61CFB1D2BAED9B70031FFF3 /* black.jpg */,
7879
B64DC2AF284897AC0063B8CF /* Localizable.strings */,
7980
B64DC26D2846199B0063B8CF /* Info.plist */,
8081
B64DC25928460B0D0063B8CF /* ScreenZApp.swift */,
8182
B64DC25B28460B0D0063B8CF /* ContentView.swift */,
8283
B64DC25D28460B0D0063B8CF /* Assets.xcassets */,
83-
B64DC26228460B0D0063B8CF /* Persistence.swift */,
8484
B64DC26728460B0D0063B8CF /* ScreenZ.entitlements */,
8585
B64DC270284625BF0063B8CF /* demo.mp4 */,
8686
B64DC26E284625BA0063B8CF /* demo.png */,
87-
B64DC26428460B0D0063B8CF /* ScreenZ.xcdatamodeld */,
8887
B64DC25F28460B0D0063B8CF /* Preview Content */,
8988
B64DC27328464B920063B8CF /* AddThemeView.swift */,
90-
B64DC27528464C820063B8CF /* utils.swift */,
9189
B64DC2782846738E0063B8CF /* AllThemeView.swift */,
9290
B64DC27A284673EA0063B8CF /* SettingsView.swift */,
91+
B6D2437D2D14607200267EC4 /* UserDefaults+Helper.swift */,
9392
);
9493
path = ScreenZ;
9594
sourceTree = "<group>";
@@ -117,6 +116,9 @@
117116
);
118117
dependencies = (
119118
);
119+
fileSystemSynchronizedGroups = (
120+
B6D2437F2D1462D600267EC4 /* Model */,
121+
);
120122
name = ScreenZ;
121123
productName = ScreenZ;
122124
productReference = B64DC25628460B0D0063B8CF /* ScreenZ.app */;
@@ -177,13 +179,11 @@
177179
isa = PBXSourcesBuildPhase;
178180
buildActionMask = 2147483647;
179181
files = (
180-
B64DC27428464B920063B8CF /* AddThemeView.swift in Sources */,
181-
B64DC27B284673EA0063B8CF /* SettingsView.swift in Sources */,
182-
B64DC26328460B0D0063B8CF /* Persistence.swift in Sources */,
182+
B6D2437E2D14607200267EC4 /* UserDefaults+Helper.swift in Sources */,
183+
B6D243B52D1477C100267EC4 /* SettingsView.swift in Sources */,
184+
B6D243B72D1477CC00267EC4 /* AllThemeView.swift in Sources */,
183185
B64DC25C28460B0D0063B8CF /* ContentView.swift in Sources */,
184-
B64DC2792846738E0063B8CF /* AllThemeView.swift in Sources */,
185-
B64DC27628464C820063B8CF /* utils.swift in Sources */,
186-
B64DC26628460B0D0063B8CF /* ScreenZ.xcdatamodeld in Sources */,
186+
B6D243B62D1477C600267EC4 /* AddThemeView.swift in Sources */,
187187
B64DC25A28460B0D0063B8CF /* ScreenZApp.swift in Sources */,
188188
);
189189
runOnlyForDeploymentPostprocessing = 0;
@@ -326,7 +326,7 @@
326326
CODE_SIGN_ENTITLEMENTS = ScreenZ/ScreenZ.entitlements;
327327
CODE_SIGN_STYLE = Automatic;
328328
COMBINE_HIDPI_IMAGES = YES;
329-
CURRENT_PROJECT_VERSION = 1;
329+
CURRENT_PROJECT_VERSION = 20241229;
330330
DEVELOPMENT_ASSET_PATHS = "\"ScreenZ/Preview Content\"";
331331
DEVELOPMENT_TEAM = QZ389N5Q44;
332332
ENABLE_HARDENED_RUNTIME = YES;
@@ -338,7 +338,8 @@
338338
"$(inherited)",
339339
"@executable_path/../Frameworks",
340340
);
341-
MARKETING_VERSION = 1.0;
341+
MACOSX_DEPLOYMENT_TARGET = 15.0;
342+
MARKETING_VERSION = 1.5;
342343
PRODUCT_BUNDLE_IDENTIFIER = com.celeglow.ScreenZ;
343344
PRODUCT_NAME = "$(TARGET_NAME)";
344345
SWIFT_EMIT_LOC_STRINGS = YES;
@@ -354,7 +355,7 @@
354355
CODE_SIGN_ENTITLEMENTS = ScreenZ/ScreenZ.entitlements;
355356
CODE_SIGN_STYLE = Automatic;
356357
COMBINE_HIDPI_IMAGES = YES;
357-
CURRENT_PROJECT_VERSION = 1;
358+
CURRENT_PROJECT_VERSION = 20241229;
358359
DEVELOPMENT_ASSET_PATHS = "\"ScreenZ/Preview Content\"";
359360
DEVELOPMENT_TEAM = QZ389N5Q44;
360361
ENABLE_HARDENED_RUNTIME = YES;
@@ -366,7 +367,8 @@
366367
"$(inherited)",
367368
"@executable_path/../Frameworks",
368369
);
369-
MARKETING_VERSION = 1.0;
370+
MACOSX_DEPLOYMENT_TARGET = 15.0;
371+
MARKETING_VERSION = 1.5;
370372
PRODUCT_BUNDLE_IDENTIFIER = com.celeglow.ScreenZ;
371373
PRODUCT_NAME = "$(TARGET_NAME)";
372374
SWIFT_EMIT_LOC_STRINGS = YES;
@@ -396,19 +398,6 @@
396398
defaultConfigurationName = Release;
397399
};
398400
/* End XCConfigurationList section */
399-
400-
/* Begin XCVersionGroup section */
401-
B64DC26428460B0D0063B8CF /* ScreenZ.xcdatamodeld */ = {
402-
isa = XCVersionGroup;
403-
children = (
404-
B64DC26528460B0D0063B8CF /* ScreenZ.xcdatamodel */,
405-
);
406-
currentVersion = B64DC26528460B0D0063B8CF /* ScreenZ.xcdatamodel */;
407-
path = ScreenZ.xcdatamodeld;
408-
sourceTree = "<group>";
409-
versionGroupType = wrapper.xcdatamodel;
410-
};
411-
/* End XCVersionGroup section */
412401
};
413402
rootObject = B64DC24E28460B0D0063B8CF /* Project object */;
414403
}

ScreenZ/ScreenZ/AddThemeView.swift

+35-29
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,31 @@ import SwiftUI
99
import AVKit
1010

1111
struct AddThemeView: View {
12-
@State var displayedVideoURL: URL?
12+
@Environment(\.modelContext) private var context
13+
14+
@State var displayedVideoURL: URL = Bundle.main.url(forResource: "demo", withExtension: "mp4")!
1315
@State var bkgImg: NSImage?
1416
@State var showSaveView: Bool = false
1517
@State var showOpenPanel: Bool = false
1618
@State var showStatusAlert: Bool = false
1719
@State var statusInfo: AddThemeStatus?
1820
@State var inputName: String = ""
1921
var alert2: NSAlert = NSAlert()
20-
var avplayer: AVPlayer = AVPlayer(url:Bundle.main.url(forResource: "demo", withExtension: "mp4")!)
2122

22-
init() {
23-
avplayer.isMuted = true
24-
}
23+
private let avplayer: AVPlayer = {
24+
var player = AVPlayer(url:Bundle.main.url(forResource: "demo", withExtension: "mp4")!)
25+
player.isMuted = true
26+
return player
27+
}()
2528

2629
var body: some View {
2730
VStack {
2831
VideoPlayer(player: avplayer)
2932
.frame(width: 192 * 4, height: 108 * 4)
3033
.onChange(of: displayedVideoURL) { oldValue, newValue in
31-
if newValue != nil {
32-
oldValue?.stopAccessingSecurityScopedResource()
33-
avplayer.replaceCurrentItem(with: AVPlayerItem(url: newValue!))
34-
avplayer.play()
35-
}
34+
oldValue.stopAccessingSecurityScopedResource()
35+
avplayer.replaceCurrentItem(with: AVPlayerItem(url: newValue))
36+
avplayer.play()
3637
}
3738
HStack {
3839
Button {
@@ -72,7 +73,7 @@ struct AddThemeView: View {
7273
.alert("Wallpaper Name", isPresented: $showSaveView) {
7374
TextField("Enter name here", text: $inputName)
7475
Button("Save") {
75-
saveTheme(url: displayedVideoURL!)
76+
saveTheme(url: displayedVideoURL)
7677
}
7778
Button("Cancel") {
7879
//do nothing
@@ -110,7 +111,10 @@ struct AddThemeView: View {
110111
Task {
111112
do {
112113
let bigImage = try await getThumbnailImageFromVideoUrl(url: monoURL)
113-
try bigImage.saveTo(("tmp.png").foundFile(in: .documentDirectory))
114+
115+
try FileManager.default.createDirectory(at: FileManager.default.folderInDocument(folderName: "tmp"), withIntermediateDirectories: true)
116+
let (picURL, vidURL) = getResourcesURL(videoID: "tmp")
117+
try bigImage.saveTo(picURL)
114118
displayedVideoURL = monoURL
115119
bkgImg = bigImage
116120
statusInfo = AddThemeStatus(type: .Success, description: "Chosed a video")
@@ -127,35 +131,36 @@ struct AddThemeView: View {
127131
}
128132

129133
func applyPaper() {
130-
if let url = displayedVideoURL {
131-
NotificationCenter.default.post(name: Notification.Name("videourlChanged"), object: url)
132-
} else {
133-
statusInfo = AddThemeStatus(type: .Error, description: "Bad URL")
134-
showStatusAlert = true
135-
}
134+
NotificationCenter.default.post(name: Notification.Name("videourlChanged"), object: displayedVideoURL)
135+
// if let url = {
136+
//
137+
// } else {
138+
// statusInfo = AddThemeStatus(type: .Error, description: "Bad URL")
139+
// showStatusAlert = true
140+
// }
136141
}
137142

138143
func storeResources(monoName: String, srcURL: URL) async -> AddThemeStatus {
139-
let dstURL = (monoName+".mp4").foundFile(in: .documentDirectory)
144+
let video = Video(name: monoName)
145+
let (picURL, vidURL) = getResourcesURL(videoID: video.id)
140146
do {
141-
try FileManager.default.copyItem(at: srcURL, to: dstURL)
147+
//Ensure the parent folder is create
148+
try FileManager.default.createDirectory(at: FileManager.default.folderInDocument(folderName: video.id), withIntermediateDirectories: true)
149+
try FileManager.default.copyItem(at: srcURL, to: vidURL)
142150
let bigImage = try await getThumbnailImageFromVideoUrl(url: srcURL)
143-
try bigImage.saveTo((monoName+".png").foundFile(in: .documentDirectory))
144-
145-
let video = Video(context: PersistenceController.shared.container.viewContext)
146-
video.name = monoName
147-
video.url = dstURL
148-
video.photo = (monoName+".png").foundFile(in: .documentDirectory)
149-
try PersistenceController.shared.container.viewContext.save()
151+
try bigImage.saveTo(picURL)
152+
153+
context.insert(video)
154+
try context.save()
150155
return AddThemeStatus(type: .Success, description: "Saved Wallpaper")
151156
} catch(let error as NSError) {
152157
return AddThemeStatus(type: .Error, description: error.localizedDescription)
153158
}
154159
}
155160

156161
func saveTheme(url: URL) {
157-
if inputName == "" || PersistenceController.shared.fileExists(inputName) {
158-
statusInfo = AddThemeStatus(type: .Error, description: "Name invalid (null or duplicate)")
162+
if inputName == "" {
163+
statusInfo = AddThemeStatus(type: .Error, description: "Name is required")
159164
showStatusAlert = true
160165
return
161166
}
@@ -179,6 +184,7 @@ struct AddThemeView: View {
179184
let bigImage = NSImage(cgImage: cgThumbImage, size: NSSize(width: NSScreen.main!.frame.width, height: NSScreen.main!.frame.height))
180185
return bigImage
181186
}
187+
182188
}
183189

184190
struct AddThemeView_Previews: PreviewProvider {

0 commit comments

Comments
 (0)