POST Alamofire Date and Gender


ScreenShot



All code,

note:

 d: day, M: month, y: year
h: hour, m: minute, s: second

import Alamofire
import UIKit

class GenderViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet var bPriaL: UIButton!
    @IBOutlet var bWanitaL: UIButton!
    @IBOutlet var TextTanggal: UITextField!
    
    var value = 2
    let picker = UIDatePicker()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        print(value)
        datePickerValueChanged()
        
        let toolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 30))
        let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let doneBtn: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneButtonAction))
        toolbar.setItems([flexSpace, doneBtn], animated: false)
        toolbar.sizeToFit()
    }
    
    @IBAction func bLaki(_ sender: Any) {
        value = 0
        bPriaL.backgroundColor = #colorLiteral(red: 0.4663587213, green: 0.7125512958, blue: 0.102428861, alpha: 1)
        bWanitaL.backgroundColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
    }
    
    @IBAction func bWani(_ sender: Any) {
        value = 1
        bWanitaL.backgroundColor = #colorLiteral(red: 0.4663587213, green: 0.7125512958, blue: 0.102428861, alpha: 1)
        bPriaL.backgroundColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
    }
    
    @IBAction func bPriaWanita(_ sender: UIButton) {
        if value == 2 {
            let alert = UIAlertController(title: " ", message: "Silahkan lengkapi data terlebih dahulu", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Close", style: .default, handler: { (_: UIAlertAction) in
                
            }))
            
            present(alert, animated: true, completion: nil)
            print("need text")
            
        } else {
            DispatchQueue.main.async {
                let headers = ["Authorization": "Token"]
                let parameters = ["gender": self.value, "tanggal_lahir": UserDefaults.standard.string(forKey: "date") ?? ""] as [String : Any]
                
                let urlString = "https://yourAPi"
                
                Alamofire.request(urlString, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
                    if response.result.isFailure {
                        print("no data!")
                    } else {
                        print(response.result.value!)
                    }
                }
                
                print(parameters)
            }
        }
    }
    
    @objc func doneButtonAction() {
        view.endEditing(true)
    }
    
    func datePickerValueChanged() {
        let loc = Locale(identifier: "id")
        let toolBar = UIToolbar()
        toolBar.sizeToFit()
        
        TextTanggal.inputAccessoryView = toolBar
        TextTanggal.inputView = picker
        picker.datePickerMode = .date
        picker.locale = loc
        
        let done = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
        toolBar.setItems([done], animated: true)
    }
    
    @objc func donePressed() {
        let format = DateFormatter()
        format.dateStyle = .medium
        format.timeStyle = .none
        let dateString = format.string(from: picker.date)
        
        TextTanggal.text = "\(dateString)"
        view.endEditing(true)
        format.dateFormat = "dd MMMM yyyy"
        TextTanggal.text! = format.string(from: picker.date)
        
        format.dateFormat = "yyyy-M-d"
        let newDate = format.string(from: picker.date)
        
        //
        print(newDate)
        UserDefaults.standard.set(newDate, forKey: "date")
    }
}





No comments: