VLCSettingsSpecifierManager.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*****************************************************************************
  2. * VLCSettingsSpecifierManager.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright © 2018 VLC authors and VideoLAN
  6. * $Id$
  7. *
  8. * Authors: Mike JS. Choi <mkchoi212 # icloud.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import UIKit
  13. class VLCSettingsSpecifierManager: NSObject {
  14. @objc var specifier: IASKSpecifier?
  15. var settingsReader: IASKSettingsReader
  16. var settingsStore: IASKSettingsStore
  17. var items: NSArray {
  18. guard let items = specifier?.multipleValues() as NSArray? else {
  19. fatalError("VLCSettingsSpecifierManager: No rows provided for \(specifier?.key() ?? "null specifier")")
  20. }
  21. return items
  22. }
  23. @objc var selectedIndex: IndexPath {
  24. let index: Int
  25. if let selectedItem = settingsStore.object(forKey: specifier?.key()) {
  26. index = items.index(of: selectedItem)
  27. } else if let specifier = specifier {
  28. index = items.index(of: specifier.defaultValue())
  29. } else {
  30. fatalError("VLCSettingsSpecifierManager: No specifier provided")
  31. }
  32. return IndexPath(row: index, section: 0)
  33. }
  34. @objc init(settingsReader: IASKSettingsReader, settingsStore: IASKSettingsStore) {
  35. self.settingsReader = settingsReader
  36. self.settingsStore = settingsStore
  37. super.init()
  38. }
  39. }
  40. // MARK: VLCActionSheetDelegate
  41. extension VLCSettingsSpecifierManager: VLCActionSheetDelegate {
  42. func headerViewTitle() -> String? {
  43. return specifier?.title()
  44. }
  45. func itemAtIndexPath(_ indexPath: IndexPath) -> Any? {
  46. return items[indexPath.row]
  47. }
  48. func actionSheet(collectionView: UICollectionView, didSelectItem item: Any, At indexPath: IndexPath) {
  49. settingsStore.setObject(item, forKey: specifier?.key())
  50. settingsStore.synchronize()
  51. }
  52. }
  53. // MARK: VLCActionSheetDataSource
  54. extension VLCSettingsSpecifierManager: VLCActionSheetDataSource {
  55. func numberOfRows() -> Int {
  56. return items.count
  57. }
  58. func actionSheet(collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  59. guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: VLCSettingsSheetCell.identifier, for: indexPath) as? VLCSettingsSheetCell else {
  60. return UICollectionViewCell()
  61. }
  62. if let titles = specifier?.multipleTitles(), indexPath.row < titles.count {
  63. cell.name.text = settingsReader.title(forStringId: titles[indexPath.row] as? String)
  64. }
  65. return cell
  66. }
  67. }