DownloadOnDemandViewController.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import UIKit
  2. import DcCore
  3. class DownloadOnDemandViewController: UITableViewController {
  4. private var dcContext: DcContext
  5. private var options: [Int]
  6. private lazy var staticCells: [UITableViewCell] = {
  7. return options.map({
  8. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  9. cell.textLabel?.text = DownloadOnDemandViewController.getValString(val: $0)
  10. return cell
  11. })
  12. }()
  13. init(dcContext: DcContext) {
  14. self.dcContext = dcContext
  15. self.options = [0, 40960, 163840, 655360, 5242880, 26214400]
  16. super.init(style: .grouped)
  17. self.title = String.localized("auto_download_messages")
  18. hidesBottomBarWhenPushed = true
  19. }
  20. required init?(coder aDecoder: NSCoder) {
  21. fatalError("init(coder:) has not been implemented")
  22. }
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. }
  26. static func getValString(val: Int) -> String {
  27. switch val {
  28. case 0:
  29. return String.localized("no_limit")
  30. case 40960:
  31. return String.localizedStringWithFormat(String.localized("up_to_x"), "40 KiB")
  32. case 163840:
  33. return String.localizedStringWithFormat(String.localized("up_to_x_most_worse_quality_images"), "160 KiB")
  34. case 655360:
  35. return String.localizedStringWithFormat(String.localized("up_to_x_most_balanced_quality_images"), "640 KiB")
  36. case 5242880:
  37. return String.localizedStringWithFormat(String.localized("up_to_x"), "5 MB")
  38. case 26214400:
  39. return String.localizedStringWithFormat(String.localized("up_to_x"), "25 MB")
  40. default:
  41. return "Err"
  42. }
  43. }
  44. // MARK: - Table view data source
  45. override func numberOfSections(in tableView: UITableView) -> Int {
  46. return 1
  47. }
  48. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  49. return options.count
  50. }
  51. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  52. tableView.deselectRow(at: indexPath, animated: true) // animated as no other elements pop up
  53. if let lastSelectedIndex = options.index(of: dcContext.getConfigInt("download_limit")) {
  54. let oldSelectedCell = tableView.cellForRow(at: IndexPath.init(row: lastSelectedIndex, section: 0))
  55. oldSelectedCell?.accessoryType = .none
  56. }
  57. let newSelectedCell = tableView.cellForRow(at: IndexPath.init(row: indexPath.row, section: 0))
  58. newSelectedCell?.accessoryType = .checkmark
  59. dcContext.setConfigInt("download_limit", options[indexPath.row])
  60. }
  61. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  62. let cell = staticCells[indexPath.row]
  63. if options[indexPath.row] == dcContext.getConfigInt("download_limit") {
  64. cell.accessoryType = .checkmark
  65. }
  66. return cell
  67. }
  68. }