Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 전반적인 서비스 흐름을 개선합니다 2 #149

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
2 changes: 1 addition & 1 deletion Box42/Menubar/MenubarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ extension MenubarViewController: MenubarViewControllerDelegate {
let window = boxWindowController?.window {
if StateManager.shared.showFirstWindow == false {
let buttonFrame = button.window?.convertToScreen(button.frame) ?? NSZeroRect
let desiredPosition = NSPoint(x: buttonFrame.origin.x - (BoxSizeManager.shared.size.width / 2) - 10, y: buttonFrame.origin.y - window.frame.height)
let desiredPosition = NSPoint(x: buttonFrame.origin.x - (BoxSizeManager.shared.size.width / 2) - 120, y: buttonFrame.origin.y - window.frame.height)

window.setFrameOrigin(desiredPosition)
StateManager.shared.toggleShowFirstWindow()
Expand Down
33 changes: 30 additions & 3 deletions Box42/Preferences/View/Funtion/StorageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import AppKit
import SnapKit
import Combine

class StorageView: NSView {

Expand All @@ -16,7 +17,8 @@ class StorageView: NSView {
// var thresholdTextField: NSTextField = NSTextField()
// var intervalTextField: NSTextField = NSTextField()
var executeScriptButton: NSButton = NSButton()

private var subscriptions = Set<AnyCancellable>()

override init(frame frameRect: NSRect) {
super.init(frame: frameRect)

Expand All @@ -34,6 +36,8 @@ class StorageView: NSView {

// Set constraints
setConstraints()
bindStorageValues()

}

required init?(coder: NSCoder) {
Expand Down Expand Up @@ -118,8 +122,8 @@ class StorageView: NSView {
}

@objc func runScript(_ sender: NSButton) {
// Storage.shared.storageTimerEvent()

StateManager.shared.autoStorage = true
Storage.shared.storageTimerEvent()
let notification = NSUserNotification()
notification.title = "자동화 스크립트를 실행합니다."
let center = NSUserNotificationCenter.default
Expand All @@ -132,4 +136,27 @@ class StorageView: NSView {
remainingStorageTextField.stringValue = "Remaining Storage: \(remaining) GB"
totalStorageTextField.stringValue = "Total Storage: \(total) GB"
}

private func bindStorageValues() {
let formatter = NumberFormatter()
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
formatter.numberStyle = .decimal

Storage.shared.$usedUsage
.map { formatter.string(from: NSNumber(value: $0 ?? 0)) ?? "0" }
.receive(on: DispatchQueue.main)
.sink { [weak self] value in
self?.currentStorageTextField.stringValue = "Current Storage : \(value) GB"
}
.store(in: &subscriptions)

Storage.shared.$availableUsage
.map { formatter.string(from: NSNumber(value: $0 ?? 0)) ?? "0" }
.receive(on: DispatchQueue.main)
.sink { [weak self] value in
self?.remainingStorageTextField.stringValue = "Remaining Storage : \(value) GB"
}
.store(in: &subscriptions)
}
}
2 changes: 1 addition & 1 deletion Box42/QuickSlot/View/QuickSlotGroupView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SnapKit
class QuickSlotGroupView: NSView {

lazy var divider: NSBox = Divider(completion: { [weak self] in self?.dividerAction?() })
lazy var headerView: QuickSlotHeaderView = QuickSlotHeaderView(image: NSImage(imageLiteralResourceName: "Star"), completion: { [weak self] in self?.headerAction?() })
lazy var headerView: QuickSlotHeaderView = QuickSlotHeaderView(image: NSImage(imageLiteralResourceName: "star"), completion: { [weak self] in self?.headerAction?() })
lazy var buttonCollectionView: QuickSlotButtonCollectionViewController = QuickSlotButtonCollectionViewController()

var dividerAction: (() -> Void)?
Expand Down
2 changes: 2 additions & 0 deletions Box42/Resources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
API.getUserProfile(WebViewManager.shared.getCookieWebKit)
_ = ScriptViewModel.shared
_ = BookmarkViewModel.shared

Storage.shared.storageTimerEvent()
}

func applicationWillTerminate(_ aNotification: Notification) {
Expand Down
4 changes: 4 additions & 0 deletions Box42/Shared/User/UserManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import Foundation

class UserManager {
static let shared = UserManager()

var userProfile: UserProfile? {
didSet {
NotificationCenter.default.post(name: .didUpdateUserProfile, object: nil)
}
}

private let fileBookmarkKey = "myFileAppSandBox"

private init() {}
Expand All @@ -32,6 +34,8 @@ class UserManager {
func updateUserProfile(newProfile: UserProfile?) {
if let new = newProfile {
self.userProfile = new
BookmarkViewModel.shared.bookMarkList = newProfile?.urlList ?? UserProfile.defaultProfile().urlList
QuickSlotViewModel.shared.buttons = newProfile?.quickSlotList ?? UserProfile.defaultProfile().quickSlotList
} else {
self.userProfile = UserProfile.defaultProfile()
}
Expand Down
18 changes: 18 additions & 0 deletions Box42/Shared/User/UserProfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,22 @@ extension UserProfile {
]
)
}

static func defaultQuickSlot() -> [QuickSlotButtonModel] {
return [
QuickSlotButtonModel(scriptUuid: UUID(uuidString: "37a56076-e72c-4efe-ba7f-de0effe7f4c3f"),
title: QuickSlotUI.title.clean,
path: Bundle.main.path(forResource: "CleanCache_cluster", ofType: "sh"),
type: "sh"
)
, QuickSlotButtonModel(title: QuickSlotUI.title.preferences,
path: "default-preferences",
type: "default-pref")
, QuickSlotButtonModel(title: QuickSlotUI.title.scripts,
path: "default-scripts",
type: "default-sh")
, QuickSlotButtonModel(title: QuickSlotUI.title.user,
path: "default-user",
type: "default-pref")]
}
}
28 changes: 21 additions & 7 deletions Box42/System/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Storage {

private var subscriptions = Set<AnyCancellable>()

var availableUsage: Double?
var usedUsage: Double?
@Published var availableUsage: Double?
@Published var usedUsage: Double?
var totalUsage: Double?

private init(config: StorageConfig = StorageConfig.shared) {
Expand Down Expand Up @@ -63,7 +63,7 @@ class Storage {
return
}

storageTimer = Timer.scheduledTimer(withTimeInterval: config.period.rawValue, repeats: true, block: { [weak self] _ in
storageTimer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true, block: { [weak self] _ in
guard let self = self else { return }

if self.checkStorage() {
Expand All @@ -72,9 +72,17 @@ class Storage {
}

if let usedUsage = self.usedUsage, let totalUsage = self.totalUsage, totalUsage != 0 {
let usagePercentage = (totalUsage - usedUsage) / totalUsage
if usagePercentage < self.config.threshold.rawValue {
self.cleanSh()
let remain = (totalUsage - usedUsage)
// let usagePercentage = (totalUsage - usedUsage) / totalUsage
// if usagePercentage < self.config.threshold.rawValue {
if remain < 0.3 {
self.autoCleanSh()

let notification = NSUserNotification()
notification.title = "자동 클린 캐시가 실행 되었습니다."
let center = NSUserNotificationCenter.default
center.deliver(notification)

self.count += 1
if self.count > 1 {
// showMessageWithAppleScript("캐시 문제가 아닙니다. ncdu ~ 를 확인해주세요.", "재시작") { button in
Expand All @@ -93,6 +101,11 @@ class Storage {
// }
// }
// }
let notification = NSUserNotification()
notification.title = "캐시 문제가 아닙니다. \n 용량을 확인해주세요."
let center = NSUserNotificationCenter.default
center.deliver(notification)

StateManager.shared.setOffAutoStorage()
self.storageTimer?.invalidate()
} else {
Expand All @@ -101,6 +114,7 @@ class Storage {
} else {
self.count = 0
}

} else {
print("Failed to get storage usage details")
}
Expand All @@ -110,7 +124,7 @@ class Storage {
}


func cleanSh() {
func autoCleanSh() {
if let scriptPath = Bundle.main.path(forResource: "CleanCache_cluster", ofType: "sh") {
let task = Process()
task.launchPath = "/bin/sh"
Expand Down