API SwiftyJSON (MVP Architecture) xcode

យើងសិក្សា ទាញ data ពី API ដាក់ TableView xcode រួចមកហើយ។ មេរៀនខាងក្រោមនេះសិក្សាជាមួយ SwiftyJSON និង delegate។

បន្ទាបពីយើងបង្កើត project រួចហើយយើងត្រូវ import library មួយគឺ SwiftyJSON ចូលក្នុង project របស់យើង

យើងរៀបចំ project យើងឲ្យទៅតាម MVP Architecture

សិក្សា services

នៅក្នុង Folder Service យើងបង្កើត file គឺ ContactService និង ContactServiceDelegate
- ContactService គឺជាអ្នកទាញទិន្ន័យពី server ប៉ុន្តែវាមិនថាវាដើរចប់នៅពេលណា
- ContactServiceDelegate គឺជាកាចំមើលថាទិន្ន័យដើរនៅពេលណាចប់ ហើយវាជាអ្នកប្រាប់យើង

នៅក្នុង ContactServiceDelegate យើងត្រូវបង្កើត protocol និង method ឲ្យវា

import Foundation
protocol ContactServiceDelegate{
    func responseContacts(contacts: [Contacts])
}

យើងទៅបង្កើត class នៃ Contacts ខាងលើនេះ នៅក្នុង Folder Models

សិក្សា Models

យើងបង្កើត class Contacts ហើយបង្កើត property ទៅតាមទិន្ន័យ JSON របស់យើង

import Foundation
import SwiftyJSON
class Contacts{
    var name: String?
    var phone: String?
    var sex: String?
    
    init(){}
    
    //របស់ library
    init(json: JSON){
        self.name = json["NAME"].string
        self.phone = json["PHONE"].string
        self.sex = json["SEX"].string
    }
}

សិក្សា services

សំរាប់ ContactService យើងបង្កើត class ឲ្យវានិងហៅ delegate មកប្រើ ។ បន្ទាប់មកបង្កើត method មួយឈ្មោះថា getContacts សំរាប់ទាញទិន្ន័យពី server

import Foundation
import SwiftyJSON

class ContactService{
    let URL_GET_CONTACT = "https://script.google.com/macros/s/AKfycbxuOXJ16OdCsatgumG40hllEHgJniQO3IBqolejLrOpC9zBUuIe1FPIJMRjhhTPGFSNsQ/exec"
    
    var delegate: ContactServiceDelegate?
    
    func getContacts(limit: Int, offSet: Int){
        var urlRequest = URLRequest(url: URL(string: "\(URL_GET_CONTACT)?limit=\(limit)&offSet=\(offSet)")!)
        urlRequest.httpMethod = "GET"
       // urlRequest.addValue(<#T##value: String##String#>, forHTTPHeaderField: <#T##String#>) //សំរាប់មានច្រើនទៀត
        
        let session = URLSession.shared
        session.dataTask(with: urlRequest) { data,response, error in
            if error == nil{
                // data get success
                let json = try? JSON(data: data!)
                
                let contactJSONArray = json!["data"].array //"data" គឺនៅក្នុង json file របស់យើង
                
                var contacts = [Contacts]()
                for contactJSON in contactJSONArray!{
                    contacts.append(Contacts(json: contactJSON))
                }
                self.delegate?.responseContacts(contacts: contacts)
            }
        }.resume()
    }
}
 

សិក្សា Presenters

នៅក្នុងកន្លែង presenter គឺសំទាញទិន្ន័យពី ContactService មកបង្ហាញ។ ដោយយើងបង្កើត file ពីរគឺ ContactPresenter និង ContactPresenterDelegate

សំរាប់ ContactPresenterDelegate យើងបង្កើត protocol និង method ស្រដៀងទៅនឹង ContactServiceDelegate ដែរ

import Foundation
protocol ContactPresenterDelegate{
    func responseContacts(contacts: [Contacts])
}

ContactPresenter យើងបង្កើត class និងបង្កើត property ចេញពី delegate មកប្រើនៅក្នុង class។ បន្ទាបមកបង្កើត property ពី ContactService មកប្រើនៅក្នុង presenter។

