Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // StartHarvestingTableViewController.swift
- // Sproyteappen
- //
- // Created by Emil Ramsdal on 13/03/2017.
- // Copyright © 2017 Veslebråten Data AS. All rights reserved.
- //
- import UIKit
- import FirebaseDatabase
- import CoreLocation
- class StartHarvestingTableViewController: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource, CLLocationManagerDelegate {
- var fieldList: [Field] = []
- var cropTypes: [CropType] = []
- var harvestingList: [Harvesting] = []
- var pesticideList: [Pesticide] = []
- var pesticideWorkList: [PesticideWork] = []
- var selectedField: Field?
- var currentHarvestingWorkKey: String = ""
- var processingDays: Int?
- var hasAddedFields: Bool = false
- var locationManager: CLLocationManager = CLLocationManager()
- var startLocation: CLLocation!
- @IBOutlet weak var fieldNameLabel: UILabel!
- @IBOutlet weak var fieldPicker: UIPickerView!
- @IBOutlet weak var timeLabel: UILabel!
- @IBOutlet weak var startHarvestingButton: UIButton!
- @IBOutlet weak var infoLabel: UILabel!
- override func viewDidLoad() {
- super.viewDidLoad()
- locationManager.desiredAccuracy = kCLLocationAccuracyBest
- locationManager.delegate = self
- locationManager.requestWhenInUseAuthorization()
- locationManager.startUpdatingLocation()
- fieldPicker.dataSource = self
- fieldPicker.delegate = self
- tableView.allowsMultipleSelectionDuringEditing = false
- timeLabel.text = TimeMethods.getTimeNow()
- harvestingRef?.queryOrdered(byChild: "fieldKey").observeSingleEvent(of: .value, with: { snapshot in
- var newItems: [Harvesting] = []
- for item in snapshot.children {
- let harvesting = Harvesting(snapshot: item as! FIRDataSnapshot)
- newItems.append(harvesting)
- }
- self.harvestingList = newItems
- self.tableView.reloadData()
- cropTypeRef.queryOrdered(byChild: "name").observeSingleEvent(of: .value, with: { snapshot in
- var newItems: [CropType] = []
- for item in snapshot.children {
- let cropType = CropType(snapshot: item as! FIRDataSnapshot)
- if(cropType.active){
- newItems.append(cropType)
- }
- }
- self.cropTypes = newItems
- self.tableView.reloadData()
- fieldRef.queryOrdered(byChild: "name").observeSingleEvent(of :.value, with: { snapshot in
- var newItems: [Field] = []
- for item in snapshot.children {
- let field = Field(snapshot: item as! FIRDataSnapshot)
- self.hasAddedFields = true
- if(!field.hidden && self.checkIfFieldIsAvailable(field: field)){
- newItems.append(field)
- }
- }
- self.fieldList = newItems
- self.fieldPicker.reloadAllComponents()
- self.tableView.reloadData()
- if(self.fieldList.count > 0){
- self.fieldNameLabel.text = self.fieldList[0].name + " - " + self.fieldList[0].crop
- self.selectedField = self.fieldList[0]
- }
- else {
- if(self.hasAddedFields){
- self.fieldNameLabel.text = "All fields harvested"
- } else {
- self.fieldNameLabel.text = "You need to add fields"
- }
- }
- self.fieldPickerUpdateField(number: 0)
- pesticideWorkRef?.queryOrdered(byChild: "timeFinished").observeSingleEvent(of: .value, with: { snapshot in
- var newItems: [PesticideWork] = []
- for item in snapshot.children {
- let pesticideWork = PesticideWork(snapshot: item as! FIRDataSnapshot)
- if(pesticideWork.finished){
- newItems.append(pesticideWork)
- }
- }
- self.pesticideWorkList = newItems
- self.tableView.reloadData()
- let pesticideForCurrentFieldRef: FIRDatabaseReference
- if(self.selectedField?.ecologic)!{
- pesticideForCurrentFieldRef = (ecologicPesticideRef.child((self.selectedField?.crop)!))
- } else {
- pesticideForCurrentFieldRef = (normalPesticideRef.child((self.selectedField?.crop)!))
- }
- pesticideForCurrentFieldRef.queryOrdered(byChild: "name").observeSingleEvent(of: .value, with: { snapshot in
- var newItems: [Pesticide] = []
- for item in snapshot.children {
- let pesticide = Pesticide(snapshot: item as! FIRDataSnapshot)
- newItems.append(pesticide)
- }
- self.pesticideList = newItems
- self.fieldPickerUpdateField(number: 0)
- self.selectedField = self.fieldList[0]
- self.updateInfoLabel(field: self.fieldList[0])
- self.tableView.reloadData()
- })
- })
- })
- })
- self.fieldPicker.reloadAllComponents()
- })
- }
- func downloadPesticideList(field: Field){
- let pesticideForCurrentFieldRef: FIRDatabaseReference
- if(field.ecologic){
- pesticideForCurrentFieldRef = (ecologicPesticideRef.child(field.crop))
- } else {
- pesticideForCurrentFieldRef = (normalPesticideRef.child(field.crop))
- }
- pesticideForCurrentFieldRef.queryOrdered(byChild: "name").observeSingleEvent(of: .value, with: { snapshot in
- var newItems: [Pesticide] = []
- for item in snapshot.children {
- let pesticide = Pesticide(snapshot: item as! FIRDataSnapshot)
- newItems.append(pesticide)
- }
- self.pesticideList.removeAll()
- self.pesticideList = newItems
- self.updateInfoLabel(field: field)
- self.tableView.reloadData()
- self.fieldPickerUpdateField(number: 0)
- })
- }
- func printAllLists(){
- print("fieldList - " + String(describing: fieldList.count))
- print("cropTypes - " + String(describing: cropTypes.count))
- print("harvestingList - " + String(describing: harvestingList.count))
- print("pesticideList - " + String(describing: pesticideList.count))
- print("pesticideWorkList - " + String(describing: pesticideWorkList.count))
- }
- func checkIfFieldIsAvailable(field: Field) -> Bool{
- for item in harvestingList {
- if (item.fieldKey == field.key){
- for crop in cropTypes{
- if(crop.name == field.crop){
- if(crop.multipleHarvesting){
- return true
- } else {
- return false
- }
- }
- }
- }
- }
- return true
- }
- override func viewDidDisappear(_ animated: Bool) {
- locationManager.stopUpdatingLocation()
- }
- override func viewDidAppear(_ animated: Bool) {
- fieldPickerUpdateField(number: 0)
- }
- @IBAction func startHarvestingButton(_ sender: Any) {
- guard let field = selectedField else {
- return
- }
- var longitude: CLLocationDegrees = CLLocationDegrees()
- var latitude: CLLocationDegrees = CLLocationDegrees()
- if let startLocation = startLocation{
- longitude = startLocation.coordinate.longitude
- latitude = startLocation.coordinate.latitude
- }
- let longitude1: String = String(describing: longitude)
- let latitude1: String = String(describing: latitude)
- let harvesting = Harvesting(timeStart: "", timeFinished: "", fieldKey: field.key, fieldName: field.name, finished: false, longitude: longitude1, latitude: latitude1)
- let newHarvestingRef = harvestingRef?.childByAutoId()
- newHarvestingRef?.setValue(harvesting.toAnyObject()){ err, ref in
- print(err ?? "Harvesting ok")
- }
- goToMainHarvestingViewController(key: newHarvestingRef!.key)
- }
- func goToMainHarvestingViewController(key: String) {
- let a = self.navigationController?.viewControllers[1] as! MainHarvestingViewController
- a.harvestingKey = key
- let _ = self.navigationController?.popToViewController(a, animated: false)
- }
- func checkIfButtonCanBeEnabled() {
- if(fieldNameLabel.text != ""){
- startHarvestingButton.isEnabled = true
- } else {
- startHarvestingButton.isEnabled = false
- }
- }
- // override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- //
- //
- //
- //
- // }
- func checkProcessingDeadline(field: Field) -> Int {
- var days = 0
- for pesticideWork in pesticideWorkList {
- if(pesticideWork.fieldKey == field.key){
- for usedPesticde in pesticideWork.pesticideObjectList {
- for pesticide in pesticideList {
- if(usedPesticde.pesticideRef == String(describing: pesticide.ref!)){
- let newDays = (TimeMethods.getDaysUntilDeadline(date: pesticideWork.timeFinished, numberOfDays: pesticide.processingDeadline!))
- if(newDays > days){
- days = newDays
- }
- }
- }
- }
- }
- }
- return days
- }
- func updateInfoLabel(field: Field){
- let days = checkProcessingDeadline(field: field)
- processingDays = days
- if(days > 0){
- infoLabel.text = "you have to wait " + String(describing: days) + " days"
- } else {
- infoLabel.text = "It is ok to harvest "
- }
- self.tableView.reloadData()
- }
- override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
- if(indexPath.row == 3){
- if (processingDays != nil) {
- if (processingDays! <= 0){
- cell.backgroundColor = UIColor.green
- } else {
- cell.backgroundColor = UIColor.red
- }
- }
- } else {
- cell.backgroundColor = UIColor.clear
- }
- }
- // pickerview stuff --- - - - - - - - - - - - - - - - - - - - - -- - - -- - - - -
- private func fieldPickerUpdateField (number: Int) {
- if (fieldList.count > 0) {
- let field = fieldList[number]
- fieldNameLabel.text = (field.name) + " - " + (field.crop)
- selectedField = field
- }
- }
- func numberOfComponents(in pickerView: UIPickerView) -> Int {
- if pickerView == fieldPicker {
- return 1
- }
- else {
- return 1
- }
- }
- func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
- if pickerView == fieldPicker {
- return fieldList.count
- } else {
- return 0
- }
- }
- func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
- if pickerView == fieldPicker {
- return fieldList[row].name
- }
- else {
- return ""
- }
- }
- func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
- if(pickerView == fieldPicker){
- if(fieldList.count > 0){
- fieldPickerUpdateField(number: row)
- selectedField = fieldList[row]
- downloadPesticideList(field: fieldList[row])
- }
- checkIfButtonCanBeEnabled()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement