Skip to content

Commit 1f74c80

Browse files
committed
[Bookmarks] Move Functionality into BookmarkService
1 parent 905fc19 commit 1f74c80

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

SwiftFlowGitHubBrowser.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
254B3BA21D3ABC1300B1E4F0 /* ReSwiftRouter.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 254B3B401D3ABBAB00B1E4F0 /* ReSwiftRouter.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
2121
254B3BA51D3ABC1E00B1E4F0 /* SSKeychain.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 254B3B251D3ABB9400B1E4F0 /* SSKeychain.framework */; };
2222
254B3BA61D3ABC1E00B1E4F0 /* SSKeychain.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 254B3B251D3ABB9400B1E4F0 /* SSKeychain.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
23+
2574204D1D4421410002D020 /* BookmarkService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2574204C1D4421410002D020 /* BookmarkService.swift */; };
2324
258184351C3CC710008E200A /* State.swift in Sources */ = {isa = PBXBuildFile; fileRef = 258184341C3CC710008E200A /* State.swift */; };
2425
2581843A1C3CC912008E200A /* Routes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 258184391C3CC912008E200A /* Routes.swift */; };
2526
25BB76211C3CB8CB008EA13B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 25BB76201C3CB8CB008EA13B /* AppDelegate.swift */; };
@@ -503,6 +504,7 @@
503504
254B3B4D1D3ABBB400B1E4F0 /* ReSwift.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = ReSwift.xcodeproj; path = Carthage/Checkouts/ReSwift/ReSwift.xcodeproj; sourceTree = SOURCE_ROOT; };
504505
254B3B651D3ABBC400B1E4F0 /* RequestKit.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RequestKit.xcodeproj; path = Carthage/Checkouts/RequestKit/RequestKit.xcodeproj; sourceTree = SOURCE_ROOT; };
505506
254B3B7D1D3ABBCE00B1E4F0 /* OctoKit.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = OctoKit.xcodeproj; path = Carthage/Checkouts/octokit.swift/OctoKit.xcodeproj; sourceTree = SOURCE_ROOT; };
507+
2574204C1D4421410002D020 /* BookmarkService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BookmarkService.swift; sourceTree = "<group>"; };
506508
258184341C3CC710008E200A /* State.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = State.swift; sourceTree = "<group>"; };
507509
258184391C3CC912008E200A /* Routes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Routes.swift; sourceTree = "<group>"; };
508510
25BB761D1C3CB8CB008EA13B /* SwiftFlowGitHubBrowser.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftFlowGitHubBrowser.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -701,6 +703,7 @@
701703
isa = PBXGroup;
702704
children = (
703705
25BE03E01C7C07E6005EBB09 /* AuthenticationService.swift */,
706+
2574204C1D4421410002D020 /* BookmarkService.swift */,
704707
);
705708
path = Services;
706709
sourceTree = "<group>";
@@ -1362,6 +1365,7 @@
13621365
62EF0A701C700AD500D13711 /* MainViewController.swift in Sources */,
13631366
6296CA0E1C4D777800A60ECD /* Credentials.swift in Sources */,
13641367
25BE03E11C7C07E6005EBB09 /* AuthenticationService.swift in Sources */,
1368+
2574204D1D4421410002D020 /* BookmarkService.swift in Sources */,
13651369
62C9357B1C8CDBDE00887A23 /* RepositoryDetailViewController.swift in Sources */,
13661370
25C00E9C1C3CDAAC0074655A /* AuthenticationState.swift in Sources */,
13671371
25C00E9E1C3CDB5D0074655A /* AppReducer.swift in Sources */,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// BookmarkService.swift
3+
// SwiftFlowGitHubBrowser
4+
//
5+
// Created by Benji Encz on 7/23/16.
6+
// Copyright © 2016 Benji Encz. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import OctoKit
11+
import ReSwiftRouter
12+
13+
class BookmarkService {
14+
static func isRepositoryBookmarked(state: State, currentRepository: Repository) -> Bool {
15+
let bookmarkActive = !state.bookmarks.contains { route, data in
16+
guard let repository = data as? Repository else { return false }
17+
18+
return RouteHash(route: route) == RouteHash(route: [mainViewRoute, repositoryDetailRoute])
19+
&& repository.name == currentRepository.name
20+
}
21+
22+
return bookmarkActive
23+
}
24+
}

SwiftFlowGitHubBrowser/ViewControllers/RepositoryDetailViewController.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,17 @@ class RepositoryDetailViewController: UIViewController, StoreSubscriber {
3232
super.viewWillAppear(animated)
3333

3434
store.subscribe(self) { state in
35-
(
36-
state.navigationState.getRouteSpecificState(state.navigationState.route),
37-
state.bookmarks
35+
let currentRepository: Repository? = state.navigationState.getRouteSpecificState(
36+
state.navigationState.route
37+
)
38+
39+
let isCurrentRepositoryBookmarked = currentRepository.map {
40+
BookmarkService.isRepositoryBookmarked(state, currentRepository: $0)
41+
} ?? false
42+
43+
return (
44+
currentRepository,
45+
isCurrentRepositoryBookmarked
3846
)
3947
}
4048
}
@@ -45,7 +53,7 @@ class RepositoryDetailViewController: UIViewController, StoreSubscriber {
4553
store.unsubscribe(self)
4654
}
4755

48-
func newState(state: (selectedRepository: Repository?, bookmarks: [Bookmark])) {
56+
func newState(state: (selectedRepository: Repository?, isBookmarked: Bool)) {
4957
// Only perform repository related updates if the repository actually changed
5058
if self.repository?.gitURL != state.selectedRepository?.gitURL {
5159
self.repository = state.selectedRepository
@@ -57,14 +65,7 @@ class RepositoryDetailViewController: UIViewController, StoreSubscriber {
5765
}
5866
}
5967

60-
let bookmarkActive = !state.bookmarks.contains { route, data in
61-
guard let repository = data as? Repository else { return false }
62-
63-
return RouteHash(route: route) == RouteHash(route: [mainViewRoute, repositoryDetailRoute])
64-
&& repository.name == self.repository?.name
65-
}
66-
67-
self.bookmarkButton.enabled = bookmarkActive
68+
self.bookmarkButton.enabled = state.isBookmarked
6869
}
6970

7071
@IBAction func bookmarkButtonTapped(sender: AnyObject) {

0 commit comments

Comments
 (0)