VLCSettingsSpecifierManager.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. assertionFailure("VLCSettingsSpecifierManager: No rows provided for \(specifier?.key() ?? "null specifier")")
  20. return []
  21. }
  22. return items
  23. }
  24. @objc var selectedIndex: IndexPath {
  25. let index: Int
  26. if let selectedItem = settingsStore.object(forKey: specifier?.key()) {
  27. index = items.index(of: selectedItem)
  28. } else if let specifier = specifier {
  29. index = items.index(of: specifier.defaultValue() as Any)
  30. } else {
  31. fatalError("VLCSettingsSpecifierManager: No specifier provided")
  32. }
  33. return IndexPath(row: index, section: 0)
  34. }
  35. @objc init(settingsReader: IASKSettingsReader, settingsStore: IASKSettingsStore) {
  36. self.settingsReader = settingsReader
  37. self.settingsStore = settingsStore
  38. super.init()
  39. }
  40. }
  41. // MARK: VLCActionSheetDelegate
  42. extension VLCSettingsSpecifierManager: ActionSheetDelegate {
  43. func headerViewTitle() -> String? {
  44. return specifier?.title()
  45. }
  46. func itemAtIndexPath(_ indexPath: IndexPath) -> Any? {
  47. return items[indexPath.row]
  48. }
  49. func actionSheet(collectionView: UICollectionView, didSelectItem item: Any, At indexPath: IndexPath) {
  50. settingsStore.setObject(item, forKey: specifier?.key())
  51. settingsStore.synchronize()
  52. }
  53. }
  54. // MARK: VLCActionSheetDataSource
  55. extension VLCSettingsSpecifierManager: ActionSheetDataSource {
  56. func numberOfRows() -> Int {
  57. return items.count
  58. }
  59. func actionSheet(collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  60. guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ActionSheetCell.identifier, for: indexPath) as? ActionSheetCell else {
  61. return UICollectionViewCell()
  62. }
  63. if let titles = specifier?.multipleTitles(), indexPath.row < titles.count {
  64. cell.name.text = settingsReader.title(forStringId: titles[indexPath.row] as? String)
  65. }
  66. return cell
  67. }
  68. }