Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions Box42.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
DE77BA562A82637900713683 /* StateManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE77BA552A82637900713683 /* StateManager.swift */; };
DE77BBA22A9DDC40006CC98B /* ScriptsFileManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE77BBA12A9DDC40006CC98B /* ScriptsFileManager.swift */; };
DE77BBA62A9DDF2B006CC98B /* WebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE0A91772A8F014F00D1D6F1 /* WebView.swift */; };
DE77BBCD2A9E0568006CC98B /* ExcuteScripts.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE77BBCC2A9E0568006CC98B /* ExcuteScripts.swift */; };
DE77BBCD2A9E0568006CC98B /* ExecuteScripts.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE77BBCC2A9E0568006CC98B /* ExecuteScripts.swift */; };
DE77BBD22A9E0AE8006CC98B /* WebViewUI.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE77BBD12A9E0AE8006CC98B /* WebViewUI.swift */; };
DE77BBE22A9E0F70006CC98B /* Scripts.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE77BBE12A9E0F70006CC98B /* Scripts.swift */; };
DE77BBF02A9E38C6006CC98B /* GetUserProfile.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE77BBA92A9DE680006CC98B /* GetUserProfile.swift */; };
Expand All @@ -92,6 +92,11 @@
DE9457162A9E6D3000B0B768 /* ScriptQuickSlotButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE9457112A9E6A0000B0B768 /* ScriptQuickSlotButton.swift */; };
DE9457192A9EEEF000B0B768 /* ScriptCellManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE9457182A9EEEF000B0B768 /* ScriptCellManager.swift */; };
DE94571C2A9EFB7800B0B768 /* Associated+NSButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE94571B2A9EFB7800B0B768 /* Associated+NSButton.swift */; };
DE9457282A9F6E4400B0B768 /* GetUserMeScripts.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE9457272A9F6E4400B0B768 /* GetUserMeScripts.swift */; };
DE94572C2A9F75D500B0B768 /* API.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE94572B2A9F75D500B0B768 /* API.swift */; };
DE9457312A9FB01400B0B768 /* PreferencesTableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE9457302A9FB01400B0B768 /* PreferencesTableView.swift */; };
DE9457342A9FB1D300B0B768 /* RequestAccessView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE9457332A9FB1D300B0B768 /* RequestAccessView.swift */; };
DE9457372A9FC0A800B0B768 /* SecurityScopedResourceAccess.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE9457362A9FC0A800B0B768 /* SecurityScopedResourceAccess.swift */; };
DE97CA692A9A6364001073DE /* PixelConversion+CGFloat.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE97CA682A9A6364001073DE /* PixelConversion+CGFloat.swift */; };
DE97CA792A9A6F6A001073DE /* QuickSlotHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE97CA782A9A6F6A001073DE /* QuickSlotHeaderView.swift */; };
DE97CA7C2A9A7199001073DE /* QuickSlotGroupView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE97CA7B2A9A7199001073DE /* QuickSlotGroupView.swift */; };
Expand Down Expand Up @@ -173,7 +178,7 @@
DE77BA552A82637900713683 /* StateManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateManager.swift; sourceTree = "<group>"; };
DE77BBA12A9DDC40006CC98B /* ScriptsFileManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptsFileManager.swift; sourceTree = "<group>"; };
DE77BBA92A9DE680006CC98B /* GetUserProfile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetUserProfile.swift; sourceTree = "<group>"; };
DE77BBCC2A9E0568006CC98B /* ExcuteScripts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExcuteScripts.swift; sourceTree = "<group>"; };
DE77BBCC2A9E0568006CC98B /* ExecuteScripts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExecuteScripts.swift; sourceTree = "<group>"; };
DE77BBD12A9E0AE8006CC98B /* WebViewUI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewUI.swift; sourceTree = "<group>"; };
DE77BBE12A9E0F70006CC98B /* Scripts.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Scripts.swift; sourceTree = "<group>"; };
DE77BBE92A9E2DDF006CC98B /* UserManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserManager.swift; sourceTree = "<group>"; };
Expand All @@ -197,6 +202,11 @@
DE9457112A9E6A0000B0B768 /* ScriptQuickSlotButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptQuickSlotButton.swift; sourceTree = "<group>"; };
DE9457182A9EEEF000B0B768 /* ScriptCellManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptCellManager.swift; sourceTree = "<group>"; };
DE94571B2A9EFB7800B0B768 /* Associated+NSButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Associated+NSButton.swift"; sourceTree = "<group>"; };
DE9457272A9F6E4400B0B768 /* GetUserMeScripts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetUserMeScripts.swift; sourceTree = "<group>"; };
DE94572B2A9F75D500B0B768 /* API.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = API.swift; sourceTree = "<group>"; };
DE9457302A9FB01400B0B768 /* PreferencesTableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesTableView.swift; sourceTree = "<group>"; };
DE9457332A9FB1D300B0B768 /* RequestAccessView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestAccessView.swift; sourceTree = "<group>"; };
DE9457362A9FC0A800B0B768 /* SecurityScopedResourceAccess.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecurityScopedResourceAccess.swift; sourceTree = "<group>"; };
DE97CA682A9A6364001073DE /* PixelConversion+CGFloat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PixelConversion+CGFloat.swift"; sourceTree = "<group>"; };
DE97CA6E2A9A6EFC001073DE /* QuickSlotViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickSlotViewModel.swift; sourceTree = "<group>"; };
DE97CA712A9A6F0D001073DE /* QuickSlotButtonModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickSlotButtonModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -424,6 +434,7 @@
DE1F1A282A8B50E200A88DD8 /* BoxSizeManager.swift */,
DE77BA552A82637900713683 /* StateManager.swift */,
DE0A91622A8E6A5400D1D6F1 /* Constants.swift */,
DE9457362A9FC0A800B0B768 /* SecurityScopedResourceAccess.swift */,
);
path = Shared;
sourceTree = "<group>";
Expand All @@ -432,6 +443,8 @@
isa = PBXGroup;
children = (
DE77BBA92A9DE680006CC98B /* GetUserProfile.swift */,
DE9457272A9F6E4400B0B768 /* GetUserMeScripts.swift */,
DE94572B2A9F75D500B0B768 /* API.swift */,
);
path = API;
sourceTree = "<group>";
Expand Down Expand Up @@ -500,7 +513,7 @@
children = (
DE0A917A2A8F0CA800D1D6F1 /* AppleScripts+ShowMessage.swift */,
DE77BBA12A9DDC40006CC98B /* ScriptsFileManager.swift */,
DE77BBCC2A9E0568006CC98B /* ExcuteScripts.swift */,
DE77BBCC2A9E0568006CC98B /* ExecuteScripts.swift */,
DE7886272A9D186700FE21DD /* ScriptsViewController.swift */,
);
path = Controller;
Expand All @@ -510,6 +523,8 @@
isa = PBXGroup;
children = (
DE78862C2A9D1ADE00FE21DD /* PreferencesCell.swift */,
DE9457302A9FB01400B0B768 /* PreferencesTableView.swift */,
DE9457332A9FB1D300B0B768 /* RequestAccessView.swift */,
);
path = View;
sourceTree = "<group>";
Expand Down Expand Up @@ -793,13 +808,15 @@
DE9457192A9EEEF000B0B768 /* ScriptCellManager.swift in Sources */,
DE9457002A9E5B0900B0B768 /* ScriptCell.swift in Sources */,
DE7886042A9C71CB00FE21DD /* ScriptsTableView.swift in Sources */,
DE9457312A9FB01400B0B768 /* PreferencesTableView.swift in Sources */,
DE97CA872A9A7407001073DE /* QuickSlotButtonModel.swift in Sources */,
DE77BA562A82637900713683 /* StateManager.swift in Sources */,
DE1F1A1C2A8B50C500A88DD8 /* BoxBaseContainerViewController.swift in Sources */,
DEE0FA962A9A554F00085A65 /* FunctionButtonUI.swift in Sources */,
DE018BE72A509B1E00FF0AA3 /* WebViewController.swift in Sources */,
DE98E8552A98EA7900F8744A /* WindowButtonUI.swift in Sources */,
DE4407FA2A923E860091937A /* BoxFunctionViewController.swift in Sources */,
DE94572C2A9F75D500B0B768 /* API.swift in Sources */,
DE4407FE2A923EA90091937A /* PreferenceButtonView.swift in Sources */,
DE77BBF02A9E38C6006CC98B /* GetUserProfile.swift in Sources */,
DEF749322A85657600D987C8 /* NSScreen.swift in Sources */,
Expand All @@ -818,22 +835,25 @@
DE77BBA62A9DDF2B006CC98B /* WebView.swift in Sources */,
DE0A91832A8F889000D1D6F1 /* GoHomePageViaToolbar().swift in Sources */,
DE6332F22A9BCA2C00DCFAF6 /* QuickSlotScriptsLogicController.swift in Sources */,
DE9457342A9FB1D300B0B768 /* RequestAccessView.swift in Sources */,
DE018BB32A5099F900FF0AA3 /* AppDelegate.swift in Sources */,
DE78860C2A9C770300FE21DD /* ScriptsViewModel.swift in Sources */,
DE0A91632A8E6A5400D1D6F1 /* Constants.swift in Sources */,
DE7886172A9CCB3B00FE21DD /* UserProfile.swift in Sources */,
DE0A91902A8F88CA00D1D6F1 /* DisplayURLInToolbar.swift in Sources */,
DE77BBCD2A9E0568006CC98B /* ExcuteScripts.swift in Sources */,
DE77BBCD2A9E0568006CC98B /* ExecuteScripts.swift in Sources */,
DE9456F82A9E44FD00B0B768 /* IconController.swift in Sources */,
DE018BF32A509B3300FF0AA3 /* MenubarModel.swift in Sources */,
DE7A257A2A6D8CA20043225A /* PreferencesViewController.swift in Sources */,
DE24E63B2A8FE93900E29F5D /* NSImage.swift in Sources */,
DE94570F2A9E69F200B0B768 /* ScriptDeleteButton.swift in Sources */,
DE97CA792A9A6F6A001073DE /* QuickSlotHeaderView.swift in Sources */,
DE9457372A9FC0A800B0B768 /* SecurityScopedResourceAccess.swift in Sources */,
DE0A916D2A8E7DD700D1D6F1 /* HoverButton.swift in Sources */,
DE97CA862A9A7404001073DE /* QuickSlotViewModel.swift in Sources */,
DE3FF3762A978AB8009C88EF /* WindowCloseButton.swift in Sources */,
DE9457092A9E69D100B0B768 /* ScriptDescriptionsLabel.swift in Sources */,
DE9457282A9F6E4400B0B768 /* GetUserMeScripts.swift in Sources */,
DE77BBD22A9E0AE8006CC98B /* WebViewUI.swift in Sources */,
DE3FF3772A978AB8009C88EF /* WindowMinimizeButton.swift in Sources */,
DE4408022A923EB60091937A /* PinButtonView.swift in Sources */,
Expand Down
8 changes: 5 additions & 3 deletions Box42/Resources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// alertAccessibility()
// hotkey()

