From 2ac74e947af96b3d9430c25e9c31b907a692278f Mon Sep 17 00:00:00 2001 From: Kazumasa Shimomura Date: Sun, 19 Aug 2018 02:16:09 +0900 Subject: [PATCH] fixed --- SimpleWallet/AppDelegate.swift | 2 +- SimpleWallet/Modules/Models/Post.swift | 15 +++++ .../DetailViewController.storyboard | 31 ++++++---- .../DetailView/DetailViewController.swift | 58 +++++++++++++------ .../ViewControllers/Top/TopTableViewCell.xib | 16 ++--- .../Top/TopTableViewController.swift | 5 +- 6 files changed, 87 insertions(+), 40 deletions(-) diff --git a/SimpleWallet/AppDelegate.swift b/SimpleWallet/AppDelegate.swift index d27a68a..15ac0ac 100644 --- a/SimpleWallet/AppDelegate.swift +++ b/SimpleWallet/AppDelegate.swift @@ -21,7 +21,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { window = UIWindow() window?.makeKeyAndVisible() let tabBarController = UITabBarController() - let viewControllers = [TopTableViewController.make(), + let viewControllers = [ UINavigationController(rootViewController: TopTableViewController.make()), WalletViewController.make(), HomeViewController.make()] viewControllers.forEach { diff --git a/SimpleWallet/Modules/Models/Post.swift b/SimpleWallet/Modules/Models/Post.swift index 2fe6cea..4d25ea5 100644 --- a/SimpleWallet/Modules/Models/Post.swift +++ b/SimpleWallet/Modules/Models/Post.swift @@ -20,4 +20,19 @@ struct Post { let deadline: Date var voteCount = 0 + + static let dateFormatter: DateFormatter = { + let df = DateFormatter() + df.dateFormat = "MM/dd HH:mm:ss" + return df + }() +} + +extension Array where Element == Post.Choice { + func get(at index: Int) -> Element? { + if index < count { + return self[index] + } + return nil + } } diff --git a/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.storyboard b/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.storyboard index 7988398..906fcab 100644 --- a/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.storyboard +++ b/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.storyboard @@ -19,32 +19,32 @@ - + - + @@ -92,7 +92,16 @@ + + + + + + + + + diff --git a/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.swift b/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.swift index 8721625..c6bc531 100644 --- a/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.swift +++ b/SimpleWallet/Modules/ViewControllers/DetailView/DetailViewController.swift @@ -10,11 +10,24 @@ import UIKit import BitcoinKit class DetailViewController: UIViewController { - + @IBOutlet weak var userNameLabel: UILabel! + @IBOutlet weak var dateLabel: UILabel! + @IBOutlet weak var descriptionLabel: UILabel! @IBOutlet weak var commentText: UITextField! - - static func make() -> DetailViewController { - return R.storyboard.detailViewController.instantiateInitialViewController()! + @IBOutlet weak var choice1Button: UIButton! + @IBOutlet weak var choice2Button: UIButton! + @IBOutlet weak var choice3Button: UIButton! + @IBOutlet weak var choice4Button: UIButton! + @IBOutlet weak var voteCountLabel: UILabel! + @IBOutlet weak var remainDateLabel: UILabel! + + private var post: Post! + private var updateTimer: Timer! + + static func make(post: Post) -> DetailViewController { + let vc = R.storyboard.detailViewController.instantiateInitialViewController()! + vc.post = post + return vc } @IBAction func voteAction(_ sender: Any) { @@ -141,24 +154,33 @@ class DetailViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - - // Do any additional setup after loading the view. + setup(post: post) + updateTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] _ in + guard let me = self else { return } + me.setup(post: me.post) + } } - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } - + func setup(post: Post) { + userNameLabel.text = post.userName - /* - // MARK: - Navigation + let diff = Int(post.deadline.timeIntervalSince(Date())) + let day = diff/24/60/60 + let hour = diff/60/60 + let min = diff/60 - hour*60 + let sec = diff - min*60 - hour*60*60 - // In a storyboard-based application, you will often want to do a little preparation before navigation - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { - // Get the new view controller using segue.destinationViewController. - // Pass the selected object to the new view controller. + dateLabel.text = Post.dateFormatter.string(from: post.createdAt) + descriptionLabel.text = post.description + remainDateLabel.text = "残り\(day)日と\(hour)時間\(min)分\(sec)秒" + choice1Button.setTitle(post.choices.get(at: 0)?.description, for: .normal) + choice2Button.setTitle(post.choices.get(at: 1)?.description, for: .normal) + choice3Button.setTitle(post.choices.get(at: 2)?.description, for: .normal) + choice4Button.setTitle(post.choices.get(at: 3)?.description, for: .normal) } - */ + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + updateTimer.invalidate() + } } diff --git a/SimpleWallet/Modules/ViewControllers/Top/TopTableViewCell.xib b/SimpleWallet/Modules/ViewControllers/Top/TopTableViewCell.xib index 0c2c84b..294c562 100644 --- a/SimpleWallet/Modules/ViewControllers/Top/TopTableViewCell.xib +++ b/SimpleWallet/Modules/ViewControllers/Top/TopTableViewCell.xib @@ -14,27 +14,27 @@ - + - + - + diff --git a/SimpleWallet/Modules/ViewControllers/Top/TopTableViewController.swift b/SimpleWallet/Modules/ViewControllers/Top/TopTableViewController.swift index 449ac71..cf5aa9f 100644 --- a/SimpleWallet/Modules/ViewControllers/Top/TopTableViewController.swift +++ b/SimpleWallet/Modules/ViewControllers/Top/TopTableViewController.swift @@ -45,7 +45,8 @@ class TopTableViewController: UITableViewController { } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - let item = posts[indexPath.row] - _ = item + let post = posts[indexPath.row] + let vc = DetailViewController.make(post: post) + navigationController?.pushViewController(vc, animated: true) } }