MVC


//
//  ViewController.swift
//  PracticalExamMVC
//
//  Created by Asha Patel on 27/02/18.
//  Copyright © 2018 Inheritx. All rights reserved.
//

import UIKit
import ANLoader

class SignInViewController: UIViewController,SignInHelperParserDelegate {

    //MARK: outlets
   @IBOutlet weak var btnSignIn: UIButton!
   
    //MARK: properties
   var userDetailHelper_Parser = UserDetailHelper_Parser()
   var arrUserDetail = [UserDetail]()

    //MARK: view controller life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
   
    //MARK: SignInHelperParser Delegate methods
   
    //success
    func signInSuccess(arrUserDetail: [AnyObject]!) {
        DispatchQueue.main.async {
           
            UIApplication.shared.endIgnoringInteractionEvents()
            self.arrUserDetail = arrUserDetail as! [UserDetail]
            print(arrUserDetail)
            print("Login Successfull")
            self.navigateHomeVC(arrData : self.arrUserDetail)
   
        }
    }
    //fail
    func signInFailed(strerrormessage: String) {
        DispatchQueue.main.async {
            UIApplication.shared.endIgnoringInteractionEvents()
            print(strerrormessage)
           
            ANLoader.hide()
           
            let alertController: UIAlertController = UIAlertController(title: "Alert", message:strerrormessage, preferredStyle: .alert)
            let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .default) { action -> Void in
            }
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
           
        }
    }
   
    //MARK: others
    func navigateHomeVC(arrData : [UserDetail])
    {
        ANLoader.hide()
       
        let HomeVC : HomeViewController = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
        HomeVC.arrUserDetail = arrData
        self.navigationController?.pushViewController(HomeVC, animated: true)
    }
   
    //MARK: API calling
    func getUserDetail()
    {
        self.view.endEditing(true)
       
        UIApplication.shared.beginIgnoringInteractionEvents()
       
        ANLoader.showLoading()
   
        userDetailHelper_Parser.initWith(strurl: constantUrl, helperDelegate: self)
        userDetailHelper_Parser.userDetail(viewController: self)
    }
   
    //MARK: action
    @IBAction func actionSignIn(_ sender: UIButton) {
        getUserDetail()
    }
   
   
   
}

 


































//
//  HomeViewController.swift
//  PracticalExamMVC
//
//  Created by Asha Patel on 27/02/18.
//  Copyright © 2018 Inheritx. All rights reserved.
//

import UIKit
import ANLoader

class HomeViewController: UIViewController {
   
    //MARK: outlets
    @IBOutlet weak var tableview: UITableView!
   
    //MARK: properties
    var arrUserDetail = [UserDetail]()
   
    //MARK: view controller life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
       
        print(arrUserDetail)
        
        tableview.delegate = self
        tableview.dataSource = self
       
        tableview.rowHeight = UITableViewAutomaticDimension
        tableview.estimatedRowHeight = 250.5
    }
}

extension HomeViewController : UITableViewDelegate,UITableViewDataSource{
   
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
   
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if arrUserDetail.count != 0
        {
             return arrUserDetail.count
        }
        else
        {
            return 0
        }
    }
   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : UserDetailCell = tableview.dequeueReusableCell(withIdentifier: "UserDetailCell") as! UserDetailCell
        cell.userDetail = arrUserDetail[indexPath.row]
        return cell
    }
}
 





















//
//  Constant.swift
//  PracticalExamMVC
//
//  Created by Asha Patel on 27/02/18.
//  Copyright © 2018 Inheritx. All rights reserved.
//

import Foundation

let strErrorMessage = "Sign In Faild. Please try again!!"

//constant URL

let constantUrl = "http://52.20.177.204/api/web/v7/users/login"
// API's Key

let kLatitude = "latitude"
let kDevice_Token = "device_token"
let kEmail = "email"
let kLongitude = "longitude"
let kPassword = "password"
let kDevice_Type = "device_type"
let kSuccess = "success"


 


























//
//  AppManager.swift
//  MVCpractice
//
//  Created by Asha Patel on 22/02/18.
//  Copyright © 2018 Inheritx. All rights reserved.
//

import UIKit
import Alamofire