// storage.storageTimerEvent()
// storage.storageTimerEvent()
_ = UserManager.shared
_ = ScriptsLogicController.shared

// MARK: - 유저데이터 동기화
// WebViewManager.shared.getCookie()
// API.getUserProfile(WebViewManager.shared.getCookieWebKit)
WebViewManager.shared.getCookie()
API.getUserProfile(WebViewManager.shared.getCookieWebKit)
_ = QuickSlotViewModel.shared
API.getUserMeScripts(WebViewManager.shared.getCookieWebKit)
_ = ScriptViewModel.shared
}

func applicationWillTerminate(_ aNotification: Notification) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// ExcuteScripts.swift
// ExecuteScripts.swift
// Box42
//
// Created by Chanhee Kim on 8/29/23.
//

import Foundation

class ExcuteScripts {
class ExecuteScripts {
static func executeShellScript(path: String) {
let task = Process()
task.launchPath = "/bin/sh"
Expand Down
2 changes: 0 additions & 2 deletions Box42/Scripts/Controller/ScriptsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class ScriptsViewController: NSViewController {

override func loadView() {
self.view = NSView()
self.view.wantsLayer = true
self.view.layer?.backgroundColor = NSColor.blue.cgColor

scriptsTableView = ScriptsTableView(frame: .zero)
scriptsTableView?.setup()
Expand Down
4 changes: 3 additions & 1 deletion Box42/Scripts/Model/Scripts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ struct Script: Codable {
var name: String
var description: String
var path: String
var savedId: Int

init(name: String, description: String, path: String) {
init(name: String, description: String, path: String, savedId: Int) {
self.id = UUID()
self.name = name
self.description = description
self.path = path
self.savedId = savedId
}
}
2 changes: 1 addition & 1 deletion Box42/Scripts/View/Button/ScriptQuickSlotButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ScriptQuickSlotButton: NSButton {
guard self.window != nil else {
return
}

if self.title == "퀵슬롯" { return }
// 현재 버튼의 위치를 윈도우 기준으로 변환
let initialLocation = self.frame.origin

Expand Down
20 changes: 18 additions & 2 deletions Box42/Scripts/View/Table/ScriptCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,25 @@ class ScriptCell: NSTableCellView {
}
}

// script 내부 클릭시 1차 실행
// 있는거면 지우고 없는거면 추가
@objc func quickSlotButtonclicked() {
if let id = script?.id {
viewModel?.quickSlotScript(id: id)
guard let id = script?.id else {
return
}

let alreadyExists = QuickSlotViewModel.shared.buttons.contains { $0.id == id }

if alreadyExists {
QuickSlotViewModel.shared.removeButton(id)
quickSlotButton.title = "퀵슬롯"
} else {
if QuickSlotViewModel.shared.buttons.count > 7 {
return
} else {
quickSlotButton.title = "저장됨"
viewModel?.quickSlotScript(id: id)
}
}
}
}
32 changes: 27 additions & 5 deletions Box42/Scripts/ViewModel/ScriptsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,45 @@ import AppKit
import Combine

class ScriptViewModel: NSObject {
static let shared = ScriptViewModel()

@Published var scripts: [Script] = []

override init() {
self.scripts = [
Script(name: "cleanCache",
description: "Cleaning cache",
path: Bundle.main.path(forResource: "cleanCache", ofType: "sh") ?? ""),
path: Bundle.main.path(forResource: "cleanCache", ofType: "sh") ?? "", savedId: -1 ),
Script(name: "brewInGoinfre",
description: "Brew download in goinfre",
path: Bundle.main.path(forResource: "brewInGoinfre", ofType: "sh") ?? "")
path: Bundle.main.path(forResource: "brewInGoinfre", ofType: "sh") ?? "", savedId: -1),
Script(name: "exportMacOSInfo",
description: "export setting MacOS Info",
path: Bundle.main.path(forResource: "exportMacOSInfo", ofType: "sh") ?? "", savedId: -1 ),
Script(name: "importMacOSInfo",
description: "import MacOS Info",
path: Bundle.main.path(forResource: "importMacOSInfo", ofType: "sh") ?? "", savedId: -1),
Script(name: "key Mapping",
description: "key Mapping",
path: Bundle.main.path(forResource: "keyMapping", ofType: "sh") ?? "", savedId: -1 ),
Script(name: "nodeInstall",
description: "node Install",
path: Bundle.main.path(forResource: "nodeInstall", ofType: "sh") ?? "", savedId: -1)
]
}

// Create
func addScript(name: String, description: String, path: String) {
let newScript = Script(name: name, description: description, path: path)
let newScript = Script(name: name, description: description, path: path, savedId: -1)
scripts.append(newScript)
}

// Read
func excuteScript(id: UUID) {
if let index = scripts.firstIndex(where: { $0.id == id }) {
ExcuteScripts.executeShellScript(path: scripts[index].name)
// ExecuteScripts.executeShellScript(path: scripts[index].name)
// MARK: - 파일스크립트 매니저에서 권한을 얻은 실행으로 실행합니다.
SecurityScopedResourceAccess.accessResourceExecuteShellScript(scriptPath: scripts[index].path)
}
}

Expand All @@ -45,15 +61,21 @@ class ScriptViewModel: NSObject {

// Delete
func deleteScript(id: UUID) {
// delete 요청 보내야함 보내고 성공하면 지우기
scripts.removeAll(where: { $0.id == id })
QuickSlotViewModel.shared.removeButton(id)
}

// 새로운 스크립트 배열로 교체하는 메서드
func replaceScripts(with newScripts: [Script]) {
self.scripts = newScripts
}

// 스크립트안에서 해당하는 스크립트를 찾아서 quickslotVM에 추가
func quickSlotScript(id: UUID) {
if let index = scripts.firstIndex(where: { $0.id == id }) {
let button = QuickSlotButtonModel(id: id, title: scripts[index].name, path: scripts[index].path)
QuickSlotViewModel.shared.addButton(button)
}
}

}
63 changes: 63 additions & 0 deletions Box42/Shared/API/API.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Shared.swift
// Box42
//
// Created by Chanhee Kim on 8/30/23.
//

import WebKit

class API {
// GET
static func fetchDataFromAPI<T: Decodable>(withURL urlString: String, forType type: T.Type, completion: @escaping (Result<T, Error>) -> Void) {

let url = URL(string: urlString)!
var request = URLRequest(url: url)
request.httpShouldHandleCookies = true

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
completion(.failure(error))
return
}

guard let data = data else {
completion(.failure(NSError(domain: "NoData", code: -1, userInfo: nil)))
return
}

do {
let decodedData = try JSONDecoder().decode(type, from: data)
completion(.success(decodedData))
} catch let jsonError {
completion(.failure(jsonError))
}
}
task.resume()
}

// DELETE
static func deleteDataFromAPI(withURL urlString: String, completion: @escaping (Result<Data?, Error>) -> Void) {

let url = URL(string: urlString)!
var request = URLRequest(url: url)
request.httpMethod = "DELETE"
request.httpShouldHandleCookies = true

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
completion(.failure(error))
return
}

if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 {
completion(.failure(NSError(domain: "InvalidStatusCode", code: httpResponse.statusCode, userInfo: nil)))
return
}

completion(.success(data))
}
task.resume()
}

}
34 changes: 34 additions & 0 deletions Box42/Shared/API/GetUserMeScripts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// GetScripts.swift
// Box42
//
// Created by Chanhee Kim on 8/30/23.
//

import WebKit

extension API {
// MARK: - Scripts GET: https://api.42box.site/user-service/users/me/scripts
// result : scripts.shared 저장

static func getUserMeScripts(_ webView: WKWebView) {
webView.configuration.websiteDataStore.httpCookieStore.getAllCookies { cookies in
let cookieStorage = HTTPCookieStorage.shared
for cookie in cookies {
cookieStorage.setCookie(cookie)
}

fetchDataFromAPI(withURL: "https://api.42box.site/user-service/users/me/scripts", forType: [Script].self) { (result: Result<[Script], Error>) in
switch result {
case .success(let scripts):
print(">> MacOS Get :", scripts)
DispatchQueue.main.async {
ScriptViewModel.shared.replaceScripts(with: scripts)
}
case .failure(let error):
print("Error: \(error)")
}
}
}
}
}
Loading