Skip to content

Commit 6b878a2

Browse files
author
Chris Jeong
authored
Feature/prevent sleep (#6)
Prevent sleep only when there's something to download
1 parent a24b31d commit 6b878a2

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

NicoDownloader.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
476658D2A46286BC3170742D /* Pods_NicoDownloaderTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B2992436EA5924EDB8CFAADF /* Pods_NicoDownloaderTests.framework */; };
1111
572B875C1E332A79008BC5AE /* ProgressViewController+Tableview.swift in Sources */ = {isa = PBXBuildFile; fileRef = 572B875B1E332A79008BC5AE /* ProgressViewController+Tableview.swift */; };
1212
572B875E1E332AB3008BC5AE /* ProgressViewController+Download.swift in Sources */ = {isa = PBXBuildFile; fileRef = 572B875D1E332AB3008BC5AE /* ProgressViewController+Download.swift */; };
13+
573B9A0D1E3DC755001C4F5D /* PowerManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 573B9A0C1E3DC755001C4F5D /* PowerManager.swift */; };
1314
575D45CB1E3093B000F9E623 /* ProgressTableCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 575D45CA1E3093B000F9E623 /* ProgressTableCellView.swift */; };
1415
575D45D11E30A80F00F9E623 /* Item.swift in Sources */ = {isa = PBXBuildFile; fileRef = 575D45D01E30A80F00F9E623 /* Item.swift */; };
1516
5784FDBB1E3075AC004BD160 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5784FDBA1E3075AC004BD160 /* AppDelegate.swift */; };
@@ -37,6 +38,7 @@
3738
/* Begin PBXFileReference section */
3839
572B875B1E332A79008BC5AE /* ProgressViewController+Tableview.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "ProgressViewController+Tableview.swift"; path = "NicoDownloader/Extensions/ProgressViewController+Tableview.swift"; sourceTree = SOURCE_ROOT; };
3940
572B875D1E332AB3008BC5AE /* ProgressViewController+Download.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "ProgressViewController+Download.swift"; path = "NicoDownloader/Extensions/ProgressViewController+Download.swift"; sourceTree = SOURCE_ROOT; };
41+
573B9A0C1E3DC755001C4F5D /* PowerManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PowerManager.swift; sourceTree = "<group>"; };
4042
575D45CA1E3093B000F9E623 /* ProgressTableCellView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProgressTableCellView.swift; sourceTree = "<group>"; };
4143
575D45D01E30A80F00F9E623 /* Item.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Item.swift; path = Models/Item.swift; sourceTree = "<group>"; };
4244
5784FDB71E3075AB004BD160 /* NicoDownloader.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NicoDownloader.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -89,6 +91,14 @@
8991
name = Frameworks;
9092
sourceTree = "<group>";
9193
};
94+
573B9A0A1E3DC731001C4F5D /* Utils */ = {
95+
isa = PBXGroup;
96+
children = (
97+
573B9A0C1E3DC755001C4F5D /* PowerManager.swift */,
98+
);
99+
path = Utils;
100+
sourceTree = "<group>";
101+
};
92102
575D45CC1E30A6A500F9E623 /* ViewControllers */ = {
93103
isa = PBXGroup;
94104
children = (
@@ -138,6 +148,7 @@
138148
5784FDB91E3075AB004BD160 /* NicoDownloader */ = {
139149
isa = PBXGroup;
140150
children = (
151+
573B9A0A1E3DC731001C4F5D /* Utils */,
141152
57F30F721E30E6DC00BB2445 /* Extensions */,
142153
575D45CF1E30A76A00F9E623 /* Models */,
143154
575D45CD1E30A6F900F9E623 /* UI */,
@@ -388,6 +399,7 @@
388399
572B875C1E332A79008BC5AE /* ProgressViewController+Tableview.swift in Sources */,
389400
575D45D11E30A80F00F9E623 /* Item.swift in Sources */,
390401
5784FDD81E307B49004BD160 /* ProgressViewController.swift in Sources */,
402+
573B9A0D1E3DC755001C4F5D /* PowerManager.swift in Sources */,
391403
57F30F741E30E72D00BB2445 /* Date+XMLPubDate.swift in Sources */,
392404
5784FDBD1E3075AC004BD160 /* ViewController.swift in Sources */,
393405
575D45CB1E3093B000F9E623 /* ProgressTableCellView.swift in Sources */,

NicoDownloader/Extensions/ProgressViewController+Download.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ extension ProgressViewController {
125125
})
126126
}.then { _ -> Void in
127127
self.items[idx].status = .done
128+
self.togglePreventSleep()
128129
semaphore.signal()
129130
DispatchQueue.main.async {
130131
self.downloadProgressTableView.reloadData()
@@ -135,6 +136,8 @@ extension ProgressViewController {
135136
}
136137
}.catch { error in
137138
self.items[idx].status = .error
139+
semaphore.signal()
140+
self.togglePreventSleep()
138141
DispatchQueue.main.async {
139142
self.downloadProgressTableView.reloadData()
140143
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// PowerManager.swift
3+
// NicoDownloader
4+
//
5+
// Created by Jeong on 2017. 1. 29..
6+
// Copyright © 2017년 Jeong. All rights reserved.
7+
//
8+
9+
import CoreFoundation
10+
import Foundation
11+
import IOKit.pwr_mgt
12+
13+
let kIOPMAssertionTypeNoDisplaySleep = "PreventUserIdleDisplaySleep" as CFString
14+
15+
class PowerManager {
16+
17+
var powerAssertion: IOReturn = -100
18+
var powerId: IOPMAssertionID = IOPMAssertionID(0)
19+
20+
func preventSleep(time: NSInteger = 0) {
21+
if powerAssertion == kIOReturnSuccess {
22+
print("Sleep already prevented; releasing existing assertion first.")
23+
releaseSleepAssertion()
24+
}
25+
26+
powerAssertion = IOPMAssertionCreateWithName(
27+
kIOPMAssertionTypeNoDisplaySleep,
28+
IOPMAssertionLevel(kIOPMAssertionLevelOn),
29+
"Keep screen on for set time" as CFString!,
30+
&powerId
31+
)
32+
33+
if powerAssertion == kIOReturnSuccess {
34+
if time != 0 {
35+
print("Disable screen sleep for %i minute(s)", time)
36+
} else {
37+
print("Disable screen sleep indefinitely")
38+
}
39+
}
40+
}
41+
42+
func releaseSleepAssertion() {
43+
print("Enable display sleep")
44+
IOPMAssertionRelease(powerId)
45+
}
46+
47+
}

NicoDownloader/ViewControllers/ProgressViewController.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ class ProgressViewController: NSViewController {
3333
var semaphore: DispatchSemaphore?
3434
var allDone: Bool {
3535
get {
36-
return self.items.reduce(true) { $0.0 && ($0.1.status == .done) }
36+
return self.items.reduce(true) { $0.0 && ($0.1.status == .done || $0.1.status == .error) }
3737
}
3838
}
3939

4040
let cookies = HTTPCookieStorage.shared
41+
let powerManager = PowerManager()
4142

4243
override func viewDidLoad() {
4344
super.viewDidLoad()
@@ -127,3 +128,19 @@ extension ProgressViewController: NSWindowDelegate {
127128
dismissViewController(self)
128129
}
129130
}
131+
132+
extension ProgressViewController {
133+
override func viewWillAppear() {
134+
super.viewWillAppear()
135+
powerManager.preventSleep()
136+
}
137+
138+
override func viewWillDisappear() {
139+
super.viewWillDisappear()
140+
powerManager.releaseSleepAssertion()
141+
}
142+
143+
func togglePreventSleep() {
144+
!allDone ? powerManager.preventSleep() : powerManager.releaseSleepAssertion()
145+
}
146+
}

0 commit comments

Comments
 (0)