1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import UIKit
- import DcCore
- class DownloadOnDemandViewController: UITableViewController {
- private var dcContext: DcContext
- private var options: [Int]
- private lazy var staticCells: [UITableViewCell] = {
- return options.map({
- let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
- cell.textLabel?.text = DownloadOnDemandViewController.getValString(val: $0)
- return cell
- })
- }()
- init(dcContext: DcContext) {
- self.dcContext = dcContext
- self.options = [0, 40960, 163840, 655360, 5242880, 26214400]
- super.init(style: .grouped)
- self.title = String.localized("auto_download_messages")
- hidesBottomBarWhenPushed = true
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- static func getValString(val: Int) -> String {
- switch val {
- case 0:
- return String.localized("no_limit")
- case 40960:
- return String.localizedStringWithFormat(String.localized("up_to_x"), "40 KiB")
- case 163840:
- return String.localizedStringWithFormat(String.localized("up_to_x_most_worse_quality_images"), "160 KiB")
- case 655360:
- return String.localizedStringWithFormat(String.localized("up_to_x_most_balanced_quality_images"), "640 KiB")
- case 5242880:
- return String.localizedStringWithFormat(String.localized("up_to_x"), "5 MB")
- case 26214400:
- return String.localizedStringWithFormat(String.localized("up_to_x"), "25 MB")
- default:
- return "Err"
- }
- }
- // MARK: - Table view data source
- override func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return options.count
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- tableView.deselectRow(at: indexPath, animated: true) // animated as no other elements pop up
- if let lastSelectedIndex = options.index(of: dcContext.getConfigInt("download_limit")) {
- let oldSelectedCell = tableView.cellForRow(at: IndexPath.init(row: lastSelectedIndex, section: 0))
- oldSelectedCell?.accessoryType = .none
- }
- let newSelectedCell = tableView.cellForRow(at: IndexPath.init(row: indexPath.row, section: 0))
- newSelectedCell?.accessoryType = .checkmark
- dcContext.setConfigInt("download_limit", options[indexPath.row])
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = staticCells[indexPath.row]
- if options[indexPath.row] == dcContext.getConfigInt("download_limit") {
- cell.accessoryType = .checkmark
- }
- return cell
- }
- }
|