import Foundation
class ContactPresenter{
    
    var delage: ContactServiceDelegate?
    var contactService: ContactService?
    
}

បន្ទាបមកយើង initialize របស់ ContactService គឺសំរាប់នៅពេលគេបង្កើត object ចេញពី class ContactPresenter នៅពេលនោះវានឹងបង្កើត object នៅក្នុង initialize ដែរ។

import Foundation
class ContactPresenter{
    
    var delage: ContactServiceDelegate?
    var contactService: ContactService?
    
    init(){
        contactService = ContactService()
    }
    
}

យើងត្រូវហៅ delegate របស់ ContactService មកប្រើដែរ និង override method មកប្រើ

import Foundation
class ContactPresenter: ContactServiceDelegate{
    
    var delage: ContactServiceDelegate?
    var contactService: ContactService?
    
    init(){
        contactService = ContactService()
        contactService?.delegate = self
    }
    func responseContacts(contacts: [Contacts]) {
        <#code#>
    }
}

យើងបង្កើត method មួយទៀតគឺ

func getContact(limit: String, offSet: String){
        
    }

ដើម្បីទទួលទិន្ន័យយើងហៅតាម contactService

func getContact(limit: Int, offSet: Int){
        contactService?.getContacts(limit: limit, offSet: offSet)
    }

នៅពេលទិន្ន័យដើរនោះ func responseContacts វានឹងដំណើរការ

import Foundation
class ContactPresenter: ContactServiceDelegate{
    
    var delage: ContactServiceDelegate?
    var contactService: ContactService?
    
    init(){
        contactService = ContactService()
        contactService?.delegate = self
    }
    func getContact(limit: Int, offSet: Int){
        contactService?.getContacts(limit: limit, offSet: offSet)
    }
    func responseContacts(contacts: [Contacts]) {
        //សំគណនា
        self.delage?.responseContacts(contacts: contacts)
    }
}

សិក្សា ViewController

នៅក្នុង ViewController យើងហៅ ContactPresenter មកប្រើ

import UIKit

class ViewController: UIViewController, ContactServiceDelegate {
    
    var contactPresenter: ContactPresenter?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        contactPresenter = ContactPresenter()
        contactPresenter?.delage = self
        
    }
    func responseContacts(contacts: [Contacts]) {
        
    }
}

បន្ទាបមកយើង method របស់វាមកប្រើនៅក្នុង viewDidload

contactPresenter?.getContact(limit: 10, offSet: 10)

នៅក្នុង method responseContacts យើងអាច loop ទិន្ន័យបានហើយ

func responseContacts(contacts: [Contacts]) {
        for contact in contacts{
            print(contact.name)
        }
    }
import UIKit

class ViewController: UIViewController,ContactServiceDelegate {
    
    var contactPresenter: ContactPresenter?

    override func viewDidLoad() {
        super.viewDidLoad()
       contactPresenter = ContactPresenter()
        contactPresenter?.delage = self
        
        contactPresenter?.getContact(limit: 10, offSet: 10)
    }
    func responseContacts(contacts: [Contacts]) {
        for contact in contacts{
            print(contact.name)
        }
    }

}

បោះទិន្ន័យចូលទៅក្នុង tableView

import UIKit

class ViewController: UIViewController,ContactServiceDelegate, UITableViewDelegate,UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    
    var contactPresenter: ContactPresenter?
    var contact = [Contacts]()

    override func viewDidLoad() {
        super.viewDidLoad()
        contactPresenter = ContactPresenter()
        contactPresenter?.delage = self
        contactPresenter?.getContact(limit: 15, offSet: 15)
        
    }
    func responseContacts(contacts: [Contacts]) {
        self.contact = contacts
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return contact.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  tableView.dequeueReusableCell(withIdentifier: "cell")
        cell?.textLabel?.text = contact[indexPath.row].phone
        cell?.detailTextLabel?.text = contact[indexPath.row].name
        
        cell?.textLabel?.font = UIFont.systemFont(ofSize: 22.0)
        cell?.detailTextLabel?.font = UIFont.systemFont(ofSize: 20.0)
        return cell!
    }
}

Post a Comment

0 Comments