MediaPreviewCarouselAccessibilityElement.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import UIKit
  21. class MediaPreviewCarouselAccessibilityElement : UIAccessibilityElement {
  22. var currentMediaItem: IndexPath?
  23. init(accessibilityContainer: Any, currentMediaItem : IndexPath?) {
  24. super.init(accessibilityContainer : accessibilityContainer)
  25. self.currentMediaItem = currentMediaItem
  26. }
  27. override var accessibilityLabel: String? {
  28. get {
  29. let text = String(format: NSLocalizedString("selected_media_list", comment: ""))
  30. return text
  31. }
  32. set {
  33. super.accessibilityLabel = newValue
  34. }
  35. }
  36. override var accessibilityValue: String? {
  37. get {
  38. let text = String(format: NSLocalizedString("media_item_of", comment: ""), "\(currentMediaItem!.item + 1)", " \(getTotalItems())")
  39. guard let containerView = accessibilityContainer as? MediaPreviewCarouselContainerView else {
  40. return text
  41. }
  42. guard let delegate = containerView.delegate else {
  43. return text
  44. }
  45. guard let index = self.currentMediaItem else {
  46. return text
  47. }
  48. guard let description = delegate.mediaData[index.item].getAccessiblityDescription() else {
  49. return text
  50. }
  51. return text + description
  52. }
  53. set {
  54. super.accessibilityValue = newValue
  55. }
  56. }
  57. override var accessibilityTraits: UIAccessibilityTraits {
  58. get {
  59. return UIAccessibilityTraits.adjustable
  60. }
  61. set {
  62. super.accessibilityTraits = newValue
  63. }
  64. }
  65. func getTotalItems() -> Int {
  66. guard let containerView = accessibilityContainer as? MediaPreviewCarouselContainerView else {
  67. return 0
  68. }
  69. guard let delegate = containerView.delegate else {
  70. return 0
  71. }
  72. guard let collectionView = delegate.largeCollectionView else {
  73. return 0
  74. }
  75. guard self.currentMediaItem != nil else {
  76. return 0
  77. }
  78. return collectionView.numberOfItems(inSection: 0)
  79. }
  80. func accessibilityScrollCollectionView(forwards: Bool) -> Bool {
  81. guard let containerView = accessibilityContainer as? MediaPreviewCarouselContainerView else {
  82. return false
  83. }
  84. guard let delegate = containerView.delegate else {
  85. return false
  86. }
  87. guard let collectionView = delegate.largeCollectionView else {
  88. return false
  89. }
  90. guard let index = self.currentMediaItem else {
  91. return false
  92. }
  93. if forwards {
  94. if collectionView.numberOfItems(inSection: 0) - 1 < index.item + 1 {
  95. return false
  96. }
  97. let newIndex = IndexPath.init(item: index.item + 1, section: 0)
  98. delegate.shouldScrollTo(indexPath: newIndex)
  99. self.currentMediaItem = newIndex
  100. } else {
  101. if index.item - 1 < 0 {
  102. return false
  103. }
  104. let newIndex = IndexPath.init(item: index.item - 1, section: 0)
  105. delegate.shouldScrollTo(indexPath: newIndex)
  106. self.currentMediaItem = newIndex
  107. }
  108. return true
  109. }
  110. override func accessibilityIncrement() {
  111. _ = accessibilityScrollCollectionView(forwards: true)
  112. }
  113. override func accessibilityDecrement() {
  114. _ = accessibilityScrollCollectionView(forwards: false)
  115. }
  116. override func accessibilityScroll(_ direction: UIAccessibilityScrollDirection) -> Bool {
  117. if direction == .left {
  118. return accessibilityScrollCollectionView(forwards: true)
  119. } else if direction == .right {
  120. return accessibilityScrollCollectionView(forwards: false)
  121. }
  122. return false
  123. }
  124. }