From 4ea41100eed434da0f86456340174b5504410048 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sat, 11 May 2024 17:46:35 +0200 Subject: [PATCH] Can change adb path --- .../AndroidTools.xcodeproj/project.pbxproj | 4 ++ .../AndroidTools/Commons/AdbHelper.swift | 4 +- .../UseCases/GetAdbVersionUseCase.swift | 17 +++++++ .../Presentation/Logcat/LogcatView.swift | 38 ++++++++++++-- .../Presentation/Logcat/LogcatViewModel.swift | 3 +- .../Presentation/Settings/SettingsView.swift | 49 +++++++++++++++++-- .../Settings/SettingsViewModel.swift | 11 ++++- 7 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 AndroidTools/AndroidTools/Domain/UseCases/GetAdbVersionUseCase.swift diff --git a/AndroidTools/AndroidTools.xcodeproj/project.pbxproj b/AndroidTools/AndroidTools.xcodeproj/project.pbxproj index d2d350c..e7efb30 100644 --- a/AndroidTools/AndroidTools.xcodeproj/project.pbxproj +++ b/AndroidTools/AndroidTools.xcodeproj/project.pbxproj @@ -41,6 +41,7 @@ 817854852BC59F6500B5B2EE /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 817854842BC59F6500B5B2EE /* Preview Assets.xcassets */; }; 817EE8592BC81D4100740D4A /* FileExplorerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 817EE8582BC81D4100740D4A /* FileExplorerViewModel.swift */; }; 817EE85D2BC8247F00740D4A /* FileItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 817EE85C2BC8247F00740D4A /* FileItem.swift */; }; + 81A067C72BEEBA0100E83DEF /* GetAdbVersionUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81A067C62BEEBA0100E83DEF /* GetAdbVersionUseCase.swift */; }; 81B7900D2BEBA7E5008E7359 /* LogcatViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81B7900C2BEBA7E5008E7359 /* LogcatViewModel.swift */; }; 81B7900F2BEBA7F3008E7359 /* LogcatView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81B7900E2BEBA7F3008E7359 /* LogcatView.swift */; }; 81B790112BEBA9E8008E7359 /* LogEntryModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81B790102BEBA9E8008E7359 /* LogEntryModel.swift */; }; @@ -108,6 +109,7 @@ 817854862BC59F6500B5B2EE /* AndroidTools.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = AndroidTools.entitlements; sourceTree = ""; }; 817EE8582BC81D4100740D4A /* FileExplorerViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileExplorerViewModel.swift; sourceTree = ""; }; 817EE85C2BC8247F00740D4A /* FileItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileItem.swift; sourceTree = ""; }; + 81A067C62BEEBA0100E83DEF /* GetAdbVersionUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetAdbVersionUseCase.swift; sourceTree = ""; }; 81B7900C2BEBA7E5008E7359 /* LogcatViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogcatViewModel.swift; sourceTree = ""; }; 81B7900E2BEBA7F3008E7359 /* LogcatView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogcatView.swift; sourceTree = ""; }; 81B790102BEBA9E8008E7359 /* LogEntryModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogEntryModel.swift; sourceTree = ""; }; @@ -389,6 +391,7 @@ 81F41C5C2BEA7B3D005F1730 /* CreateFolderUseCase.swift */, 81F41C5E2BEA95FB005F1730 /* DeleteFileItemUseCase.swift */, 81F41C602BEA995D005F1730 /* ImportFileUseCase.swift */, + 81A067C62BEEBA0100E83DEF /* GetAdbVersionUseCase.swift */, ); path = UseCases; sourceTree = ""; @@ -485,6 +488,7 @@ 81E9822D2BDD06600004B154 /* InstallApplicationUseCase.swift in Sources */, 816A1D3A2BE8CAA300E7DA71 /* ApplicationRepository.swift in Sources */, 812399C82BDC3CC7004219C9 /* ToastView.swift in Sources */, + 81A067C72BEEBA0100E83DEF /* GetAdbVersionUseCase.swift in Sources */, 81D04F5A2BCA4B3400B04FAB /* FolderItem.swift in Sources */, 81F41C5B2BEA4DF4005F1730 /* TableView.swift in Sources */, 816A1D402BE8CDE000E7DA71 /* InstallApplicationError.swift in Sources */, diff --git a/AndroidTools/AndroidTools/Commons/AdbHelper.swift b/AndroidTools/AndroidTools/Commons/AdbHelper.swift index 5203d60..d5a16c3 100644 --- a/AndroidTools/AndroidTools/Commons/AdbHelper.swift +++ b/AndroidTools/AndroidTools/Commons/AdbHelper.swift @@ -9,7 +9,9 @@ import Foundation class AdbHelper { - let adbPath = "/usr/local/bin/adb" + var adbPath: String { + UserDefaults.standard.string(forKey: "adbPath") ?? "/usr/local/bin/adb" + } func getDevices() -> [DeviceListModel] { let command = "devices -l | awk 'NR>1 {print $1}'" diff --git a/AndroidTools/AndroidTools/Domain/UseCases/GetAdbVersionUseCase.swift b/AndroidTools/AndroidTools/Domain/UseCases/GetAdbVersionUseCase.swift new file mode 100644 index 0000000..5d16f78 --- /dev/null +++ b/AndroidTools/AndroidTools/Domain/UseCases/GetAdbVersionUseCase.swift @@ -0,0 +1,17 @@ +// +// GetAdbVersionUseCase.swift +// AndroidTools +// +// Created by Thomas Bernard on 10/05/2024. +// + +import Foundation + +class GetAdbVersionUseCase { + private let adbHelper : AdbHelper = AdbHelper() + + func execute() -> String { + let result = adbHelper.runAdbCommand("version") + return result + } +} diff --git a/AndroidTools/AndroidTools/Presentation/Logcat/LogcatView.swift b/AndroidTools/AndroidTools/Presentation/Logcat/LogcatView.swift index ccc3df5..2383ff4 100644 --- a/AndroidTools/AndroidTools/Presentation/Logcat/LogcatView.swift +++ b/AndroidTools/AndroidTools/Presentation/Logcat/LogcatView.swift @@ -19,8 +19,33 @@ struct LogcatView: View { var body: some View { VStack { HStack { + Menu(viewModel.filterPackage ?? "All packages") { + Button("Display all packages") { + viewModel.filterPackage = nil + } + + Divider() + + ForEach(Array(viewModel.pidToPackageMap.keys), id: \.self) { pid in + // Obtenir le nom du package à partir du dictionnaire en utilisant le PID + if let packageName = viewModel.pidToPackageMap[pid] { + Button("\(packageName) (\(pid))") { + // Mise à jour du filtre pour afficher uniquement les logs de ce package + viewModel.filterPackage = packageName + } + } + } + + + } + + TextField("Package", text: $viewModel.package) + + + } + .padding() // Table(viewModel.logEntries){ // TableColumn("Date") { entry in @@ -42,12 +67,15 @@ struct LogcatView: View { // Text(entry.message) // } // } + + List(viewModel.logEntries){ + LogEntryItem(date: $0.datetime, processId: $0.processID, threadId: $0.threadID, tag: $0.tag, packageName: $0.packageName, level: $0.level, message: $0.message) + .listRowSeparator(.hidden) + .listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0)) + } + + - List(viewModel.logEntries){ - LogEntryItem(date: $0.datetime, processId: $0.processID, threadId: $0.threadID, tag: $0.tag, packageName: $0.packageName, level: $0.level, message: $0.message) - .listRowSeparator(.hidden) - .listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0)) - } } diff --git a/AndroidTools/AndroidTools/Presentation/Logcat/LogcatViewModel.swift b/AndroidTools/AndroidTools/Presentation/Logcat/LogcatViewModel.swift index a17b88c..2761267 100644 --- a/AndroidTools/AndroidTools/Presentation/Logcat/LogcatViewModel.swift +++ b/AndroidTools/AndroidTools/Presentation/Logcat/LogcatViewModel.swift @@ -9,9 +9,10 @@ class LogcatViewModel : ObservableObject { @Published var logLevel: LogLevel? = nil @Published var logEntries: [LogEntryModel] = [] + @Published var filterPackage : String? = nil private var buffer: String = "" - private var pidToPackageMap: [Int: String] = [:] + @Published var pidToPackageMap: [Int: String] = [:] func getLogcat(deviceId: String) { DispatchQueue.global(qos: .userInitiated).async { [self] in diff --git a/AndroidTools/AndroidTools/Presentation/Settings/SettingsView.swift b/AndroidTools/AndroidTools/Presentation/Settings/SettingsView.swift index 515fae9..09d6a4d 100644 --- a/AndroidTools/AndroidTools/Presentation/Settings/SettingsView.swift +++ b/AndroidTools/AndroidTools/Presentation/Settings/SettingsView.swift @@ -6,7 +6,7 @@ struct SettingsView: View { case general, advanced } - private let viewModel = SettingsViewModel() + @StateObject private var viewModel = SettingsViewModel() private let appIcons: [String] = ["AppIconDark", "AppIconLight", "AppIconAndroid"] private let modes : [String] = ["Automatic", "Dark", "Light"] @@ -14,17 +14,32 @@ struct SettingsView: View { @AppStorage("mode") private var mode = "Automatic" @AppStorage("appIconName") private var appIconName = "AppIconDark" + @AppStorage("adbPath") private var adbPath = "/usr/local/bin/adb" + // State to track the currently selected icon @State private var selectedIcon: String = (NSApplication.shared.applicationIconImage.name() ?? "") + + private func openFinderToSelectADB() { + let panel = NSOpenPanel() + panel.canChooseFiles = true + panel.canChooseDirectories = false + panel.allowsMultipleSelection = false + panel.directoryURL = URL(fileURLWithPath: adbPath) + + panel.begin { response in + if response == .OK, let url = panel.url { + self.adbPath = url.path + viewModel.getAdbVersion() + } + } + } + var body: some View { Form { Group { - - - - Section(header: Text("Apparence").padding(.horizontal)) { + Section(header: Text("Apparence")) { VStack { Picker("", selection: $mode) { ForEach(modes, id: \.self) { mode in @@ -70,6 +85,26 @@ struct SettingsView: View { Text("Check for updates") }.buttonStyle(LargeButtonStyle()) + + + TextField("", text: $adbPath) + .disabled(true) + + + + HStack { + Button("Change location"){ + openFinderToSelectADB() + } + + Button("Check installation") { + viewModel.getAdbVersion() + } + } + + Text(viewModel.adbVersion) + + @@ -78,11 +113,15 @@ struct SettingsView: View { Spacer() } + .onAppear { + viewModel.getAdbVersion() + } } + } struct SettingsView_Previews: PreviewProvider { diff --git a/AndroidTools/AndroidTools/Presentation/Settings/SettingsViewModel.swift b/AndroidTools/AndroidTools/Presentation/Settings/SettingsViewModel.swift index 135907b..b0f7da5 100644 --- a/AndroidTools/AndroidTools/Presentation/Settings/SettingsViewModel.swift +++ b/AndroidTools/AndroidTools/Presentation/Settings/SettingsViewModel.swift @@ -8,8 +8,12 @@ import Foundation import Sparkle -class SettingsViewModel { +@Observable +class SettingsViewModel : ObservableObject { let updaterController : SPUStandardUpdaterController + private let getAdbVersionUseCase : GetAdbVersionUseCase = GetAdbVersionUseCase() + + var adbVersion : String = "" init(){ updaterController = SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil) @@ -18,4 +22,9 @@ class SettingsViewModel { func checkForUpdates(){ updaterController.updater.checkForUpdates() } + + func getAdbVersion(){ + adbVersion = "" + adbVersion = getAdbVersionUseCase.execute() + } }