Skip to content

Commit

Permalink
Fix slow download speed and add more resources. (#2)
Browse files Browse the repository at this point in the history
* Fix slow download speed and add more resoources.

* Check to update function
  • Loading branch information
diegotl authored Oct 19, 2023
1 parent 113c79f commit af54e63
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 88 deletions.
38 changes: 34 additions & 4 deletions Empusa.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
3F3B176E2ADDE117000283AE /* DestinationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F3B176D2ADDE117000283AE /* DestinationView.swift */; };
3F3B17702ADDE16A000283AE /* ResourcesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F3B176F2ADDE16A000283AE /* ResourcesView.swift */; };
3F3B17722ADDE25D000283AE /* DataProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F3B17712ADDE25D000283AE /* DataProgressView.swift */; };
3FCE9D2D2AE0944C00A4E3F5 /* Sparkle in Frameworks */ = {isa = PBXBuildFile; productRef = 3FCE9D2C2AE0944C00A4E3F5 /* Sparkle */; };
3FCE9D312AE17F5F00A4E3F5 /* CheckForUpdatesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FCE9D302AE17F5F00A4E3F5 /* CheckForUpdatesView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -43,6 +45,8 @@
3F3B176D2ADDE117000283AE /* DestinationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DestinationView.swift; sourceTree = "<group>"; };
3F3B176F2ADDE16A000283AE /* ResourcesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResourcesView.swift; sourceTree = "<group>"; };
3F3B17712ADDE25D000283AE /* DataProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataProgressView.swift; sourceTree = "<group>"; };
3FCE9D2E2AE0FE2E00A4E3F5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3FCE9D302AE17F5F00A4E3F5 /* CheckForUpdatesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckForUpdatesView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -51,6 +55,7 @@
buildActionMask = 2147483647;
files = (
3F21D3412ADD77E700B6A5D9 /* EmpusaKit in Frameworks */,
3FCE9D2D2AE0944C00A4E3F5 /* Sparkle in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -107,6 +112,7 @@
3F21D3352ADADC8D00B6A5D9 /* Resources */ = {
isa = PBXGroup;
children = (
3FCE9D2E2AE0FE2E00A4E3F5 /* Info.plist */,
3F21D30E2ADADC7400B6A5D9 /* Assets.xcassets */,
);
path = Resources;
Expand Down Expand Up @@ -135,6 +141,7 @@
3F3B176D2ADDE117000283AE /* DestinationView.swift */,
3F3B176F2ADDE16A000283AE /* ResourcesView.swift */,
3F3B17712ADDE25D000283AE /* DataProgressView.swift */,
3FCE9D302AE17F5F00A4E3F5 /* CheckForUpdatesView.swift */,
);
path = "UI Components";
sourceTree = "<group>";
Expand All @@ -157,6 +164,7 @@
name = Empusa;
packageProductDependencies = (
3F21D3402ADD77E700B6A5D9 /* EmpusaKit */,
3FCE9D2C2AE0944C00A4E3F5 /* Sparkle */,
);
productName = SwitchSDTool;
productReference = 3F21D3072ADADC7300B6A5D9 /* Empusa.app */;
Expand Down Expand Up @@ -208,6 +216,9 @@
Base,
);
mainGroup = 3F21D2FE2ADADC7300B6A5D9;
packageReferences = (
3FCE9D2B2AE0944C00A4E3F5 /* XCRemoteSwiftPackageReference "Sparkle" */,
);
productRefGroup = 3F21D3082ADADC7300B6A5D9 /* Products */;
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -243,6 +254,7 @@
files = (
3F3B17702ADDE16A000283AE /* ResourcesView.swift in Sources */,
3F21D30D2ADADC7300B6A5D9 /* MainView.swift in Sources */,
3FCE9D312AE17F5F00A4E3F5 /* CheckForUpdatesView.swift in Sources */,
3F3B17722ADDE25D000283AE /* DataProgressView.swift in Sources */,
3F21D30B2ADADC7300B6A5D9 /* EmpusaApp.swift in Sources */,
3F3B176E2ADDE117000283AE /* DestinationView.swift in Sources */,
Expand Down Expand Up @@ -396,11 +408,12 @@
CODE_SIGN_ENTITLEMENTS = Empusa/Empusa.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_TEAM = D97MJ3844Q;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Empusa/Resources/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = "εmpusa";
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
Expand All @@ -409,7 +422,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.0.1;
MARKETING_VERSION = 1.0.2;
PRODUCT_BUNDLE_IDENTIFIER = nl.trevisa.diego.Empusa;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -425,11 +438,12 @@
CODE_SIGN_ENTITLEMENTS = Empusa/Empusa.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_TEAM = D97MJ3844Q;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Empusa/Resources/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = "εmpusa";
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
Expand All @@ -438,7 +452,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.0.1;
MARKETING_VERSION = 1.0.2;
PRODUCT_BUNDLE_IDENTIFIER = nl.trevisa.diego.Empusa;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down Expand Up @@ -516,11 +530,27 @@
};
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
3FCE9D2B2AE0944C00A4E3F5 /* XCRemoteSwiftPackageReference "Sparkle" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/sparkle-project/Sparkle";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 2.5.1;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
3F21D3402ADD77E700B6A5D9 /* EmpusaKit */ = {
isa = XCSwiftPackageProductDependency;
productName = EmpusaKit;
};
3FCE9D2C2AE0944C00A4E3F5 /* Sparkle */ = {
isa = XCSwiftPackageProductDependency;
package = 3FCE9D2B2AE0944C00A4E3F5 /* XCRemoteSwiftPackageReference "Sparkle" */;
productName = Sparkle;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 3F21D2FF2ADADC7300B6A5D9 /* Project object */;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"pins" : [
{
"identity" : "sparkle",
"kind" : "remoteSourceControl",
"location" : "https://github.com/sparkle-project/Sparkle",
"state" : {
"revision" : "1f07f4096e52f19b5e7abaa697b7fc592b7ff57c",
"version" : "2.5.1"
}
},
{
"identity" : "zip",
"kind" : "remoteSourceControl",
Expand Down
15 changes: 14 additions & 1 deletion Empusa/EmpusaApp.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import SwiftUI
import Sparkle

@main
struct EmpusaApp: App {
private let updaterController: SPUStandardUpdaterController = .init(
startingUpdater: true,
updaterDelegate: nil,
userDriverDelegate: nil
)

var body: some Scene {
WindowGroup {
MainView(
model: EmpusaModel()
)
.frame(width: 680, height: 400)
.frame(width: 680, height: 420)
}
.windowResizability(.contentSize)
.commands {
CommandGroup(replacing: CommandGroupPlacement.newItem) {}
CommandGroup(after: .appInfo) {
CheckForUpdatesView(updater: updaterController.updater)
}
}
}
}
7 changes: 6 additions & 1 deletion Empusa/EmpusaModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ final class EmpusaModel: ObservableObject {
into: selectedVolume.url,
progressSubject: progressSubject
)

self.alertData = .init(
title: "Success",
message: "Selected resources have been downloaded into the selected destination."
)
} catch {
alertData = .init(error: error)
}
Expand Down Expand Up @@ -170,7 +175,7 @@ final class EmpusaModel: ObservableObject {

self.alertData = .init(
title: "Success",
message: "Backup sucessfully restored to the selected volume"
message: "Backup sucessfully restored to the selected destination."
)
}
}
Expand Down
10 changes: 10 additions & 0 deletions Empusa/Resources/Info.plist
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>SUFeedURL</key>
<string>https://empusa.pacotevicio.app/sparkle.xml</string>
<key>SUPublicEDKey</key>
<string>62os1l9hx8vk8noKLn706G/htYbpMlhOP2TTJQFa66w=</string>
</dict>
</plist>
26 changes: 26 additions & 0 deletions Empusa/UI Components/CheckForUpdatesView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import SwiftUI
import Sparkle

final class CheckForUpdatesViewModel: ObservableObject {
@Published var canCheckForUpdates = false

init(updater: SPUUpdater) {
updater.publisher(for: \.canCheckForUpdates)
.assign(to: &$canCheckForUpdates)
}
}

struct CheckForUpdatesView: View {
@ObservedObject private var checkForUpdatesViewModel: CheckForUpdatesViewModel
private let updater: SPUUpdater

init(updater: SPUUpdater) {
self.updater = updater
self.checkForUpdatesViewModel = CheckForUpdatesViewModel(updater: updater)
}

var body: some View {
Button("Check for Updates...", action: updater.checkForUpdates)
.disabled(!checkForUpdatesViewModel.canCheckForUpdates)
}
}
32 changes: 15 additions & 17 deletions Packages/EmpusaKit/Sources/EmpusaKit/Client/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import Foundation
import Combine

protocol ClientProtocol {
func request<T: Decodable>(url: URL) async throws -> T
func request<T: Decodable>(
url: URL
) async throws -> T

func downloadFile(
url: URL,
progressSubject: CurrentValueSubject<Double, Never>
) async throws -> Data
) async throws -> URL
}

