Skip to content

Commit

Permalink
Merge pull request #64 from ginsudev/improvement/latest-grant-full-disk
Browse files Browse the repository at this point in the history
Updated grant_full_disk_access, concurrency and code improvements
  • Loading branch information
ginsudev authored Feb 9, 2023
2 parents 9ff779c + a2e83d3 commit 51acb70
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 96 deletions.
14 changes: 10 additions & 4 deletions WDBFontOverwrite.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
4F085E392994EF2F004099C1 /* ActionButtons.ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F085E382994EF2F004099C1 /* ActionButtons.ViewModel.swift */; };
4F4E64A7295F9AB600D4F04D /* CustomFontsScene.ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F4E64A6295F9AB600D4F04D /* CustomFontsScene.ViewModel.swift */; };
4FD690952986367C00B751B2 /* grant_full_disk_access.m in Sources */ = {isa = PBXBuildFile; fileRef = 4FD690942986367C00B751B2 /* grant_full_disk_access.m */; };
4FD690992986395B00B751B2 /* helpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 4FD690982986395B00B751B2 /* helpers.m */; };
Expand Down Expand Up @@ -38,6 +39,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
4F085E382994EF2F004099C1 /* ActionButtons.ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionButtons.ViewModel.swift; sourceTree = "<group>"; };
4F4E64A6295F9AB600D4F04D /* CustomFontsScene.ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomFontsScene.ViewModel.swift; sourceTree = "<group>"; };
4FD690942986367C00B751B2 /* grant_full_disk_access.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = grant_full_disk_access.m; sourceTree = "<group>"; };
4FD69096298637D400B751B2 /* grant_full_disk_access.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = grant_full_disk_access.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -116,6 +118,7 @@
4FE5EF302963E460003384EC /* NoticeView.swift */,
4FF28A1129679EEC00143640 /* ExplanationView.swift */,
4FF28A132967AA2D00143640 /* ActionButtons.swift */,
4F085E382994EF2F004099C1 /* ActionButtons.ViewModel.swift */,
);
path = MainInterface;
sourceTree = "<group>";
Expand Down Expand Up @@ -283,6 +286,7 @@
4FE5EF4129668C9C003384EC /* AlignedRowContentView.swift in Sources */,
4FE5EF492966AE1A003384EC /* FontDiscoveryCard.swift in Sources */,
4FE5EF3529653188003384EC /* FontMap.swift in Sources */,
4F085E392994EF2F004099C1 /* ActionButtons.ViewModel.swift in Sources */,
4FF28A102967956300143640 /* PresetFontsScene.ViewModel.swift in Sources */,
4FE5EF312963E460003384EC /* NoticeView.swift in Sources */,
4FE5EF3329640075003384EC /* WDBImportCustomFontPickerViewControllerDelegate.swift in Sources */,
Expand Down Expand Up @@ -421,6 +425,7 @@
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = WDBFontOverwrite/Info.plist;
INFOPLIST_KEY_NSAppleMusicUsageDescription = WDBFontOverwrite;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand All @@ -435,8 +440,8 @@
"$(inherited)",
"$(PROJECT_DIR)/WDBFontOverwrite",
);
MARKETING_VERSION = "1.10.7-1";
PRODUCT_BUNDLE_IDENTIFIER = com.ginsu.WDBFontOverwrite;
MARKETING_VERSION = 1.10.8;
PRODUCT_BUNDLE_IDENTIFIER = com.ginsudev.WDBFontOverwrite;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_OBJC_BRIDGING_HEADER = "WDBFontOverwrite/WDBFontOverwrite-Bridging-Header.h";
Expand All @@ -459,6 +464,7 @@
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = WDBFontOverwrite/Info.plist;
INFOPLIST_KEY_NSAppleMusicUsageDescription = WDBFontOverwrite;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand All @@ -473,8 +479,8 @@
"$(inherited)",
"$(PROJECT_DIR)/WDBFontOverwrite",
);
MARKETING_VERSION = "1.10.7-1";
PRODUCT_BUNDLE_IDENTIFIER = com.ginsu.WDBFontOverwrite;
MARKETING_VERSION = 1.10.8;
PRODUCT_BUNDLE_IDENTIFIER = com.ginsudev.WDBFontOverwrite;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_OBJC_BRIDGING_HEADER = "WDBFontOverwrite/WDBFontOverwrite-Bridging-Header.h";
Expand Down
6 changes: 3 additions & 3 deletions WDBFontOverwrite/FontMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ struct FontMap {

static let emojiCustomFont = CustomFont(
name: "Emoji",
targetPath: .many([
targetPaths: [
"/System/Library/Fonts/CoreAddition/AppleColorEmoji-160px.ttc",
"/System/Library/Fonts/Core/AppleColorEmoji.ttc",
]),
],
localPath: "CustomAppleColorEmoji.ttc"
)

Expand All @@ -32,7 +32,7 @@ struct FontMap {
}
fontMap[key(forFont: font)] = CustomFont(
name: font,
targetPath: .single("\(fontDirPath)\(dir)/\(font)"),
targetPaths: ["\(fontDirPath)\(dir)/\(font)"],
localPath: "Custom\(font)"
)
}
Expand Down
45 changes: 45 additions & 0 deletions WDBFontOverwrite/MainInterface/ActionButtons.ViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// ActionButtons.ViewModel.swift
// WDBFontOverwrite
//
// Created by Noah Little on 9/2/2023.
//

