Skip to content

Commit

Permalink
Merge pull request #98 from 42Box/96-scriptview
Browse files Browse the repository at this point in the history
feat: script 기능을 구현합니다.
  • Loading branch information
chanhihi authored Aug 29, 2023
2 parents cb1b6e5 + 1f5f73a commit 6d4c188
Show file tree
Hide file tree
Showing 34 changed files with 993 additions and 135 deletions.
157 changes: 100 additions & 57 deletions Box42.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "SnapKit",
"repositoryURL": "https://github.com/SnapKit/SnapKit.git",
"state": {
"branch": null,
"revision": "f222cbdf325885926566172f6f5f06af95473158",
"version": "5.6.0"
}
}
]
},
"version": 1
}
26 changes: 26 additions & 0 deletions Box42/Icon/IconController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// IconController.swift
// Box42
//
// Created by Chanhee Kim on 8/30/23.
//

import Foundation

class IconController {
let icon = MenubarViewController()

init() {
NotificationCenter.default.addObserver(self,
selector: #selector(handleUserProfileIconUpdate),
name: .didUpdateUserProfile,
object: nil)
}

@objc private func handleUserProfileIconUpdate() {
DispatchQueue.main.async {
self.icon.buttonImageChange(UserManager.shared.getUserProfile()?.icon ?? "fox")
}
print("Icon Changed")
}
}
9 changes: 8 additions & 1 deletion Box42/Main/BoxBaseContainerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BoxBaseContainerViewController: NSViewController {

// MARK: - QuickSlot
var preferenceVC: PreferencesViewController = PreferencesViewController()
var scriptsVC: ScriptsViewController = ScriptsViewController()

weak var menubarVCDelegate: MenubarViewControllerDelegate? // extension

Expand All @@ -37,10 +38,11 @@ class BoxBaseContainerViewController: NSViewController {

override func viewDidLoad() {
self.view.wantsLayer = true

// self.view.layer?.backgroundColor = NSColor(hex: "#FF9548").cgColor
self.view.layer?.backgroundColor = NSColor(hex: "#E7E7E7").cgColor

NotificationCenter.default.addObserver(self, selector: #selector(handleButtonTapped), name: NSNotification.Name(NotifConst.object.collectionButtonTapped), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleButtonTapped), name: .collectionButtonTapped, object: nil)
}

func BoxButtonViewGroupInit() -> BoxButtonViewGroup {
Expand Down Expand Up @@ -187,6 +189,11 @@ extension BoxBaseContainerViewController {
contentGroup.showPreferences()
}

if button.title == QuickSlotUI.title.scripts {
print("Button with title \(button.title) was tapped in BaseVC")
contentGroup.showScripts()
}

if button.title == QuickSlotUI.title.user {
print("Button with title \(button.title) was tapped in BaseVC")
contentGroup.removeAllSubviews()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ScriptsLogicController {
static let shared = ScriptsLogicController()

private init() {
NotificationCenter.default.addObserver(self, selector: #selector(handleButtonTapped), name: NSNotification.Name(NotifConst.object.collectionButtonTapped), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleButtonTapped), name: .collectionButtonTapped, object: nil)
}

@objc func handleButtonTapped(notification: NSNotification) {
Expand Down
2 changes: 1 addition & 1 deletion Box42/QuickSlot/Controller/QuickSlotViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class QuickSlotViewController: NSViewController {
let quickSlotViewGroup = QuickSlotGroupView()
quickSlotViewGroup.headerAction = headerAction

NotificationCenter.default.addObserver(self, selector: #selector(handleButtonTapped), name: NSNotification.Name(NotifConst.object.collectionButtonTapped), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleButtonTapped), name: .collectionButtonTapped, object: nil)

self.view = quickSlotViewGroup
}
Expand Down
4 changes: 2 additions & 2 deletions Box42/QuickSlot/Model/QuickSlotButtonModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ struct QuickSlotButtonModel {
let id: UUID
var title: String

init(title: String = "Default") {
self.id = UUID()
init(id: UUID = UUID(), title: String = "Default") {
self.id = id
self.title = title
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ extension PreferencesViewController: NSTableViewDelegate, NSTableViewDataSource
cell.textField?.stringValue = "Row \(row), Column \(tableColumn?.identifier ?? NSUserInterfaceItemIdentifier(""))"
return cell
}

func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
return 44.0 // 셀 높이를 44로 설정
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Combine

class QuickSlotButtonCollectionViewController: NSViewController {
@IBOutlet weak var quickSlotButtonCollectionView: NSCollectionView!
var viewModel: QuickSlotViewModel = QuickSlotViewModel()
var viewModel = QuickSlotViewModel.shared
var cancellables: Set<AnyCancellable> = []

override func viewDidLoad() {
Expand Down Expand Up @@ -60,10 +60,10 @@ class QuickSlotButtonCollectionViewController: NSViewController {
quickSlotButtonCollectionView.layer?.borderWidth = 0
quickSlotButtonCollectionView.layer?.borderColor = NSColor.clear.cgColor
quickSlotButtonCollectionView.backgroundColors = [NSColor.clear]

if let scrollView = quickSlotButtonCollectionView.enclosingScrollView {
// scrollView.hasVerticalScroller = false
// scrollView.hasHorizontalScroller = false
// scrollView.hasVerticalScroller = false
// scrollView.hasHorizontalScroller = false
scrollView.backgroundColor = NSColor.clear
scrollView.drawsBackground = false
}
Expand All @@ -86,11 +86,10 @@ extension QuickSlotButtonCollectionViewController: NSCollectionViewDelegate, NSC

func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "QuickSlotButtonViewItem"), for: indexPath)

if let customItem = item as? QuickSlotButtonViewItem {
let buttonModel = viewModel.buttons[indexPath.item]
let btn = NSButton()
// btn.title = "\(indexPath)"
btn.title = buttonModel.title
btn.action = #selector(collectionButtonTapped)
btn.target = self
Expand All @@ -105,6 +104,11 @@ extension QuickSlotButtonCollectionViewController: NSCollectionViewDelegate, NSC

extension QuickSlotButtonCollectionViewController {
@objc func collectionButtonTapped(_ sender: NSButton) {
NotificationCenter.default.post(name: NSNotification.Name(NotifConst.object.collectionButtonTapped), object: sender)
NotificationCenter.default.post(name: .collectionButtonTapped, object: sender)
}
}

// MARK: - Notification Name collectionButtonTapped
extension Notification.Name {
static let collectionButtonTapped = Notification.Name("collectionButtonTapped")
}
12 changes: 8 additions & 4 deletions Box42/QuickSlot/ViewModel/QuickSlotViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ import AppKit
import Combine

class QuickSlotViewModel {
static let shared = QuickSlotViewModel()
@Published var buttons: [QuickSlotButtonModel] = []

init() {
private init() {
let button1 = QuickSlotButtonModel(title: QuickSlotUI.title.clean)
let button2 = QuickSlotButtonModel(title: QuickSlotUI.title.preferences)
let button3 = QuickSlotButtonModel(title: QuickSlotUI.title.scripts)
let button4 = QuickSlotButtonModel(title: QuickSlotUI.title.user)

buttons = [button1, button2, button3, button4]
}

func addButton(_ button: QuickSlotButtonModel) {
buttons.append(button)
if buttons.count > 7 { return }
if !buttons.contains(where: { $0.id == button.id }) {
buttons.append(button)
}
}

func removeButton(id: UUID) {
func removeButton(_ id: UUID) {
buttons.removeAll { $0.id == id }
}

Expand Down
8 changes: 7 additions & 1 deletion Box42/Resources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Cocoa
@main
class AppDelegate: NSObject, NSApplicationDelegate {
var menubarController = MenubarViewController()
var iconController: IconController?
lazy var storage = Storage()

func applicationWillFinishLaunching(_ notification: Notification) {
Expand All @@ -18,13 +19,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
menubarController.menubarViewControllerStart()
iconController = IconController()
// alertAccessibility()
// hotkey()

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

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

func applicationWillTerminate(_ aNotification: Notification) {
Expand Down
17 changes: 17 additions & 0 deletions Box42/Scripts/Controller/ExcuteScripts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ExcuteScripts.swift
// Box42
//
// Created by Chanhee Kim on 8/29/23.
//

import Foundation

class ExcuteScripts {
static func executeShellScript(path: String) {
let task = Process()
task.launchPath = "/bin/sh"
task.arguments = [path]
task.launch()
}
}
49 changes: 49 additions & 0 deletions Box42/Scripts/Controller/ScriptsFileManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// ScriptsFileManager.swift
// Box42
//
// Created by Chanhee Kim on 8/29/23.
//

import Foundation

class ScriptsFileManager {
static func downloadFile(from URLString: String) {
let fileManager = FileManager.default
let pathComponent = URLString.split(separator: "/").map { String($0) }.last ?? ""
let documentsURL = try? fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let savedURL = documentsURL?.appendingPathComponent(pathComponent)

if let savedURL = savedURL, fileManager.fileExists(atPath: savedURL.path) {
print("File already exists, executing...")
ExcuteScripts.executeShellScript(path: savedURL.path)
return
}

guard let url = URL(string: URLString) else {
print("Invalid URL: \(URLString)")
return
}

let task = URLSession.shared.downloadTask(with: url) { (location, _, error) in
guard let location = location else {
print("Download failed: \(error?.localizedDescription ?? "Unknown error")")
return
}

do {
let documentsURL = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let savedURL = documentsURL.appendingPathComponent(pathComponent)
try fileManager.moveItem(at: location, to: savedURL)

print("Saved URL: ", savedURL)

ExcuteScripts.executeShellScript(path: savedURL.path)

} catch {
print("File error: \(error)")
}
}
task.resume()
}
}
40 changes: 40 additions & 0 deletions Box42/Scripts/Controller/ScriptsViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ScriptsViewController.swift
// Box42
//
// Created by Chanhee Kim on 8/29/23.
//

import Cocoa
import Foundation

class ScriptsViewController: NSViewController {
var scriptsTableView: ScriptsTableView?
var viewModel: ScriptViewModel? = ScriptViewModel() {
didSet {
scriptsTableView?.viewModel = viewModel
}
}

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

scriptsTableView = ScriptsTableView(frame: .zero)
scriptsTableView?.setup()
scriptsTableView?.viewModel = viewModel

let scrollView = NSScrollView()
scrollView.documentView = scriptsTableView
self.view.addSubview(scrollView)

scrollView.snp.makeConstraints({ make in
make.edges.equalToSuperview()
})

scriptsTableView?.snp.makeConstraints({ make in
make.edges.equalToSuperview()
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

import Foundation

struct Scripts {
var info: [(name: String, description: String)] = [("cleanCache", "cleaning cache"),
("brewInGoinfre", "brew download in goinfre")]
struct Scripts: Codable {
var info: [Script]
}

struct Script {
struct Script: Codable {
var id: UUID
var name: String
var description: String
Expand Down
Loading

0 comments on commit 6d4c188

Please sign in to comment.