Skip to content

Commit

Permalink
Add SimpleMusicPlayerUserDefaults
Browse files Browse the repository at this point in the history
Issue #204
  • Loading branch information
eofster committed Aug 26, 2016
1 parent 7bd60af commit dd96837
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 5 deletions.
26 changes: 25 additions & 1 deletion Telephone.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@
8AA748A91D7022BA000587DC /* MusicPlayerSpy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AA748A81D7022BA000587DC /* MusicPlayerSpy.swift */; };
8AA748AB1D7023B5000587DC /* MusicPlayerUserDefaultsFake.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AA748AA1D7023B5000587DC /* MusicPlayerUserDefaultsFake.swift */; };
8AA748AD1D702459000587DC /* MusicPlayerUserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AA748AC1D702459000587DC /* MusicPlayerUserDefaults.swift */; };
8AA748B51D70460B000587DC /* SimpleMusicPlayerUserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AA748B01D703ED3000587DC /* SimpleMusicPlayerUserDefaults.swift */; };
8AA748B61D704657000587DC /* SimpleMusicPlayerUserDefaultsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AA748B21D703F45000587DC /* SimpleMusicPlayerUserDefaultsTests.swift */; };
8AA791FC1CE245940082E4FC /* AsyncProductsFake.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AA791FB1CE245940082E4FC /* AsyncProductsFake.swift */; };
8AAACA011CAE9EE5001930C4 /* SoundIO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AAACA001CAE9EE5001930C4 /* SoundIO.swift */; };
8AAACA031CAE9F36001930C4 /* SimpleSoundIO.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AAACA021CAE9F36001930C4 /* SimpleSoundIO.swift */; };
Expand Down Expand Up @@ -718,6 +720,8 @@
8AA748A81D7022BA000587DC /* MusicPlayerSpy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MusicPlayerSpy.swift; sourceTree = "<group>"; };
8AA748AA1D7023B5000587DC /* MusicPlayerUserDefaultsFake.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MusicPlayerUserDefaultsFake.swift; sourceTree = "<group>"; };
8AA748AC1D702459000587DC /* MusicPlayerUserDefaults.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MusicPlayerUserDefaults.swift; sourceTree = "<group>"; };
8AA748B01D703ED3000587DC /* SimpleMusicPlayerUserDefaults.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimpleMusicPlayerUserDefaults.swift; sourceTree = "<group>"; };
8AA748B21D703F45000587DC /* SimpleMusicPlayerUserDefaultsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SimpleMusicPlayerUserDefaultsTests.swift; path = UseCasesTests/SimpleMusicPlayerUserDefaultsTests.swift; sourceTree = SOURCE_ROOT; };
8AA791FB1CE245940082E4FC /* AsyncProductsFake.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AsyncProductsFake.swift; sourceTree = "<group>"; };
8AAACA001CAE9EE5001930C4 /* SoundIO.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SoundIO.swift; sourceTree = "<group>"; };
8AAACA021CAE9F36001930C4 /* SimpleSoundIO.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimpleSoundIO.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1568,14 +1572,32 @@
sourceTree = "<group>";
};
8AA7489F1D701DEC000587DC /* Music Player */ = {
isa = PBXGroup;
children = (
8AA748B71D70472E000587DC /* MusicPlayer */,
8AA748B81D704742000587DC /* MusicPlayerUserDefaults */,
);
name = "Music Player";
sourceTree = "<group>";
};
8AA748B71D70472E000587DC /* MusicPlayer */ = {
isa = PBXGroup;
children = (
8AA748A31D70220D000587DC /* ConditionalMusicPlayer.swift */,
8AA748A51D70221F000587DC /* ConditionalMusicPlayerTests.swift */,
8AA748901D6F40FD000587DC /* MusicPlayer.h */,
);
name = MusicPlayer;
sourceTree = "<group>";
};
8AA748B81D704742000587DC /* MusicPlayerUserDefaults */ = {
isa = PBXGroup;
children = (
8AA748B01D703ED3000587DC /* SimpleMusicPlayerUserDefaults.swift */,
8AA748B21D703F45000587DC /* SimpleMusicPlayerUserDefaultsTests.swift */,
8AA748AC1D702459000587DC /* MusicPlayerUserDefaults.swift */,
);
name = "Music Player";
name = MusicPlayerUserDefaults;
sourceTree = "<group>";
};
8AD6597D1D37C95E008A1007 /* StoreViewEventTarget */ = {
Expand Down Expand Up @@ -2670,6 +2692,7 @@
8ADD6A4B1CDCF75D001EDBBA /* StoreUseCaseFactory.swift in Sources */,
8AC1A4411C67BFA8007778A2 /* SoundEventTarget.swift in Sources */,
8AE396EE1D6C873300DCFB9E /* PurchaseReminderUseCase.swift in Sources */,
8AA748B51D70460B000587DC /* SimpleMusicPlayerUserDefaults.swift in Sources */,
AA3905471BFFDE9A005A8AA3 /* KeyValueUserDefaults.swift in Sources */,
AA3905441BFFDE75005A8AA3 /* UserAgentEventTargets.swift in Sources */,
AA7F6D471C0381C00064DA3A /* UserDefaultsSoundIOLoadUseCase.swift in Sources */,
Expand Down Expand Up @@ -2707,6 +2730,7 @@
buildActionMask = 2147483647;
files = (
8ADD31CC1CC165B5008C083C /* DelayingUserAgentSoundIOSelectionUseCaseTests.swift in Sources */,
8AA748B61D704657000587DC /* SimpleMusicPlayerUserDefaultsTests.swift in Sources */,
8A32A4EA1D675A300086B49D /* PurchaseCheckUseCaseTests.swift in Sources */,
8AE396EC1D6C86E000DCFB9E /* PurchaseReminderUseCaseTests.swift in Sources */,
AA39053C1BFFDDED005A8AA3 /* PreferredSoundIOTests.swift in Sources */,
Expand Down
1 change: 0 additions & 1 deletion Telephone/AppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ + (void)initialize {
defaultsDict[kTransportPublicHost] = @"";
defaultsDict[kRingingSound] = @"Purr";
defaultsDict[kSignificantPhoneNumberLength] = @9;
defaultsDict[kPauseITunes] = @YES;
defaultsDict[kAutoCloseCallWindow] = @NO;
defaultsDict[kAutoCloseMissedCallWindow] = @NO;
defaultsDict[kCallWaiting] = @YES;
Expand Down
1 change: 0 additions & 1 deletion Telephone/UserDefaultsKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ extern NSString * const kOutboundProxyPort;
extern NSString * const kUseICE;
extern NSString * const kUseDNSSRV;
extern NSString * const kSignificantPhoneNumberLength;
extern NSString * const kPauseITunes;
extern NSString * const kAutoCloseCallWindow;
extern NSString * const kAutoCloseMissedCallWindow;
extern NSString * const kCallWaiting;
Expand Down
1 change: 0 additions & 1 deletion Telephone/UserDefaultsKeys.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
NSString * const kUseICE = @"UseICE";
NSString * const kUseDNSSRV = @"UseDNSSRV";
NSString * const kSignificantPhoneNumberLength = @"SignificantPhoneNumberLength";
NSString * const kPauseITunes = @"PauseITunes";
NSString * const kAutoCloseCallWindow = @"AutoCloseCallWindow";
NSString * const kAutoCloseMissedCallWindow = @"AutoCloseMissedCallWindow";
NSString * const kCallWaiting = @"CallWaiting";
Expand Down
7 changes: 6 additions & 1 deletion UseCases/KeyValueUserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import Foundation
subscript(key: String) -> String? { get set }
func stringForKey(key: String) -> String?

func setBool(value: Bool, forKey key: String)
func boolForKey(key: String) -> Bool

func setArray(array: [AnyObject], forKey key: String)
func arrayForKey(defaultName: String) -> [AnyObject]?
func arrayForKey(key: String) -> [AnyObject]?

func registerDefaults(defaults: [String: AnyObject])
}
39 changes: 39 additions & 0 deletions UseCases/SimpleMusicPlayerUserDefaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// SimpleMusicPlayerUserDefaults.swift
// Telephone
//
// Copyright (c) 2008-2016 Alexey Kuznetsov
// Copyright (c) 2016 64 Characters
//
// Telephone is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Telephone is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//

public final class SimpleMusicPlayerUserDefaults {
private let defaults: KeyValueUserDefaults

public init(defaults: KeyValueUserDefaults) {
self.defaults = defaults
defaults.registerDefaults([key: true])
}
}

extension SimpleMusicPlayerUserDefaults: MusicPlayerUserDefaults {
public var shouldPause: Bool {
get {
return defaults.boolForKey(key)
}
set {
defaults.setBool(newValue, forKey: key)
}
}
}

private let key = "PauseITunes"
20 changes: 20 additions & 0 deletions UseCasesTestDoubles/UserDefaultsFake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ public final class UserDefaultsFake {
public var date: NSDate = NSDate.distantPast()
public var version = ""

public var registeredDefaults: [String: AnyObject] {
return registered
}

private var dictionary: [String: AnyObject] = [:]
private var registered: [String: AnyObject] = [:]

public init() {}
}
Expand All @@ -42,13 +47,28 @@ extension UserDefaultsFake: KeyValueUserDefaults {
return dictionary[key] as? String
}

@objc public func setBool(value: Bool, forKey key: String) {
dictionary[key] = value
}

@objc public func boolForKey(key: String) -> Bool {
return dictionary[key] as? Bool ?? false
}

@objc public func setArray(array: [AnyObject], forKey key: String) {
dictionary[key] = array
}

@objc public func arrayForKey(key: String) -> [AnyObject]? {
return dictionary[key] as? [AnyObject]
}

@objc public func registerDefaults(defaults: [String : AnyObject]) {
for (key, value) in defaults {
registered.updateValue(value, forKey: key)
dictionary.updateValue(value, forKey: key)
}
}
}

extension UserDefaultsFake: PurchaseReminderUserDefaults {}
49 changes: 49 additions & 0 deletions UseCasesTests/SimpleMusicPlayerUserDefaultsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// SimpleMusicPlayerUserDefaultsTests.swift
// Telephone
//
// Copyright (c) 2008-2016 Alexey Kuznetsov
// Copyright (c) 2016 64 Characters
//
// Telephone is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Telephone is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//

import UseCases
import UseCasesTestDoubles
import XCTest

final class SimpleMusicPlayerUserDefaultsTests: XCTestCase {
func testGetsFromUserDefaultsWithExpectedKey() {
let defaults = UserDefaultsFake()
defaults.setBool(true, forKey: key)
let sut = SimpleMusicPlayerUserDefaults(defaults: defaults)

XCTAssertTrue(sut.shouldPause)
}

func testSetsToUserDefaultsWithExpectedKey() {
let defaults = UserDefaultsFake()
let sut = SimpleMusicPlayerUserDefaults(defaults: defaults)

sut.shouldPause = true

XCTAssertTrue(defaults.boolForKey(key))
}

func testRegistersDefaults() {
let defaults = UserDefaultsFake()
_ = SimpleMusicPlayerUserDefaults(defaults: defaults)

XCTAssertTrue(defaults.registeredDefaults[key] as! Bool)
}
}

private let key = "PauseITunes"

0 comments on commit dd96837

Please sign in to comment.