Advertisement
Guest User

Untitled

a guest
Jul 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. //
  2. // ItemsViewController.swift
  3. // Alamofire-Starter
  4. //
  5. // Created by Sevgjan Haxhija on 7/17/18.
  6. // Copyright © 2018 Sevgjan Haxhija. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Alamofire
  11. import SwiftyJSON
  12.  
  13. class ItemsViewController: UIViewController {
  14. @IBOutlet weak var tableView: UITableView!
  15. var sapURL = "http://212.4.68.34:50002/sitf/values/?url=http://sitfsed.sitf.local:8000/mobile/pco/f4_wbs"
  16. let searchController = UISearchController(searchResultsController: nil)
  17.  
  18. let localTablesURL = "http://212.4.68.34:50002/sitf/values/?url=http://sitfsed.sitf.local:8000/mobile/pco/loctables"
  19.  
  20. var oldFilteredNames:[BUKRS] = [BUKRS]()
  21. var filteredArray:[BUKRS] = [BUKRS]()
  22. var bukrsArray:[BUKRS] = [BUKRS]()
  23.  
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26.  
  27. tableView.delegate = self
  28. tableView.dataSource = self
  29.  
  30. var myPSP:LOCALTABLES = LOCALTABLES(object: Any.self)
  31. myPSP.psp = "219301"
  32. mypsp.pspsnr = 941005
  33. login(withUsername: "SHAXHIJA", andPassword: "sitfsw1ss", withPSP: myPSP)
  34. // Do any additional setup after loading the view.
  35.  
  36. searchController.searchBar.tintColor = .white
  37. searchController.searchBar.barTintColor = .white
  38. searchController.searchResultsUpdater = self as UISearchResultsUpdating
  39. searchController.searchBar.keyboardAppearance = .dark
  40. searchController.definesPresentationContext = false
  41. searchController.dimsBackgroundDuringPresentation = false
  42. UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self] ).tintColor = UIColor.white
  43. if #available(iOS 11.0, *) {
  44. self.navigationItem.searchController = searchController
  45. } else {
  46. // self.navigationItem.titleView = searchController.searchBar
  47. // Fallback on earlier versions
  48. }
  49.  
  50. filteredArray = bukrsArray
  51. }
  52.  
  53. override func didReceiveMemoryWarning() {
  54. super.didReceiveMemoryWarning()
  55. // Dispose of any resources that can be recreated.
  56. }
  57.  
  58. func psp(psp" spsp) {
  59. login(wihasoidjw, iodjapos withPSP: psp)
  60. }
  61.  
  62. //MARK: Login
  63. func login(withUsername username: String, andPassword password: String, withPSP psp: F4PSP) {
  64. sapURL = sapURL + "?bukrs=\(psp.bukrs)&prctr=&probez=&pspnr=\(psp.pspnr)&vernr=&verna=&loevm="
  65. // startLoading()
  66. let credentials: String! = "Basic \("\(username):\(password)".data(using: String.Encoding.utf8)?.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) ?? "")"
  67.  
  68. //basic parameteres
  69. // let params = [
  70. // "email": username,
  71. // "password": password
  72. // ] as [String: Any]
  73.  
  74.  
  75. //prepare for sending in SAP
  76. // let project:PRCTR = PRCTR(object: Any.self)
  77. // project.pRCTR = "Gzim"
  78. // project.kTEXT = "21.02.2018"
  79. // project.kOKRS = "42"
  80. //
  81. // convert it to dictionary to be able to send in parameters (ALAMOFIRE)
  82. // let dictionary = project.dictionaryRepresentation()
  83.  
  84. //Base64 Authorization Header
  85. let loginHeaders = [
  86. "Authorization": "\(credentials!)"
  87. ]
  88.  
  89. Alamofire.request(localTablesURL, method: .get, parameters: [:], headers: loginHeaders).responseJSON { response in
  90. if response.response?.statusCode == 200 {
  91. do {
  92. let json = try JSON(data: response.data!) //SwiftJSON
  93. let localTablesBase = LOCALTABLESBase(json: json)
  94. // let localTables = localTablesBase.lOCALTABLES
  95.  
  96. // if let arrayPRCTR = localTablesBase.lOCALTABLES?.pRCTR {
  97. // self.prctrArray = arrayPRCTR
  98. // }
  99. if let arrayBUKRS = localTablesBase.lOCALTABLES?.bUKRS {
  100. self.bukrsArray = arrayBUKRS
  101. self.filteredArray = arrayBUKRS
  102. }
  103.  
  104. //here you reload table, stop loading or whatever you want to do with the data after received them
  105. self.tableView.reloadData()
  106. //stopLoading
  107.  
  108. } catch {
  109. print("diqka nuk o n'rregëll")
  110. }
  111. } else {
  112. print("error with code \(response.response?.statusCode ?? 0)")
  113. }
  114.  
  115. }
  116.  
  117. }
  118.  
  119. }
  120.  
  121. //MARK: TableViewDelegates and DataSources
  122. extension ItemsViewController: UITableViewDelegate, UITableViewDataSource {
  123.  
  124. func numberOfSections(in tableView: UITableView) -> Int {
  125. return 1
  126. }
  127.  
  128. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  129. return filteredArray.count
  130. }
  131.  
  132. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  133. let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
  134. //bukrsArray = filteredArray
  135. let myBUKRS:BUKRS = filteredArray[indexPath.row]
  136. //bukrsArray = filteredArray[indexPath.row]
  137. cell.textLabel?.text = myBUKRS.bUKRS
  138. cell.detailTextLabel?.text = myBUKRS.bUTXT
  139.  
  140. return cell
  141. }
  142.  
  143. }
  144.  
  145. extension ItemsViewController: UISearchResultsUpdating {
  146. func updateSearchResults(for searchController: UISearchController) {
  147. let oldFilteredNames = self.filteredArray
  148.  
  149. if (searchController.searchBar.text?.isEmpty)! {
  150. self.filteredArray = bukrsArray
  151. } else {
  152. self.filteredArray = bukrsArray.filter({ (user) -> Bool in
  153. return (user.bUKRS ?? "").lowercased().contains(searchController.searchBar.text!.lowercased()) || (user.bUTXT ?? "").lowercased().contains(searchController.searchBar.text!.lowercased())
  154. })
  155. }
  156.  
  157. self.tableView.performBatchUpdates({
  158. for (oldIndex, oldUser) in oldFilteredNames.enumerated() {
  159. if self.filteredArray.contains(oldUser) == false {
  160. let indexPath = IndexPath(item: oldIndex, section: 0)
  161. //self.tableView.deleteItems(at: [indexPath])
  162. self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.top)
  163. }
  164. }
  165.  
  166. for (index, oldUser) in self.filteredArray.enumerated() {
  167. if oldFilteredNames.contains(oldUser) == false {
  168. let indexPath = IndexPath(item: index, section: 0)
  169. //self.tableView.insertItems(at: [indexPath])
  170. self.tableView.insertRows(at: [indexPath], with: UITableViewRowAnimation.bottom)
  171. }
  172. }
  173. }, completion: nil)
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement