Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ItemsViewController.swift
- // Alamofire-Starter
- //
- // Created by Sevgjan Haxhija on 7/17/18.
- // Copyright © 2018 Sevgjan Haxhija. All rights reserved.
- //
- import UIKit
- import Alamofire
- import SwiftyJSON
- class ItemsViewController: UIViewController {
- @IBOutlet weak var tableView: UITableView!
- var sapURL = "http://212.4.68.34:50002/sitf/values/?url=http://sitfsed.sitf.local:8000/mobile/pco/f4_wbs"
- let searchController = UISearchController(searchResultsController: nil)
- let localTablesURL = "http://212.4.68.34:50002/sitf/values/?url=http://sitfsed.sitf.local:8000/mobile/pco/loctables"
- var oldFilteredNames:[BUKRS] = [BUKRS]()
- var filteredArray:[BUKRS] = [BUKRS]()
- var bukrsArray:[BUKRS] = [BUKRS]()
- override func viewDidLoad() {
- super.viewDidLoad()
- tableView.delegate = self
- tableView.dataSource = self
- var myPSP:LOCALTABLES = LOCALTABLES(object: Any.self)
- myPSP.psp = "219301"
- mypsp.pspsnr = 941005
- login(withUsername: "SHAXHIJA", andPassword: "sitfsw1ss", withPSP: myPSP)
- // Do any additional setup after loading the view.
- searchController.searchBar.tintColor = .white
- searchController.searchBar.barTintColor = .white
- searchController.searchResultsUpdater = self as UISearchResultsUpdating
- searchController.searchBar.keyboardAppearance = .dark
- searchController.definesPresentationContext = false
- searchController.dimsBackgroundDuringPresentation = false
- UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self] ).tintColor = UIColor.white
- if #available(iOS 11.0, *) {
- self.navigationItem.searchController = searchController
- } else {
- // self.navigationItem.titleView = searchController.searchBar
- // Fallback on earlier versions
- }
- filteredArray = bukrsArray
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- func psp(psp" spsp) {
- login(wihasoidjw, iodjapos withPSP: psp)
- }
- //MARK: Login
- func login(withUsername username: String, andPassword password: String, withPSP psp: F4PSP) {
- sapURL = sapURL + "?bukrs=\(psp.bukrs)&prctr=&probez=&pspnr=\(psp.pspnr)&vernr=&verna=&loevm="
- // startLoading()
- let credentials: String! = "Basic \("\(username):\(password)".data(using: String.Encoding.utf8)?.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) ?? "")"
- //basic parameteres
- // let params = [
- // "email": username,
- // "password": password
- // ] as [String: Any]
- //prepare for sending in SAP
- // let project:PRCTR = PRCTR(object: Any.self)
- // project.pRCTR = "Gzim"
- // project.kTEXT = "21.02.2018"
- // project.kOKRS = "42"
- //
- // convert it to dictionary to be able to send in parameters (ALAMOFIRE)
- // let dictionary = project.dictionaryRepresentation()
- //Base64 Authorization Header
- let loginHeaders = [
- "Authorization": "\(credentials!)"
- ]
- Alamofire.request(localTablesURL, method: .get, parameters: [:], headers: loginHeaders).responseJSON { response in
- if response.response?.statusCode == 200 {
- do {
- let json = try JSON(data: response.data!) //SwiftJSON
- let localTablesBase = LOCALTABLESBase(json: json)
- // let localTables = localTablesBase.lOCALTABLES
- // if let arrayPRCTR = localTablesBase.lOCALTABLES?.pRCTR {
- // self.prctrArray = arrayPRCTR
- // }
- if let arrayBUKRS = localTablesBase.lOCALTABLES?.bUKRS {
- self.bukrsArray = arrayBUKRS
- self.filteredArray = arrayBUKRS
- }
- //here you reload table, stop loading or whatever you want to do with the data after received them
- self.tableView.reloadData()
- //stopLoading
- } catch {
- print("diqka nuk o n'rregëll")
- }
- } else {
- print("error with code \(response.response?.statusCode ?? 0)")
- }
- }
- }
- }
- //MARK: TableViewDelegates and DataSources
- extension ItemsViewController: UITableViewDelegate, UITableViewDataSource {
- func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return filteredArray.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
- //bukrsArray = filteredArray
- let myBUKRS:BUKRS = filteredArray[indexPath.row]
- //bukrsArray = filteredArray[indexPath.row]
- cell.textLabel?.text = myBUKRS.bUKRS
- cell.detailTextLabel?.text = myBUKRS.bUTXT
- return cell
- }
- }
- extension ItemsViewController: UISearchResultsUpdating {
- func updateSearchResults(for searchController: UISearchController) {
- let oldFilteredNames = self.filteredArray
- if (searchController.searchBar.text?.isEmpty)! {
- self.filteredArray = bukrsArray
- } else {
- self.filteredArray = bukrsArray.filter({ (user) -> Bool in
- return (user.bUKRS ?? "").lowercased().contains(searchController.searchBar.text!.lowercased()) || (user.bUTXT ?? "").lowercased().contains(searchController.searchBar.text!.lowercased())
- })
- }
- self.tableView.performBatchUpdates({
- for (oldIndex, oldUser) in oldFilteredNames.enumerated() {
- if self.filteredArray.contains(oldUser) == false {
- let indexPath = IndexPath(item: oldIndex, section: 0)
- //self.tableView.deleteItems(at: [indexPath])
- self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.top)
- }
- }
- for (index, oldUser) in self.filteredArray.enumerated() {
- if oldFilteredNames.contains(oldUser) == false {
- let indexPath = IndexPath(item: index, section: 0)
- //self.tableView.insertItems(at: [indexPath])
- self.tableView.insertRows(at: [indexPath], with: UITableViewRowAnimation.bottom)
- }
- }
- }, completion: nil)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement