Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  TransferWaitingController.swift
  3. //  KoinBonds
  4. //
  5. //  Created by helmi taufiq alhakim on 12/02/20.
  6. //  Copyright © 2020 helmi taufiq alhakim. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class TransferWaitingController: UIViewController, TransferProtocol {
  12.    
  13.     @IBOutlet weak var timerLabel: UILabel!
  14.     @IBOutlet weak var transferView: TransferView!
  15.     @IBOutlet weak var containerScrollView: UIScrollView!
  16.    
  17.     var currentMinute = 59
  18.     var currentSecond = 59
  19.     var currentHour = 71
  20.     private var timer = Timer()
  21.     private var timerIsOn = false
  22.     private var dateNow: Date!
  23.    
  24.     override func viewDidLoad() {
  25.         super.viewDidLoad()
  26.        
  27.         timerLabel.isHidden = false
  28.         configureSubView()
  29.         initTimer()
  30.         initTransfer()
  31.     }
  32.  
  33.     func configureSubView(){
  34.         containerScrollView.addSubView(transferView)
  35.     }
  36.    
  37.     func onClickTransfer(index: CGFloat, isExpand: Bool) {
  38.         let frame = containerScrollView.convert(transferView.contentView.frame, from: transferView)
  39.         containerScrollView.setContentOffset(CGPoint(x: 0, y: (frame.maxY - containerScrollView.bounds.size.height)), animated: true)
  40.     }
  41.    
  42.     private func initTimer() {
  43.         dateNow = Date()
  44.        
  45.         if !timerIsOn {
  46.             timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTick), userInfo: nil, repeats: true)
  47.             timerIsOn = true
  48.         }
  49.     }
  50.    
  51.     @objc func onTick() {
  52.         if ((currentHour > 0 || currentMinute > 0 || currentSecond > 0) && (currentMinute >= 0 || currentHour >= 0)) {
  53.            
  54.             if (currentSecond == 0) {
  55.                 if (currentMinute == 0) {
  56.                     currentMinute = 59
  57.                     currentHour -= 1
  58.                 } else{
  59.                     currentMinute -= 1
  60.                     currentSecond = 59
  61.                 }
  62.             }
  63.             else if (currentSecond > 0) {
  64.                 currentSecond -= 1
  65.             }
  66.             else if (currentSecond < 1) {
  67.                 timer.invalidate()
  68.             }
  69.            
  70.             if (currentHour >= 0) {
  71.                 timerLabel.text = NSLocalizedString("", comment: "") + " \(timeString(hour: TimeInterval(currentHour), minute: TimeInterval(currentMinute), second: TimeInterval(currentSecond)))"
  72.             }
  73.         }
  74.         else {
  75.             currentHour = 71
  76.             currentMinute = 59
  77.             currentSecond = 59
  78.             timer.invalidate()
  79.             timerIsOn = false
  80.             timerLabel.isHidden = true
  81.         }
  82.     }
  83.    
  84.     private func timeString(hour: TimeInterval,minute: TimeInterval, second: TimeInterval) -> String {
  85.         let hour = Int(hour)
  86.         let minute = Int(minute)  % 60
  87.         let seconds = Int(second) % 60
  88.        
  89.         return String(format: "%02i:%02i:%02i", hour, minute, seconds)
  90.     }
  91.    
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement