Skip to content

Commit

Permalink
Add toggle bookmark action to detail view and its peek #24
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanhuang13 committed Aug 12, 2018
1 parent 945c39d commit e82535b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]
- Scroll to top when loading another radar #19
- Radar title in the lists should show full text with multiple lines #21
- Add toggle bookmark action to detail view and its peek #24

## [1.2.0(11)] - 2018-07-26
### Added
Expand Down
2 changes: 1 addition & 1 deletion Ladybug/App/TabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension TabBarController: RadarURLOpenerUI {

if let navController = self.selectedViewController as? UINavigationController {
if let vc = navController.topViewController as? DetailViewController {
vc.load(radar: radar)
vc.load(radar: radar, scrollToTop: true)
} else {
navController.pushViewController(vc, animated: true)
}
Expand Down
32 changes: 21 additions & 11 deletions Ladybug/Detail/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ class DetailViewController: UITableViewController, TableViewControllerUsingViewM
OpenRadarAPI().fetchRadar(by: radar.number) { [weak self] (result) in
switch result {
case .value(let radar):
if RadarCollection.shared.upsert(radar: radar) {
self?.radar = radar
if RadarCollection.shared.upsert(radar: radar),
let updatedRadar = RadarCollection.shared.radar(radar.number) {
self?.load(radar: updatedRadar)
RadarCollection.shared.forceNotifyDelegates()
}
case .error(_):
Expand All @@ -60,6 +61,16 @@ class DetailViewController: UITableViewController, TableViewControllerUsingViewM
self.userActivity?.becomeCurrent()
}

func load(radar: Radar, scrollToTop: Bool = false) {
self.radar = radar

if scrollToTop {
DispatchQueue.main.async {
self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
}
}
}

func reloadData() {
/*
Ladybug Layout
Expand Down Expand Up @@ -123,6 +134,10 @@ class DetailViewController: UITableViewController, TableViewControllerUsingViewM
}

var actionRows: [TableViewCellViewModel] = []
actionRows.append(TableViewCellViewModel(title: radar.isBookmarked ? "Unbookmark".localized() : "Bookmark".localized(), selectAction: {
radar.toggleBookmark()
self.reloadData()
}))
let rdarURLString = radar.number.rdarURLString
actionRows.append(TableViewCellViewModel(title: "Copy link".localized(), subtitle: rdarURLString, cellStyle: .subtitle, selectAction: {
self.copyRdarLink()
Expand Down Expand Up @@ -152,17 +167,12 @@ class DetailViewController: UITableViewController, TableViewControllerUsingViewM
}
}

func load(radar: Radar) {
self.radar = radar

DispatchQueue.main.async {
self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
}
}

override var previewActionItems: [UIPreviewActionItem] {
var items: [UIPreviewActionItem] = []

let toggleBookmarkAction = UIPreviewAction(title: radar.isBookmarked ? "Unbookmark".localized() : "Bookmark".localized(), style: .default) { (_, _) in
self.radar.toggleBookmark()
}
let copyRdarLinkAction = UIPreviewAction(title: String(format: "Copy %@".localized(), radar.number.rdarURLString), style: .default) { (_, _) in
self.copyRdarLink()
}
Expand All @@ -179,7 +189,7 @@ class DetailViewController: UITableViewController, TableViewControllerUsingViewM
self.duplicateInBrisk()
}

items = [copyRdarLinkAction, copyOpenRadarLinkAction, openInSafariViewControllerAction, openInSafariAction]
items = [toggleBookmarkAction, copyRdarLinkAction, copyOpenRadarLinkAction, openInSafariViewControllerAction, openInSafariAction]

if RadarURLOpener.shared.canOpen(in: .briskApp) {
items.append(duplicateInBrisk)
Expand Down
11 changes: 9 additions & 2 deletions Ladybug/Extensions/Radar+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
import UIKit

extension Radar {
var isBookmarked: Bool {
return bookmarkedDate != nil
}

func toggleBookmark() {
try? RadarCollection.shared.toggleBookmark(radarNumber: self.number)
}

var toggleBookmarkAction: UIContextualAction {
let isBookmarked = bookmarkedDate != nil
let action = UIContextualAction(style: .normal, title: isBookmarked ? "Unbookmark".localized() : "Bookmark".localized(), handler: { (_, _, completion) in
try? RadarCollection.shared.toggleBookmark(radarNumber: self.number)
self.toggleBookmark()
completion(true)
})
return action
Expand Down

0 comments on commit e82535b

Please sign in to comment.