Skip to content

Commit af3e7b8

Browse files
committed
feat: toggle plugins
Signed-off-by: 82Flex <[email protected]>
1 parent eced7db commit af3e7b8

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

TrollFools/EjectListModel.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ final class EjectListModel: ObservableObject {
1818
@Published var isOkToEnableAll = false
1919
@Published var isOkToDisableAll = false
2020

21+
@Published var processingPlugIn: InjectedPlugIn?
22+
2123
private var cancellables = Set<AnyCancellable>()
2224

2325
init(_ app: App) {
@@ -58,7 +60,14 @@ final class EjectListModel: ObservableObject {
5860
}
5961

6062
self.filteredPlugIns = filteredPlugIns
61-
self.isOkToEnableAll = filteredPlugIns.contains { !$0.isEnabled }
62-
self.isOkToDisableAll = filteredPlugIns.contains { $0.isEnabled }
63+
isOkToEnableAll = filteredPlugIns.contains { !$0.isEnabled }
64+
isOkToDisableAll = filteredPlugIns.contains { $0.isEnabled }
65+
}
66+
67+
func togglePlugIn(_ plugIn: InjectedPlugIn, isEnabled: Bool) {
68+
guard plugIn.isEnabled != isEnabled else {
69+
return
70+
}
71+
processingPlugIn = plugIn
6372
}
6473
}

TrollFools/EjectListView.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ struct EjectListView: View {
163163
)
164164
} label: { }
165165
})
166+
.onChange(of: ejectList.processingPlugIn) { plugIn in
167+
if let plugIn {
168+
togglePlugIn(plugIn)
169+
}
170+
}
166171
}
167172

168173
var enableAllButton: some View {
@@ -337,6 +342,53 @@ struct EjectListView: View {
337342
}
338343
}
339344

345+
private func togglePlugIn(_ plugIn: InjectedPlugIn) {
346+
var logFileURL: URL?
347+
348+
do {
349+
let plugInURLsToProcess = [plugIn.url]
350+
351+
let injector = try InjectorV3(ejectList.app.url)
352+
logFileURL = injector.latestLogFileURL
353+
354+
if injector.appID.isEmpty {
355+
injector.appID = ejectList.app.id
356+
}
357+
358+
if injector.teamID.isEmpty {
359+
injector.teamID = ejectList.app.teamID
360+
}
361+
362+
injector.useWeakReference = useWeakReference
363+
injector.preferMainExecutable = preferMainExecutable
364+
injector.injectStrategy = injectStrategy
365+
366+
if plugIn.isEnabled {
367+
try injector.eject(plugInURLsToProcess, shouldDesist: false)
368+
} else {
369+
try injector.inject(plugInURLsToProcess, shouldPersist: false)
370+
}
371+
372+
ejectList.app.reload()
373+
ejectList.reload()
374+
} catch {
375+
DDLogError("\(error)", ddlog: InjectorV3.main.logger)
376+
377+
var userInfo: [String: Any] = [
378+
NSLocalizedDescriptionKey: error.localizedDescription,
379+
]
380+
381+
if let logFileURL {
382+
userInfo[NSURLErrorKey] = logFileURL
383+
}
384+
385+
let nsErr = NSError(domain: Constants.gErrorDomain, code: 0, userInfo: userInfo)
386+
387+
lastError = nsErr
388+
isErrorOccurred = true
389+
}
390+
}
391+
340392
private func enableAll() {
341393
let disabledPlugInURLs = ejectList.injectedPlugIns
342394
.filter { !$0.isEnabled }

TrollFools/InjectedPlugIn.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
struct InjectedPlugIn: Identifiable, Hashable {
10+
struct InjectedPlugIn: Equatable, Identifiable, Hashable {
1111
let id: String
1212
let url: URL
1313
let createdAt: Date

TrollFools/InjectorV3+Inject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension InjectorV3 {
4040
.filter { $0.pathExtension.lowercased() == "dylib" || $0.pathExtension.lowercased() == "framework" })
4141

4242
if shouldPersist {
43-
try persist(assetURLs)
43+
try persist(preparedAssetURLs)
4444
}
4545
}
4646

TrollFools/PlugInCell.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ struct PlugInCell: View {
8484
.onAppear {
8585
isEnabled = plugIn.isEnabled
8686
}
87+
.onChange(of: isEnabled) { value in
88+
ejectList.togglePlugIn(plugIn, isEnabled: value)
89+
}
8790
.contextMenu {
8891
if #available(iOS 16.4, *) {
8992
ShareLink(item: plugIn.url) {

0 commit comments

Comments
 (0)