2018-08-04 21:49:32 +08:00
|
|
|
//
|
|
|
|
// ProxyMenuItemFactory.swift
|
|
|
|
// ClashX
|
|
|
|
//
|
|
|
|
// Created by CYC on 2018/8/4.
|
|
|
|
// Copyright © 2018年 west2online. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
import SwiftyJSON
|
|
|
|
import RxCocoa
|
|
|
|
|
|
|
|
class ProxyMenuItemFactory {
|
|
|
|
static func menuItems(completionHandler:@escaping (([NSMenuItem])->())){
|
|
|
|
ApiRequest.requestProxyGroupList { (res) in
|
|
|
|
let dataDict = JSON(res)
|
|
|
|
var menuItems = [NSMenuItem]()
|
|
|
|
for proxyGroup in dataDict.dictionaryValue {
|
|
|
|
guard proxyGroup.value["type"].stringValue == "Selector" else {continue}
|
|
|
|
let menu = NSMenuItem(title: proxyGroup.key, action: nil, keyEquivalent: "")
|
|
|
|
let selectedName = proxyGroup.value["now"].stringValue
|
|
|
|
let submenu = NSMenu(title: proxyGroup.key)
|
2018-08-04 22:09:11 +08:00
|
|
|
for proxy in proxyGroup.value["all"].arrayValue.reversed() {
|
2018-08-04 21:49:32 +08:00
|
|
|
let proxyItem = NSMenuItem(title: proxy.stringValue, action: #selector(ProxyMenuItemFactory.actionSelectProxy(sender:)), keyEquivalent: "")
|
|
|
|
proxyItem.target = ProxyMenuItemFactory.self
|
|
|
|
proxyItem.state = proxy.stringValue == selectedName ? .on : .off
|
|
|
|
submenu.addItem(proxyItem)
|
|
|
|
}
|
|
|
|
menu.submenu = submenu
|
|
|
|
menuItems.append(menu);
|
|
|
|
}
|
2018-08-04 22:09:11 +08:00
|
|
|
completionHandler(menuItems.reversed())
|
2018-08-04 21:49:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc static func actionSelectProxy(sender:NSMenuItem){
|
|
|
|
guard let proxyGroup = sender.menu?.title else {return}
|
|
|
|
let proxyName = sender.title
|
|
|
|
|
|
|
|
ApiRequest.updateProxyGroup(group: proxyGroup, selectProxy: proxyName) { (success) in
|
|
|
|
if (success) {
|
|
|
|
for items in sender.menu?.items ?? [NSMenuItem]() {
|
|
|
|
items.state = .off
|
|
|
|
}
|
|
|
|
sender.state = .on
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|