VLCSettingsSpecifierManager.swift 3.2 KB

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