Skip to content

Commit aa094b7

Browse files
committed
Today Widget auto reconnect to aria2
1 parent 487fdd2 commit aa094b7

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

Maria Widget/Base.lproj/TodayViewController.xib

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11201" systemVersion="16A319" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11762" systemVersion="16C67" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11201"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11762"/>
5+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
56
</dependencies>
67
<objects>
78
<customObject id="-2" userLabel="File's Owner" customClass="TodayViewController" customModule="Maria_Widget" customModuleProvider="target">
@@ -81,7 +82,7 @@
8182
<rect key="frame" x="79" y="73" width="157" height="19"/>
8283
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="center" title="Please run aria2 first" id="Twu-sf-eXL">
8384
<font key="font" metaFont="system" size="16"/>
84-
<color key="textColor" name="gridColor" catalog="System" colorSpace="catalog"/>
85+
<color key="textColor" red="0.37254901960784315" green="0.37254901960784315" blue="0.37254901960784315" alpha="1" colorSpace="calibratedRGB"/>
8586
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
8687
</textFieldCell>
8788
</textField>

Maria Widget/Maria.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Cocoa
1010
import Aria2RPC
1111

1212
class Maria {
13-
var rpc: Aria2RPC!
13+
var rpc: Aria2RPC?
1414

1515
static let shared = Maria()
1616

Maria Widget/TodayViewController.swift

+22-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ class TodayViewController: NSViewController, NCWidgetProviding {
4040

4141
var maria = Maria.shared
4242

43-
var timer: Timer!
43+
var timer: Timer?
44+
45+
var timeToConnectAria = 4
46+
var countdownTimeToConnectAria = 4
4447
var authorized: Bool = true
4548

4649
func widgetPerformUpdate(_ completionHandler: ((NCUpdateResult) -> Void)) {
@@ -67,9 +70,25 @@ class TodayViewController: NSViewController, NCWidgetProviding {
6770
}
6871

6972
func updateListStatus() {
70-
if maria.rpc?.status == .connected {
73+
switch maria.rpc!.status {
74+
case .disconnected:
75+
countdownTimeToConnectAria -= 1
76+
if countdownTimeToConnectAria == 0 {
77+
maria.rpc?.connect()
78+
timeToConnectAria *= 2
79+
countdownTimeToConnectAria = timeToConnectAria
80+
} else {
81+
let localized = NSLocalizedString("aria2.status.disconnected", comment: "")
82+
alertLabel.stringValue = String(format: localized, countdownTimeToConnectAria)
83+
}
84+
case .connected:
85+
timeToConnectAria = 4
86+
countdownTimeToConnectAria = 4
87+
7188
maria.rpc?.getGlobalStatus()
7289
maria.rpc?.tellActive()
90+
default:
91+
break
7392
}
7493
}
7594

@@ -120,7 +139,6 @@ class TodayViewController: NSViewController, NCWidgetProviding {
120139
self.taskListScrollViewHeightConstraint.constant = 0
121140
case .disconnected:
122141
self.noTaskAlertLabel.isHidden = true
123-
self.alertLabel.stringValue = NSLocalizedString("aria2.status.disconnected", comment: "")
124142
self.taskListScrollViewHeightConstraint.constant = 0
125143
}
126144
}
@@ -134,7 +152,7 @@ extension TodayViewController {
134152
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateListStatus), userInfo: nil, repeats: true)
135153
}
136154
fileprivate func closeTimer() {
137-
timer.invalidate()
155+
timer?.invalidate()
138156
timer = nil
139157
}
140158
}

0 commit comments

Comments
 (0)