// MARK: - typealias
// api success
typealias apiCalledSuccess = (_ response: [String:AnyObject]) -> ()
// api failure
typealias apiCalledFailure = (_ error: Error) -> ()
// api progrss indocator
typealias progressIndicator = (_ response: Float) -> ()

class AppManager: NSObject {

  static let sharedIntance = AppManager()
   
    func PostMethodAPi(request: String,httpMethod: Alamofire.HTTPMethod, params: [String:AnyObject], fromVC: UIViewController, apiSuccess: @escaping apiCalledSuccess, apiFailuer: @escaping apiCalledFailure) {
    
        Alamofire.request(request, method: httpMethod, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
        
            guard response.result.error == nil else {
               
                print(response.result.error!)
                apiFailuer(response.result.error!)
                return
         
            }
            guard let json = response.result.value as? [String: AnyObject] else {
                print("Error: \(String(describing: response.result.error))")
                return
            }
            apiSuccess(json)
        }
    }
   
}

 






















//
//  UserDetail.swift
//  PracticalExamMVC
//
//  Created by Asha Patel on 27/02/18.
//  Copyright © 2018 Inheritx. All rights reserved.
//

import Foundation

struct UserDetail : Codable {
    var user_id : String?
    var first_name : String?
    var last_name : String?
    var email : String?
    var phone : String?
    var country_phone_code : String?
    var country_image : String?
    var user_mode : String?
    var start_time : String?
    var follower_mode : String?
    var user_image : String?
    var cover_image : String?
    var relationship_status : String?
    var auth_token : String?
    var birth_date : String?
    var qr_image : String?
    var created_at : String?

   
//    enum UserDetail {
//        case user_id
//        case first_name
//        case last_name
//        case email
//        case phone
//        case country_phone_code
//        case country_image
//        case user_mode
//        case start_time
//        case follower_mode
//        case user_image
//        case cover_image
//        case relationship_status
//        case auth_token
//        case birth_date
//        case qr_image
//        case created_at
//    }
   
}



 
//
//  UserDetailHelper+Parser.swift
//  PracticalExamMVC
//
//  Created by Asha Patel on 27/02/18.
//  Copyright © 2018 Inheritx. All rights reserved.
//

import UIKit
import Alamofire

protocol SignInHelperParserDelegate: class {
   
    func signInSuccess(arrUserDetail: [AnyObject]!)
    func signInFailed(strerrormessage: String)
   
}
class UserDetailHelper_Parser: NSObject {

    weak var delegate: SignInHelperParserDelegate?
    var strURL: String?
    var arrUserDetail : [UserDetail]?
   
    func initWith(strurl: String, helperDelegate: SignInHelperParserDelegate) {
        strURL = strurl
        delegate = helperDelegate
    }
   
    func userDetail(viewController: UIViewController) {
       
        let strParameters = [
            kLatitude : "23.0225" as AnyObject,
            kDevice_Token : "" as AnyObject,
            kEmail : "8905444545" as AnyObject,
            kLongitude : "72.5714" as AnyObject,
            kPassword : "123456" as AnyObject,
            kDevice_Type : "iphone" as AnyObject,
            ]
       
 
        AppManager.sharedIntance.PostMethodAPi(request: strURL!, httpMethod: .post, params: strParameters, fromVC:viewController , apiSuccess: { (response) in
           
            if response[kSuccess] as? NSNumber  == 1
            {
                do{
                    print(response)
                   
                    let jsonData = try JSONSerialization.data(withJSONObject: response["data"] as! [AnyObject], options: .prettyPrinted)
                    print(jsonData)
                    let jsonDecoder = JSONDecoder()
                    self.arrUserDetail = try jsonDecoder.decode([UserDetail].self, from: jsonData)
                    // api success
                    self.delegate?.signInSuccess(arrUserDetail: self.arrUserDetail! as [AnyObject])
                   
                }catch let error{
                    print(error)
                }
            }
            else
            {
                // api fail error
                DispatchQueue.main.async() {
                    self.delegate?.signInFailed(strerrormessage: strErrorMessage)
                }
            }
           
        }) { (error) in
            DispatchQueue.main.async() {
                // api fail error
                self.delegate?.signInFailed(strerrormessage: strErrorMessage)
            }
            return
        }
    }


}
 

Comments