Skip to content

Commit eced7db

Browse files
committed
feat: re-enable all
Signed-off-by: 82Flex <[email protected]>
1 parent 674e7c0 commit eced7db

File tree

6 files changed

+101
-10
lines changed

6 files changed

+101
-10
lines changed

TrollFools/CLI/CmdInject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ struct CmdInject: ParsableCommand {
5252
}
5353
injector.useWeakReference = weakReference
5454
injector.injectStrategy = fastInjection ? .fast : .lexicographic
55-
try injector.inject(pluginURLs)
55+
try injector.inject(pluginURLs, shouldPersist: false)
5656
}
5757
}

TrollFools/EjectListView.swift

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ struct EjectListView: View {
149149
.navigationTitle(NSLocalizedString("Plug-Ins", comment: ""))
150150
.animation(.easeOut, value: combines(
151151
ejectList.filter,
152+
ejectList.isOkToEnableAll,
153+
ejectList.isOkToDisableAll,
154+
isEnablingAll,
155+
isDisablingAll,
152156
isDeletingAll
153157
))
154158
.background(Group {
@@ -334,7 +338,76 @@ struct EjectListView: View {
334338
}
335339

336340
private func enableAll() {
341+
let disabledPlugInURLs = ejectList.injectedPlugIns
342+
.filter { !$0.isEnabled }
343+
.map { $0.url }
337344

345+
var logFileURL: URL?
346+
347+
do {
348+
let injector = try InjectorV3(ejectList.app.url)
349+
logFileURL = injector.latestLogFileURL
350+
351+
if injector.appID.isEmpty {
352+
injector.appID = ejectList.app.id
353+
}
354+
355+
if injector.teamID.isEmpty {
356+
injector.teamID = ejectList.app.teamID
357+
}
358+
359+
injector.useWeakReference = useWeakReference
360+
injector.preferMainExecutable = preferMainExecutable
361+
injector.injectStrategy = injectStrategy
362+
363+
let view = viewControllerHost.viewController?
364+
.navigationController?.view
365+
366+
view?.isUserInteractionEnabled = false
367+
368+
isEnablingAll = true
369+
isDisablingAll = false
370+
isDeletingAll = false
371+
372+
DispatchQueue.global(qos: .userInitiated).async {
373+
defer {
374+
DispatchQueue.main.async {
375+
ejectList.app.reload()
376+
ejectList.reload()
377+
378+
isEnablingAll = false
379+
isDisablingAll = false
380+
isDeletingAll = false
381+
382+
view?.isUserInteractionEnabled = true
383+
}
384+
}
385+
386+
do {
387+
try injector.inject(disabledPlugInURLs, shouldPersist: false)
388+
} catch {
389+
DispatchQueue.main.async {
390+
DDLogError("\(error)", ddlog: InjectorV3.main.logger)
391+
392+
var userInfo: [String: Any] = [
393+
NSLocalizedDescriptionKey: error.localizedDescription,
394+
]
395+
396+
if let logFileURL {
397+
userInfo[NSURLErrorKey] = logFileURL
398+
}
399+
400+
let nsErr = NSError(domain: Constants.gErrorDomain, code: 0, userInfo: userInfo)
401+
402+
lastError = nsErr
403+
isErrorOccurred = true
404+
}
405+
}
406+
}
407+
} catch {
408+
lastError = error
409+
isErrorOccurred = true
410+
}
338411
}
339412

340413
private func disableAll() {
@@ -365,15 +438,20 @@ struct EjectListView: View {
365438

366439
view?.isUserInteractionEnabled = false
367440

441+
isEnablingAll = false
442+
isDisablingAll = !shouldDesist
368443
isDeletingAll = shouldDesist
369444

370-
DispatchQueue.global(qos: .userInteractive).async {
445+
DispatchQueue.global(qos: .userInitiated).async {
371446
defer {
372447
DispatchQueue.main.async {
373448
ejectList.app.reload()
374449
ejectList.reload()
375450

451+
isEnablingAll = false
452+
isDisablingAll = false
376453
isDeletingAll = false
454+
377455
view?.isUserInteractionEnabled = true
378456
}
379457
}
@@ -413,7 +491,7 @@ struct EjectListView: View {
413491

414492
isExportingAll = true
415493

416-
DispatchQueue.global(qos: .userInteractive).async {
494+
DispatchQueue.global(qos: .userInitiated).async {
417495
defer {
418496
DispatchQueue.main.async {
419497
isExportingAll = false

TrollFools/InjectView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct InjectView: View {
9494
viewControllerHost.viewController = viewController
9595
}
9696
.onAppear {
97-
DispatchQueue.global(qos: .userInteractive).async {
97+
DispatchQueue.global(qos: .userInitiated).async {
9898
let result = inject()
9999

100100
DispatchQueue.main.async {
@@ -126,7 +126,7 @@ struct InjectView: View {
126126
injector.preferMainExecutable = preferMainExecutable
127127
injector.injectStrategy = injectStrategy
128128

129-
try injector.inject(urlList)
129+
try injector.inject(urlList, shouldPersist: true)
130130
return .success(injector.latestLogFileURL)
131131

132132
} catch {

TrollFools/InjectedPlugIn.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ struct InjectedPlugIn: Identifiable, Hashable {
1616
init(url: URL, isEnabled: Bool) {
1717
self.id = url.absoluteString
1818
self.url = url
19-
let attributes = try? FileManager.default.attributesOfItem(atPath: url.path)
20-
self.createdAt = attributes?[.creationDate] as? Date ?? Date()
19+
self.createdAt = (try? url.resourceValues(forKeys: [.creationDateKey]).creationDate) ?? Date()
2120
self.isEnabled = isEnabled
2221
}
2322
}

TrollFools/InjectorV3+Eject.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ extension InjectorV3 {
1212
// MARK: - Instance Methods
1313

1414
func ejectAll(shouldDesist: Bool) throws {
15-
try eject(injectedAssetURLsInBundle(bundleURL), shouldDesist: shouldDesist)
15+
var assetURLs: [URL]
16+
17+
assetURLs = injectedAssetURLsInBundle(bundleURL)
18+
if !assetURLs.isEmpty {
19+
try eject(assetURLs, shouldDesist: shouldDesist)
20+
}
21+
22+
if shouldDesist {
23+
assetURLs = persistedAssetURLs(id: appID)
24+
if !assetURLs.isEmpty {
25+
desist(assetURLs)
26+
}
27+
}
1628
}
1729

1830
func eject(_ assetURLs: [URL], shouldDesist: Bool) throws {

TrollFools/InjectorV3+Inject.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extension InjectorV3 {
2727

2828
// MARK: - Instance Methods
2929

30-
func inject(_ assetURLs: [URL]) throws {
30+
func inject(_ assetURLs: [URL], shouldPersist: Bool) throws {
3131
let preparedAssetURLs = try preprocessAssets(assetURLs)
3232

3333
precondition(!preparedAssetURLs.isEmpty, "No asset to inject.")
@@ -39,7 +39,9 @@ extension InjectorV3 {
3939
try injectDylibsAndFrameworks(preparedAssetURLs
4040
.filter { $0.pathExtension.lowercased() == "dylib" || $0.pathExtension.lowercased() == "framework" })
4141

42-
try persist(assetURLs)
42+
if shouldPersist {
43+
try persist(assetURLs)
44+
}
4345
}
4446

4547
// MARK: - Private Methods

0 commit comments

Comments
 (0)