import UIKit.UIApplication

extension ActionButtons {
struct ViewModel {
func clearKBCache() {
grant_full_disk_access { error in
if error != nil {
print("can't get disk access")
} else {
_UIKeyboardCache.purge()
}
}
}

@available(iOS 15, *)
func respringModern() {
grant_full_disk_access { error in
if error != nil {
print("can't get disk access, using backup respring")
respringLegacy()
} else {
xpc_crasher(UnsafeMutablePointer<CChar>(mutating: "com.apple.frontboard.systemappservices"))
}
}
}

@available(iOS, deprecated: 15)
func respringLegacy() {
let sharedApplication = UIApplication.shared
let windows = sharedApplication.windows
if let window = windows.first {
while true {
window.snapshotView(afterScreenUpdates: false)
}
}
}
}
}
45 changes: 8 additions & 37 deletions WDBFontOverwrite/MainInterface/ActionButtons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import SwiftUI

struct ActionButtons: View {
private let viewModel = ViewModel()

var body: some View {
if #available(iOS 15, *) {
Button {
clearKBCache()
viewModel.clearKBCache()
} label: {
AlignedRowContentView(
imageName: "trash",
Expand All @@ -20,49 +22,18 @@ struct ActionButtons: View {
}
}
Button {
respring()
if #available(iOS 15, *) {
viewModel.respringModern()
} else {
viewModel.respringLegacy()
}
} label: {
AlignedRowContentView(
imageName: "arrow.triangle.2.circlepath",
text: "Restart SpringBoard"
)
}
}

private func clearKBCache() {
grant_full_disk_access { error in
if error != nil {
print("can't get disk access")
} else {
_UIKeyboardCache.purge()
}
}
}

private func respring() {
if #available(iOS 15, *) {
grant_full_disk_access { error in
if error != nil {
print("can't get disk access, using backup respring")
respringBackup()
} else {
xpc_crasher(UnsafeMutablePointer<Int8>(mutating: "com.apple.frontboard.systemappservices"))
}
}
} else {
respringBackup()
}
}

private func respringBackup() {
let sharedApplication = UIApplication.shared
let windows = sharedApplication.windows
if let window = windows.first {
while true {
window.snapshotView(afterScreenUpdates: false)
}
}
}
}

