diff --git a/SwiftHub/Extensions/UIColor/UIColor+SwiftHub.swift b/SwiftHub/Extensions/UIColor/UIColor+SwiftHub.swift index 8332324f..27615c19 100644 --- a/SwiftHub/Extensions/UIColor/UIColor+SwiftHub.swift +++ b/SwiftHub/Extensions/UIColor/UIColor+SwiftHub.swift @@ -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!) + } +} diff --git a/SwiftHub/Models/PullRequest.swift b/SwiftHub/Models/PullRequest.swift index c0a78e67..9522e601 100644 --- a/SwiftHub/Models/PullRequest.swift +++ b/SwiftHub/Models/PullRequest.swift @@ -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? diff --git a/SwiftHub/Modules/Issues/IssueCellViewModel.swift b/SwiftHub/Modules/Issues/IssueCellViewModel.swift index 8e3284f8..beaedbb8 100644 --- a/SwiftHub/Modules/Issues/IssueCellViewModel.swift +++ b/SwiftHub/Modules/Issues/IssueCellViewModel.swift @@ -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) } } diff --git a/SwiftHub/Modules/Main/HomeTabBarController.swift b/SwiftHub/Modules/Main/HomeTabBarController.swift index 34c4bc0c..d06ed4e9 100644 --- a/SwiftHub/Modules/Main/HomeTabBarController.swift +++ b/SwiftHub/Modules/Main/HomeTabBarController.swift @@ -100,7 +100,6 @@ class HomeTabBarController: RAMAnimatedTabBarController, Navigatable { let tabTapped = PublishSubject() - override func viewDidLoad() { super.viewDidLoad() diff --git a/SwiftHub/Modules/Pull Requests/PullRequestCell.swift b/SwiftHub/Modules/Pull Requests/PullRequestCell.swift index c0470b65..7e1f0c33 100644 --- a/SwiftHub/Modules/Pull Requests/PullRequestCell.swift +++ b/SwiftHub/Modules/Pull Requests/PullRequestCell.swift @@ -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 { diff --git a/SwiftHub/Modules/Pull Requests/PullRequestCellViewModel.swift b/SwiftHub/Modules/Pull Requests/PullRequestCellViewModel.swift index 8d468774..25f11586 100644 --- a/SwiftHub/Modules/Pull Requests/PullRequestCellViewModel.swift +++ b/SwiftHub/Modules/Pull Requests/PullRequestCellViewModel.swift @@ -9,12 +9,13 @@ import Foundation import RxSwift import RxCocoa +import BonMot class PullRequestCellViewModel { let title: Driver let detail: Driver - let secondDetail: Driver + let secondDetail: Driver let imageUrl: Driver let badge: Driver let badgeColor: Driver @@ -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) + } +}