finish viewcontroller

This commit is contained in:
秦宇航
2017-07-16 21:07:31 +08:00
parent 55ef345611
commit a984a62f7e
4 changed files with 99 additions and 33 deletions
@@ -8,6 +8,8 @@
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="SubscribePreferenceWindowController" customModule="ShadowsocksX_NG" customModuleProvider="target">
<connections>
<outlet property="AddSubscribeBtn" destination="nzj-fK-Vip" id="Tlt-DM-cZF"/>
<outlet property="DeleteSubscribeBtn" destination="jSv-em-0Gp" id="uDp-Bj-Mb8"/>
<outlet property="FeedLabel" destination="uYX-0C-bOD" id="odZ-Tm-7SC"/>
<outlet property="FeedTextField" destination="p1W-Md-Wfw" id="zh0-1b-0zw"/>
<outlet property="GroupTextField" destination="MrH-Ft-1tn" id="txD-SB-Kpr"/>
@@ -47,7 +49,7 @@
</textField>
<textField verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="hnP-yo-KBb">
<rect key="frame" x="236" y="165" width="224" height="22"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="ASh-mc-Uyt">
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" placeholderString="Optional" drawsBackground="YES" id="ASh-mc-Uyt">
<font key="font" metaFont="system"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
@@ -71,6 +73,9 @@
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="onAdd:" target="-2" id="giW-QT-LEJ"/>
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="jSv-em-0Gp">
<rect key="frame" x="40" y="20" width="20" height="20"/>
@@ -81,6 +86,9 @@
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="onDelete:" target="-2" id="1OX-xh-AJb"/>
</connections>
</button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="uNY-1k-LwL">
<rect key="frame" x="182" y="205" width="30" height="17"/>
@@ -111,7 +119,7 @@
<constraints>
<constraint firstAttribute="width" constant="224" id="zre-fn-PCG"/>
</constraints>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="3kt-n3-uTF">
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" placeholderString="Optional" drawsBackground="YES" id="3kt-n3-uTF">
<font key="font" metaFont="system"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
@@ -71,6 +71,32 @@ class ServerProfileManager: NSObject {
}
}
func isExisted(profile: ServerProfile) -> (Bool, Int){
for (index, value) in profiles.enumerated() {
let ret = value.serverHost == profile.serverHost
if ret {
return (ret, index)
}
}
return (false, -1)
}
func isDuplicated(profile: ServerProfile) -> (Bool, Int){
for (index, value) in profiles.enumerated() {
let ret = value.serverHost == profile.serverHost
&& value.password == profile.password
&& value.serverPort == profile.serverPort
&& value.ssrProtocol == profile.ssrProtocol
&& value.ssrObfs == profile.ssrObfs
&& value.ssrObfsParam == value.ssrObfsParam
&& value.ssrProtocolParam == value.ssrProtocolParam
if ret {
return (ret, index)
}
}
return (false, -1)
}
func importConfigFile() {
let openPanel = NSOpenPanel()
openPanel.title = "Choose Config Json File".localized
+10 -28
View File
@@ -135,11 +135,20 @@ class Subscribe: NSObject{
let ssrregexp = "ssr://([A-Za-z0-9_-]+)"
let urls = splitor(url: decodeRes, regexp: ssrregexp)
let maxN = self.maxCount == -1 ? urls.count: self.maxCount
// TODO change the loop into random pick
for index in 0..<maxN {
let profielDict = ParseAppURLSchemes(URL(string: urls[index]))//ParseSSURL(url)
if let profielDict = profielDict {
let profile = ServerProfile.fromDictionary(profielDict as [String : AnyObject])
let (dupResult, _) = self.profileMgr.isDuplicated(profile: profile)
let (existResult, existIndex) = self.profileMgr.isExisted(profile: profile)
if dupResult {
return
}
if existResult {
return self.profileMgr.profiles.replaceSubrange(Range(existIndex..<existIndex + 1), with: [profile])
}
self.profileMgr.profiles.append(profile)
}
}
@@ -158,37 +167,10 @@ class Subscribe: NSObject{
}
func feedValidator() -> Bool{
// is the right format
return subscribeFeed != "" && groupName != "" && token != ""
return subscribeFeed != "" && groupName != ""
}
fileprivate func pushNotification(){
}
fileprivate func isExisted(profile: ServerProfile) -> Bool{
// using to url judge
// if existed update
for (_, value) in profileMgr.profiles.enumerated() {
let ret = value.serverHost == profile.serverHost
if ret {
return ret
}
}
return false
}
fileprivate func isDuplicated(profile: ServerProfile) -> Bool{
// if duplicated skip
for (_, value) in profileMgr.profiles.enumerated() {
let ret = value.serverHost == profile.serverHost
&& value.password == profile.password
&& value.serverPort == profile.serverPort
&& value.ssrProtocol == profile.ssrProtocol
&& value.ssrObfs == profile.ssrObfs
&& value.ssrObfsParam == value.ssrObfsParam
&& value.ssrProtocolParam == value.ssrProtocolParam
if ret {
return ret
}
}
return false
}
fileprivate func isSame(source: Subscribe, target: Subscribe) -> Bool {
return source.subscribeFeed == target.subscribeFeed && source.token == target.token && source.maxCount == target.maxCount
@@ -20,6 +20,8 @@ class SubscribePreferenceWindowController: NSWindowController
@IBOutlet weak var MaxCountTextField: NSTextField!
@IBOutlet weak var SubscribeTableView: NSTableView!
@IBOutlet weak var AddSubscribeBtn: NSButton!
@IBOutlet weak var DeleteSubscribeBtn: NSButton!
var sbMgr: SubscribeManager!
var defaults: UserDefaults!
@@ -42,14 +44,59 @@ class SubscribePreferenceWindowController: NSWindowController
@IBAction func onOk(_ sender: NSButton) {
// TODO append to manager and save
let subscribe = Subscribe.init(initUrlString: FeedTextField.stringValue, initGroupName: GroupTextField.stringValue, initToken: TokenTextField.stringValue, initMaxCount: MaxCountTextField.integerValue)
if subscribe.feedValidator() {
_ = sbMgr.addSubscribe(oneSubscribe: subscribe)
if !subscribe.feedValidator() {
return shakeWindows()
}
_ = sbMgr.addSubscribe(oneSubscribe: subscribe)
subscribe.updateServerFromFeed()
SubscribeTableView.reloadData()
window?.performClose(self)
}
@IBAction func onAdd(_ sender: NSButton) {
if editingSubscribe != nil && !editingSubscribe.feedValidator(){
shakeWindows()
return
}
SubscribeTableView.beginUpdates()
let subscribe = Subscribe(initUrlString: "", initGroupName: "", initToken: "", initMaxCount: -1)
sbMgr.subscribes.append(subscribe)
let index = IndexSet(integer: sbMgr.subscribes.count-1)
SubscribeTableView.insertRows(at: index, withAnimation: .effectFade)
self.SubscribeTableView.scrollRowToVisible(self.sbMgr.subscribes.count-1)
self.SubscribeTableView.selectRowIndexes(index, byExtendingSelection: false)
SubscribeTableView.endUpdates()
updateSubscribeBoxVisible()
}
@IBAction func onDelete(_ sender: NSButton) {
let index = Int(SubscribeTableView.selectedRowIndexes.first!)
var deleteCount = 0
if index >= 0 {
SubscribeTableView.beginUpdates()
for (_, toDeleteIndex) in SubscribeTableView.selectedRowIndexes.enumerated() {
print(sbMgr.subscribes.count)
sbMgr.subscribes.remove(at: toDeleteIndex - deleteCount)
SubscribeTableView.removeRows(at: IndexSet(integer: toDeleteIndex - deleteCount), withAnimation: .effectFade)
deleteCount += 1
}
SubscribeTableView.endUpdates()
}
self.SubscribeTableView.scrollRowToVisible(index - 1)
self.SubscribeTableView.selectRowIndexes(IndexSet(integer: index - 1), byExtendingSelection: false)
updateSubscribeBoxVisible()
}
func updateSubscribeBoxVisible() {
if sbMgr.subscribes.count <= 0 {
DeleteSubscribeBtn.isEnabled = false
}else{
DeleteSubscribeBtn.isEnabled = true
}
}
func bindSubscribe(_ index:Int) {
if index >= 0 && index < sbMgr.subscribes.count {
editingSubscribe = sbMgr.subscribes[index]
@@ -69,7 +116,10 @@ class SubscribePreferenceWindowController: NSWindowController
}
func getDataAtRow(_ index:Int) -> String {
return sbMgr.subscribes[index].groupName
if sbMgr.subscribes[index].groupName != "" {
return sbMgr.subscribes[index].groupName
}
return sbMgr.subscribes[index].subscribeFeed
}
// MARK: For NSTableViewDataSource