Skip to content

Commit

Permalink
Show labels in issue and pull request cells
Browse files Browse the repository at this point in the history
  • Loading branch information
khoren93 committed May 12, 2019
1 parent 4d31077 commit 1316741
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
11 changes: 11 additions & 0 deletions SwiftHub/Extensions/UIColor/UIColor+SwiftHub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ extension UIColor {
return randomFlat
}
}

extension UIColor {

var brightnessAdjustedColor: UIColor {
var components = self.cgColor.components
let alpha = components?.last
components?.removeLast()
let color = CGFloat(1-(components?.max())! >= 0.5 ? 1.0 : 0.0)
return UIColor(red: color, green: color, blue: color, alpha: alpha!)
}
}
2 changes: 1 addition & 1 deletion SwiftHub/Models/PullRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct PullRequest: Mappable {
var htmlUrl: String?
var id: Int?
var issueUrl: String?
var labels: [Label]?
var labels: [IssueLabel]?
var locked: Bool?
var maintainerCanModify: Bool?
var mergeCommitSha: String?
Expand Down
13 changes: 12 additions & 1 deletion SwiftHub/Modules/Issues/IssueCellViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ extension Issue {
if let commentsString = comments?.string.styled( with: .color(.text())) {
let commentsImage = R.image.icon_cell_badge_comment()?.filled(withColor: .secondary()).scaled(toHeight: 15)?.styled(with: .baselineOffset(-3)) ?? NSAttributedString()
texts.append(NSAttributedString.composed(of: [
commentsImage, Special.space, commentsString
commentsImage, Special.space, commentsString, Tab.headIndent(10)
]))
}
labels?.forEach({ (label) in
if let name = label.name, let color = UIColor(hexString: label.color ?? "") {
let labelString = " \(name) ".styled(with: .color(color.darken(by: 0.35).brightnessAdjustedColor),
.backgroundColor(color),
.lineHeightMultiple(1.2),
.baselineOffset(1))
texts.append(NSAttributedString.composed(of: [
labelString, Special.space
]))
}
})
return NSAttributedString.composed(of: texts)
}
}
1 change: 0 additions & 1 deletion SwiftHub/Modules/Main/HomeTabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class HomeTabBarController: RAMAnimatedTabBarController, Navigatable {

let tabTapped = PublishSubject<UIGestureRecognizer>()


override func viewDidLoad() {
super.viewDidLoad()

Expand Down
2 changes: 1 addition & 1 deletion SwiftHub/Modules/Pull Requests/PullRequestCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PullRequestCell: DetailedTableViewCell {
func bind(to viewModel: PullRequestCellViewModel) {
viewModel.title.drive(titleLabel.rx.text).disposed(by: rx.disposeBag)
viewModel.detail.drive(detailLabel.rx.text).disposed(by: rx.disposeBag)
viewModel.secondDetail.drive(secondDetailLabel.rx.text).disposed(by: rx.disposeBag)
viewModel.secondDetail.drive(secondDetailLabel.rx.attributedText).disposed(by: rx.disposeBag)
viewModel.imageUrl.drive(leftImageView.rx.imageURL).disposed(by: rx.disposeBag)
viewModel.imageUrl.drive(onNext: { [weak self] (url) in
if let url = url {
Expand Down
33 changes: 30 additions & 3 deletions SwiftHub/Modules/Pull Requests/PullRequestCellViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import Foundation
import RxSwift
import RxCocoa
import BonMot

class PullRequestCellViewModel {

let title: Driver<String>
let detail: Driver<String>
let secondDetail: Driver<String>
let secondDetail: Driver<NSAttributedString>
let imageUrl: Driver<URL?>
let badge: Driver<UIImage?>
let badgeColor: Driver<UIColor>
Expand All @@ -26,10 +27,36 @@ class PullRequestCellViewModel {
init(with pullRequest: PullRequest) {
self.pullRequest = pullRequest
title = Driver.just("\(pullRequest.title ?? "")")
detail = Driver.just("\(pullRequest.createdAt?.toRelative() ?? "")")
secondDetail = Driver.just("#\(pullRequest.number ?? 0)")
detail = Driver.just(pullRequest.detail())
secondDetail = Driver.just(pullRequest.attributedDetail())
imageUrl = Driver.just(pullRequest.user?.avatarUrl?.url)
badge = Driver.just(R.image.icon_cell_badge_pull_request()?.template)
badgeColor = Driver.just(pullRequest.state == .open ? .flatGreenDark : .flatPurpleDark)
}
}

extension PullRequest {
func detail() -> String {
switch state {
case .open: return "#\(number ?? 0) opened \(createdAt?.toRelative() ?? "") by \(user?.login ?? "")"
case .closed: return "#\(number ?? 0) closed \(closedAt?.toRelative() ?? "") by \(user?.login ?? "")"
case .all: return ""
}
}

func attributedDetail() -> NSAttributedString {
var texts: [NSAttributedString] = []
labels?.forEach({ (label) in
if let name = label.name, let color = UIColor(hexString: label.color ?? "") {
let labelString = " \(name) ".styled(with: .color(color.darken(by: 0.35).brightnessAdjustedColor),
.backgroundColor(color),
.lineHeightMultiple(1.2),
.baselineOffset(1))
texts.append(NSAttributedString.composed(of: [
labelString, Special.space
]))
}
})
return NSAttributedString.composed(of: texts)
}
}

0 comments on commit 1316741

Please sign in to comment.