struct ActionButtons_Previews: PreviewProvider {
Expand Down
11 changes: 3 additions & 8 deletions WDBFontOverwrite/MainInterface/CustomFontsScene.ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@

import Foundation

enum PathType {
case single(String)
case many([String])
}

struct CustomFont {
var name: String
var targetPath: PathType?
var targetPaths: [String]
var localPath: String
var notice: Notice?
}
Expand Down Expand Up @@ -42,7 +37,7 @@ extension CustomFontsScene {
let emojiFont = FontMap.emojiCustomFont
await overwriteWithCustomFont(
name: emojiFont.localPath,
targetPath: emojiFont.targetPath
targetPaths: emojiFont.targetPaths
)
await MainActor.run {
ProgressManager.shared.isBusy = false
Expand All @@ -62,7 +57,7 @@ extension CustomFontsScene {
if let customFont = FontMap.fontMap[key] {
await overwriteWithCustomFont(
name: customFont.localPath,
targetPath: customFont.targetPath
targetPaths: customFont.targetPaths
)
}
}
Expand Down
7 changes: 6 additions & 1 deletion WDBFontOverwrite/MainInterface/ExplanationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ struct ExplanationView: View {
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 5)
.fill(Color(UIColor(red: 0.44, green: 0.69, blue: 0.67, alpha: 1.00)))
.fill(Color(UIColor(
red: 0.44,
green: 0.69,
blue: 0.67,
alpha: 1.00
)))
VStack(alignment: .center, spacing: 10) {
imageView
descriptionView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ extension FileEditorView {
@Published var files = [String]()
@Published var isVisibleRemoveAllAlert = false

private var documentsDirectory: URL {
fileManager.urls(
for: .documentDirectory,
in: .userDomainMask
)[0]
}

func populateFiles() async {
do {
let path = documentsDirectory().relativePath
let path = documentsDirectory.relativePath

try await MainActor.run { [weak self] in
self?.files = try fileManager.contentsOfDirectory(atPath: path)
self?.files = try fileManager.contentsOfDirectory(atPath: path).filter({ !$0.hasSuffix("token.txt") })
}
} catch {
print(error)
Expand All @@ -27,7 +34,7 @@ extension FileEditorView {

func remove(file: String) {
do {
try fileManager.removeItem(at: documentsDirectory().appendingPathComponent(file))
try fileManager.removeItem(at: documentsDirectory.appendingPathComponent(file))
} catch {
print(error)
}
Expand All @@ -38,12 +45,5 @@ extension FileEditorView {
remove(file: file)
}
}

private func documentsDirectory() -> URL {
return fileManager.urls(
for: .documentDirectory,
in: .userDomainMask
)[0]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ extension PresetFontsScene {
),
]

func overwrite(withName name: String) async {
nonisolated func overwrite(withName name: String) async {
await overwriteWithFont(name: name)
Task { @MainActor in
await MainActor.run {
ProgressManager.shared.isBusy = false
}
}
Expand Down
14 changes: 4 additions & 10 deletions WDBFontOverwrite/OverwriteFontImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func dumpCurrentFont() {

func overwriteWithCustomFont(
name: String,
targetPath: PathType?
targetPaths: [String]?
) async {
let documentDirectory = FileManager.default.urls(
for: .documentDirectory,
Expand All @@ -171,22 +171,16 @@ func overwriteWithCustomFont(
return
}

switch targetPath {
case .single(let path):
await overwriteWithFont(
fontURL: fontURL,
pathToTargetFont: path
)
case .many(let paths):
for path in paths {
if let targetPaths {
for path in targetPaths {
if (access(path, F_OK) == 0) {
await overwriteWithFont(
fontURL: fontURL,
pathToTargetFont: path
)
}
}
default:
} else {
await MainActor.run {
ProgressManager.shared.message = "Either targetName or targetNames must be provided"
}
Expand Down
14 changes: 5 additions & 9 deletions WDBFontOverwrite/Progress/ProgressManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ final class ProgressManager: ObservableObject {
@Published var isPresentedResultsAlert = false {
didSet {
if !isPresentedResultsAlert {
Task { @MainActor in
importResults = []
message = "Done."
}
importResults = []
message = "Done."
}
}
}
Expand All @@ -35,11 +33,9 @@ final class ProgressManager: ObservableObject {
didSet {
if !isBusy {
// Reset values when done.
Task { @MainActor in
completedProgress = 0
totalProgress = 0
isPresentedResultsAlert = true
}
completedProgress = 0
totalProgress = 0
isPresentedResultsAlert = true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class WDBImportCustomFontPickerViewControllerDelegate: NSObject, UIDocumentPicke
}

func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
ProgressManager.shared.message = "Cancelled"
Task { @MainActor in
ProgressManager.shared.message = "Cancelled"
}
}

private func importSelectedFonts(atURLs urls: [URL]) async {
Expand Down Expand Up @@ -66,11 +68,7 @@ class WDBImportCustomFontPickerViewControllerDelegate: NSObject, UIDocumentPicke
targetURL: targetURL,
ttcRepackMode: self.ttcRepackMode
)
if success == nil {
return 1
} else {
return 0
}
return success == nil ? 1 : 0
}
}

Expand Down
Loading

0 comments on commit 51acb70

Please sign in to comment.