final class Client: ClientProtocol {
final class Client: NSObject, ClientProtocol {
func request<T: Decodable>(url: URL) async throws -> T {
let request = URLRequest(url: url)
let (data, response) = try await URLSession
Expand All @@ -26,21 +29,16 @@ final class Client: ClientProtocol {
func downloadFile(
url: URL,
progressSubject: CurrentValueSubject<Double, Never>
) async throws -> Data {
let (asyncBytes, urlResponse) = try await URLSession
.shared
.bytes(from: url)

let length = urlResponse.expectedContentLength
var data = Data()
data.reserveCapacity(Int(length))

for try await byte in asyncBytes {
data.append(byte)
let downloadProgress = Double(data.count) / Double(length)
progressSubject.send(downloadProgress)
) async throws -> URL {
// TODO: implement download progress
defer {
progressSubject.send(1)
}

return data
let (localUrl, response) = try await URLSession
.shared
.download(from: url)

return localUrl
}
}
27 changes: 16 additions & 11 deletions Packages/EmpusaKit/Sources/EmpusaKit/ContentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,48 @@ public final class ContentManager: ContentManagerProtocol {

// Download asset
progressTitleSubject.send("Downloading \(resource.assetFileName)...")
let asset = try await githubService.downloadAsset(
let assetFileTempUrl = try await githubService.downloadAsset(
for: resource,
progressSubject: downloadProgressSubject
)

progressTitleSubject.send("Saving \(resource.assetFileName)...")
let assetFilePath = try await storageService.saveFile(
data: asset,
fileName: resource.assetFileName
// Rename asset file
let assetFileUrl = assetFileTempUrl
.deletingLastPathComponent()
.appending(component: resource.assetFileName)

try storageService.moveItem(
at: assetFileTempUrl,
to: assetFileUrl
)

let extractedPath = try {
let extractedUrl = try {
if resource.isAssetZipped {
// Unzip asset
progressTitleSubject.send("Unzipping \(resource.assetFileName)...")
return try storageService.unzipFile(
at: assetFilePath,
at: assetFileUrl,
progressSubject: unzipProgressSubject
)
} else {
// Do nothing
unzipProgressSubject.send(1)
return assetFilePath
return assetFileUrl
}
}()

// Copy asset to SD
progressTitleSubject.send("Copying contents of \(resource.assetFileName) into destination...")
try resource.handleAsset(
at: extractedPath,
at: extractedUrl,
destination: destination,
progressSubject: mergeProgressSubject
)

// Delete downloaded files on disk
progressTitleSubject.send("Removing temporary files...")
storageService.removeItem(at: assetFilePath)
storageService.removeItem(at: extractedPath)
storageService.removeItem(at: assetFileUrl)
storageService.removeItem(at: extractedUrl)

cancellable.cancel()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ extension FileManager {
.init(subsystem: "nl.trevisa.diego.Empusa.Services", category: "FileManager")
}

enum ActionType {
case move
case copy
}

enum ConflictResolution {
case keepSource
case keepDestination
}

func moveFile(
at location: URL,
to destination: URL
to destination: URL,
progressSubject: CurrentValueSubject<Double, Never>
) {
let fileName = location.lastPathComponent
let filedestination = destination.appending(path: fileName)
Expand Down Expand Up @@ -50,6 +46,8 @@ extension FileManager {
} catch {
logger.error("Move file error: \(error.localizedDescription)")
}

progressSubject.send(1)
}

func merge(
Expand Down
Loading

0 comments on commit af54e63

Please sign in to comment.