Created
July 21, 2016 19:27
-
-
Save karthikgs7/0e37089bc02bfcebdb3ca72d66f7ac3d to your computer and use it in GitHub Desktop.
Monitors external screen connection, notifies on connected/disconnected with the screen size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ScreenManager.swift | |
// | |
// Created by karthikeyan gm on 3/14/16. | |
// Copyright © 2016 karthikeyan g. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import RxSwift | |
import RxCocoa | |
class ScreenManager { | |
static let defaultManager = ScreenManager() | |
//MARK:- Private instance variables | |
private(set) var connected: Bool = UIScreen.screens().count > 1 | |
private (set) var screenSize: CGSize = .zero | |
private var _connected: Variable<Bool>! | |
private var _screenSize: Variable<CGSize>! | |
private let disposeBag = DisposeBag() | |
//MARK:- Public instance variables | |
var connectionNotifier: (Bool -> Void)? { | |
didSet { | |
if let _ = connectionNotifier { | |
_connected = Variable(connected) | |
startObservingScreenConnection() | |
startConnectionNotifier() | |
} else { | |
_connected = nil | |
} | |
} | |
} | |
var screenSizeNotifier: (CGSize -> Void)? { | |
didSet { | |
if let _ = screenSizeNotifier { | |
_screenSize = Variable(screenSize) | |
startObservingScreenSize() | |
startScreenSizeNotifier() | |
} else { | |
_screenSize = nil | |
} | |
} | |
} | |
private init() { | |
connected = UIScreen.screens().count > 1 | |
screenSize = connected ? UIScreen.screens()[1].bounds.size : .zero | |
} | |
} | |
private extension ScreenManager { | |
func startConnectionNotifier() { | |
_connected.asDriver().driveNext { [weak self] (value) -> Void in | |
self!.connectionNotifier?(value) | |
}.addDisposableTo(disposeBag) | |
} | |
func startScreenSizeNotifier() { | |
_screenSize.asDriver().driveNext { [weak self] (size) -> Void in | |
self!.screenSizeNotifier?(size) | |
}.addDisposableTo(disposeBag) | |
} | |
func startObservingScreenConnection() { | |
guard let _ = _connected else { | |
return | |
} | |
NSNotificationCenter.defaultCenter().rx_notification(UIScreenDidConnectNotification) | |
.observeOn(MainScheduler.instance) | |
.map { _ in true } | |
.bindTo(_connected) | |
.addDisposableTo(disposeBag) | |
NSNotificationCenter.defaultCenter().rx_notification(UIScreenDidDisconnectNotification) | |
.observeOn(MainScheduler.instance) | |
.map { _ in false } | |
.bindTo(_connected) | |
.addDisposableTo(disposeBag) | |
} | |
func startObservingScreenSize() { | |
guard let _ = _screenSize else { | |
return | |
} | |
NSNotificationCenter.defaultCenter().rx_notification(UIScreenModeDidChangeNotification) | |
.observeOn(MainScheduler.instance) | |
.map { [weak self] notification in self!.screenSizeWithNotification(notification) } | |
.bindTo(_screenSize) | |
.addDisposableTo(disposeBag) | |
NSNotificationCenter.defaultCenter().rx_notification(UIScreenDidConnectNotification) | |
.observeOn(MainScheduler.instance) | |
.map { [weak self] notification in self!.screenSizeWithNotification(notification) } | |
.bindTo(_screenSize) | |
.addDisposableTo(disposeBag) | |
NSNotificationCenter.defaultCenter().rx_notification(UIScreenDidDisconnectNotification) | |
.observeOn(MainScheduler.instance) | |
.map { [weak self] notification in self!.screenSizeWithNotification(notification) } | |
.bindTo(_screenSize) | |
.addDisposableTo(disposeBag) | |
} | |
func screenSizeWithNotification(theNotification: NSNotification?) -> CGSize { | |
guard let notification = theNotification, screen = notification.object as? UIScreen, screenMode = screen.currentMode | |
else { return .zero } | |
return screenMode.size | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment