Created
October 23, 2018 19:15
-
-
Save FabiolaRamirez/3d47c84ed64385d4ee8ff90289f598f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // IDProtectionLocationsViewController.swift | |
| // finance | |
| // | |
| // Created by DavidMora on 10/2/18. | |
| // Copyright © 2018 creditsesame. All rights reserved. | |
| // | |
| import UIKit | |
| enum IDProtectionLocationsSection: Int { | |
| case ssnTrace | |
| case changeOfAddress | |
| case sexOffender | |
| case getHelp | |
| var name: String { | |
| switch self { | |
| case .ssnTrace: | |
| return NSLocalizedString("SSN Trace", comment: "SSN Trace") | |
| case .changeOfAddress: | |
| return NSLocalizedString("Change of Address", comment: "Change of Address") | |
| case .sexOffender: | |
| return NSLocalizedString("Sex Offenders", comment: "Sex Offenders") | |
| case .getHelp: | |
| return NSLocalizedString("Get Help", comment: "Get Help") | |
| } | |
| } | |
| var idAlertCategories: [IDAlertCategory]? { | |
| switch self { | |
| case .ssnTrace: | |
| return [.ssnTrace, .ssnTraceReport] | |
| case .changeOfAddress: | |
| return [.changeAddress, .changeAddressReport] | |
| case .sexOffender: | |
| return [.sexOffender, .sexOffenderReport] | |
| default: | |
| return nil | |
| } | |
| } | |
| static var all: [IDProtectionLocationsSection] { | |
| return [.ssnTrace, .changeOfAddress, .sexOffender, .getHelp] | |
| } | |
| var numberOfRows: Int { | |
| switch self { | |
| case .ssnTrace: | |
| return 2 | |
| case .changeOfAddress, .sexOffender, .getHelp: | |
| return 1 | |
| } | |
| } | |
| var noFoundText: String { | |
| switch self { | |
| case .ssnTrace: | |
| return ClientConfigurationManager.shared.configurationFile?.premiumCopy?.ssnTrace?.notificationsNotFound ?? NSLocalizedString("NotificationsNotFound", comment: "NotificationsNotFound") | |
| default: | |
| return "" | |
| } | |
| } | |
| } | |
| class IDProtectionLocationsViewController: ViewController { | |
| @IBOutlet weak var tableView: UITableView! | |
| let mapCellIdentifier = "MapCellIdentifier" | |
| let alertCellIdentifier = "AlertCellIdentifier" | |
| let noReportCellIdentifier = "NoReportCell" | |
| var ssnTraceAlerts: [Alert] = [] | |
| var changeAddressAlerts: [Alert] = [] | |
| var sexOffenderAlerts: [Alert] = [] | |
| var ssnTraceAlertPoints: [MapPoint] = [] | |
| var currentAlert: Alert? | |
| var mapView: MapView? | |
| var pageTitle: String { | |
| return NSLocalizedString("ID Protection Locations", comment: "ID Protection Locations") | |
| } | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| tableView.register(UITableViewCell.self, forCellReuseIdentifier: noReportCellIdentifier) | |
| tableView.register(UITableViewCell.self, forCellReuseIdentifier: alertCellIdentifier) | |
| tableView.register(UITableViewCell.self, forCellReuseIdentifier: mapCellIdentifier) | |
| tableView.estimatedRowHeight = 128 | |
| tableView.estimatedSectionHeaderHeight = 60 | |
| tableView.estimatedSectionFooterHeight = 34 | |
| tableView.backgroundColor = UIColor.creditSesameLightGrayBackgroundColor | |
| tableView.separatorStyle = .singleLine | |
| tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude)) | |
| tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude)) | |
| generatePoints() | |
| //validate SSNTrace, SexOff.. | |
| if !ssnTraceAlertPoints.isEmpty { | |
| CurrentAlert.shared.currentSSNTraceAlertPoint = ssnTraceAlertPoints[0] | |
| } | |
| } | |
| func generatePoints() { | |
| //validate SSNTrace, SexOff.. | |
| for alert in ssnTraceAlerts { | |
| let alertPoint: MapPoint? = alert.mapPointInfo | |
| if let alertPoint = alertPoint { | |
| ssnTraceAlertPoints.append(alertPoint) | |
| } | |
| } | |
| } | |
| override func viewWillAppear(_ animated: Bool) { | |
| super.viewWillAppear(animated) | |
| navigationController?.setNavigationBarHidden(true, animated: false) | |
| creditSesameNavigationController?.setNavigationBarHidden(false, animated: true) | |
| } | |
| @objc func seeSSNTraceAlerts() { | |
| let vc = UIViewController.instantiateViewController(storyBoard: "Alerts", identifier: "AlertsLocationViewController") as! AlertsLocationViewController | |
| vc.mode = .ssnTracePremium | |
| vc.alertCategories = IDProtectionLocationsSection.ssnTrace.idAlertCategories | |
| vc.idAlertSectionTitle = IDProtectionLocationsSection.ssnTrace.name | |
| vc.alerts = ssnTraceAlerts | |
| vc.updateBlock = nil | |
| self.creditSesameNavigationController?.pushViewController(vc, animated: true) | |
| } | |
| @objc func seeAddressReports() { | |
| } | |
| @objc func seeSexOffenderAlerts() { | |
| } | |
| } | |
| extension IDProtectionLocationsViewController: UITableViewDelegate, UITableViewDataSource { | |
| func numberOfSections(in tableView: UITableView) -> Int { | |
| return IDProtectionLocationsSection.all.count | |
| } | |
| func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
| let section = IDProtectionLocationsSection(rawValue: section) ?? .ssnTrace | |
| return section.numberOfRows | |
| } | |
| func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { | |
| let idSection = IDProtectionLocationsSection(rawValue: section) ?? .ssnTrace | |
| if idSection.numberOfRows == 0 { | |
| return CGFloat.leastNonzeroMagnitude | |
| } | |
| return UITableViewAutomaticDimension | |
| } | |
| func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { | |
| if tableView.numberOfRows(inSection: section) == 0 { | |
| return UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude)) | |
| } | |
| let idSection = IDProtectionLocationsSection(rawValue: section) ?? .ssnTrace | |
| let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 60)) | |
| let titleLabel = UILabel() | |
| titleLabel.textColor = UIColor.creditSesameGrayTextColor | |
| titleLabel.font = UIFont.latoHeavy(18) | |
| titleView.addSubview(titleLabel) | |
| titleLabel.snp.makeConstraints { make in | |
| make.top.equalToSuperview().offset(24) | |
| make.bottom.equalToSuperview().offset(-16) | |
| make.left.equalToSuperview().offset(16) | |
| make.right.equalToSuperview().offset(-16) | |
| } | |
| titleLabel.text = idSection.name | |
| let headerView = UITableViewHeaderFooterView(frame: CGRect(x: 0, y: 0, width: 0, height: 60)) | |
| headerView.addSubview(titleView) | |
| titleView.snp.makeConstraints { make in | |
| make.edges.equalToSuperview() | |
| } | |
| return headerView | |
| } | |
| func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { | |
| let idSection = IDProtectionLocationsSection(rawValue: section) ?? .ssnTrace | |
| if idSection.numberOfRows == 0 || idSection == .getHelp { | |
| return CGFloat.leastNonzeroMagnitude | |
| } | |
| return UITableViewAutomaticDimension | |
| } | |
| func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { | |
| let idSection = IDProtectionLocationsSection(rawValue: section) ?? .ssnTrace | |
| if tableView.numberOfRows(inSection: section) == 0 || idSection == .getHelp { | |
| return UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude)) | |
| } | |
| let moreView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 60)) | |
| let moreButton = UIButton() | |
| moreView.backgroundColor = UIColor.white | |
| moreButton.setThemeSeeMoreButton() | |
| moreButton.setTitle(NSLocalizedString("More", comment: "More"), for: .normal) | |
| moreView.addSubview(moreButton) | |
| moreButton.snp.makeConstraints { make in | |
| make.height.equalTo(34) | |
| make.top.equalToSuperview() | |
| make.bottom.equalToSuperview() | |
| make.right.equalToSuperview().offset(-16) | |
| } | |
| moreView.addTopSeparator() | |
| moreView.addBottomSeparator() | |
| switch idSection { | |
| case .ssnTrace: | |
| moreButton.addTarget(self, action: #selector(seeSSNTraceAlerts), for: .touchUpInside) | |
| case .changeOfAddress: | |
| moreButton.addTarget(self, action: #selector(seeAddressReports), for: .touchUpInside) | |
| case .sexOffender: | |
| moreButton.addTarget(self, action: #selector(seeSexOffenderAlerts), for: .touchUpInside) | |
| default: | |
| break | |
| } | |
| let footerView = UITableViewHeaderFooterView(frame: CGRect(x: 0, y: 0, width: 0, height: 60)) | |
| footerView.addSubview(moreView) | |
| moreView.snp.makeConstraints { make in | |
| make.edges.equalToSuperview() | |
| } | |
| return footerView | |
| } | |
| func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
| var cell = UITableViewCell() | |
| if let section = IDProtectionLocationsSection(rawValue: indexPath.section) { | |
| switch section { | |
| case .ssnTrace: | |
| if indexPath.row == 0 { | |
| cell = configureMapCell() | |
| } else if indexPath.row == 1 { | |
| if let current = CurrentAlert.shared.currentSSNTraceAlertPoint { | |
| cell = ssnTraceAlertPoints.count <= 0 ? configureNoAlertCell(section: section) : configureAlertCell(indexPath: indexPath, alert: current.alert!) | |
| } | |
| } | |
| case .changeOfAddress: | |
| cell = configureNoAlertCell(section: section) | |
| case .sexOffender: | |
| cell = configureNoAlertCell(section: section) | |
| case .getHelp: | |
| cell = configureNoAlertCell(section: section) | |
| default: | |
| break | |
| } | |
| } | |
| cell.selectionStyle = .none | |
| return cell | |
| } | |
| func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | |
| if let section = IDProtectionLocationsSection(rawValue: indexPath.section) { | |
| switch section { | |
| case .ssnTrace: | |
| if indexPath.row == 0 { | |
| return 180 | |
| } | |
| default: | |
| break | |
| } | |
| } | |
| return UITableViewAutomaticDimension | |
| } | |
| func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
| if let section = IDProtectionLocationsSection(rawValue: indexPath.section) { | |
| switch section { | |
| case .ssnTrace: | |
| if indexPath.row == 1 { | |
| let alerts = ssnTraceAlerts | |
| if indexPath.row < alerts.count { | |
| let alert = alerts[indexPath.row] | |
| openIDAlertInfo(alert: alert, mode: AlertLocationType.ssnTracePremium) | |
| markAlertAsViewed(alert: alert, mode: .iDAlert) { result in | |
| if result { | |
| DispatchQueue.main.async { | |
| self.tableView.reloadRows(at: [indexPath], with: .none) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| default: | |
| break | |
| } | |
| } | |
| } | |
| func openIDAlertInfo(alert: Alert, mode: AlertLocationType) { | |
| showProgressView() | |
| DispatchQueue.global(qos: .background).async { | |
| // parse of formatted info on background thread | |
| if let info = alert.formattedAlertInfo { | |
| let vc = UIViewController.instantiateViewController(storyBoard: "Alerts", identifier: "AlertDetailsLocationViewController") as! AlertDetailsLocationViewController | |
| vc.mode = mode | |
| vc.alert = alert | |
| vc.formattedAlertInfo = info | |
| DispatchQueue.main.async { | |
| self.hideProgressView() | |
| self.creditSesameNavigationController?.pushViewController(vc, animated: true) | |
| } | |
| } | |
| else { | |
| let alertId = alert.id ?? "none" | |
| let alertCategory = alert.alertCategory ?? "none" | |
| fatalError("Error: AlertInfo invalid or empty - Alert Id: \(alertId), Category: \(alertCategory)") | |
| } | |
| } | |
| } | |
| func configureMapCell() -> UITableViewCell { | |
| if let cell = tableView.dequeueReusableCell(withIdentifier: mapCellIdentifier) { | |
| for v in cell.contentView.subviews { | |
| v.removeFromSuperview() | |
| } | |
| let mapViewContainer = UIView.loadNib("MapView") | |
| mapView = mapViewContainer.viewWithTag(10) as? MapView | |
| mapView?.alertPointDelegate = self | |
| mapView?.alertType = .ssnTracePremium | |
| if ssnTraceAlertPoints.isEmpty { | |
| let user = CreditSesameRestClient.shared.user | |
| if let userAddress = user?.formattedPrimaryAddressString { | |
| mapView?.points = [MapPoint(address:userAddress, alert: nil)] | |
| } | |
| } else { | |
| mapView?.points = ssnTraceAlertPoints | |
| } | |
| mapViewContainer.addTopSeparator() | |
| mapViewContainer.addBottomSeparator() | |
| cell.contentView.addSubview(mapViewContainer) | |
| mapViewContainer.snp.makeConstraints { make in | |
| make.edges.equalToSuperview() | |
| } | |
| return cell | |
| } | |
| return UITableViewCell() | |
| } | |
| func configureAlertCell(indexPath: IndexPath, alert: Alert) -> UITableViewCell { | |
| if let cell = tableView.dequeueReusableCell(withIdentifier: alertCellIdentifier) { | |
| for v in cell.contentView.subviews { | |
| v.removeFromSuperview() | |
| } | |
| let alertView = UIView.loadNib("AlertInfoView") | |
| let badgeView = alertView.viewWithTag(101) | |
| let dateLabel = alertView.labelWithTag(211) | |
| let titleLabel = alertView.labelWithTag(221) | |
| let companyView = alertView.viewWithTag(230) | |
| badgeView?.clipsToBounds = true | |
| badgeView?.backgroundColor = UIColor.creditSesameRed | |
| badgeView?.layer.cornerRadius = (badgeView?.frame.size.height ?? 0) / 2 | |
| dateLabel?.font = UIFont.lato(14) | |
| dateLabel?.textColor = UIColor.creditSesameGrayTextColor | |
| titleLabel?.font = UIFont.latoBold(16) | |
| titleLabel?.textColor = UIColor.creditSesameGrayTextColor | |
| dateLabel?.text = alert.formattedCreateDate | |
| titleLabel?.text = alert.localizedCategory | |
| companyView?.isHidden = true | |
| badgeView?.isHidden = !(alert.viewedDate ?? "").isEmpty | |
| if indexPath.row == 0 { | |
| alertView.addTopSeparator() | |
| alertView.addBottomSeparator() | |
| } | |
| cell.contentView.addSubview(alertView) | |
| alertView.snp.makeConstraints { make in | |
| make.edges.equalToSuperview() | |
| } | |
| return cell | |
| } | |
| return UITableViewCell() | |
| } | |
| func configureNoAlertCell(section: IDProtectionLocationsSection) -> UITableViewCell { | |
| if let cell = tableView.dequeueReusableCell(withIdentifier: noReportCellIdentifier) { | |
| for v in cell.contentView.subviews { | |
| v.removeFromSuperview() | |
| } | |
| let noReportView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 100)) | |
| let titleLabel = UILabel() | |
| titleLabel.textColor = UIColor.creditSesameGrayTextColor | |
| titleLabel.font = UIFont.lato(14) | |
| titleLabel.numberOfLines = 0 | |
| titleLabel.text = section.noFoundText.htmlToString | |
| noReportView.addSubview(titleLabel) | |
| titleLabel.snp.makeConstraints { make in | |
| make.top.equalToSuperview().offset(16) | |
| make.bottom.equalToSuperview().offset(-16) | |
| make.left.equalToSuperview().offset(16) | |
| make.right.equalToSuperview().offset(-16) | |
| } | |
| noReportView.addTopSeparator() | |
| cell.contentView.addSubview(noReportView) | |
| noReportView.snp.makeConstraints { make in | |
| make.edges.equalToSuperview() | |
| } | |
| return cell | |
| } | |
| return UITableViewCell() | |
| } | |
| } | |
| extension IDProtectionLocationsViewController: AlertPointDelegate { | |
| func selectAlertPoint(mapPoint: MapPoint, alertType: AlertLocationType) { | |
| print("pin selected: \(mapPoint)") | |
| switch alertType { | |
| case .ssnTracePremium: | |
| CurrentAlert.shared.currentSSNTraceAlertPoint = mapPoint | |
| tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .automatic) | |
| mapView?.drawPoints() | |
| default: | |
| break | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment