Skip to content

Commit

Permalink
Support RTL locales
Browse files Browse the repository at this point in the history
  • Loading branch information
fermoya committed May 20, 2022
1 parent 6b8ebd7 commit 9153fc1
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Documentation/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ Pager(...)

<img src="/resources/usage/vertical-pager.gif" alt="Vertical pager" height="640"/>

Pass a direction to `horizontal` or `vertical` to change the scroll direction. For instance, you can have a horizontal `Pager` that scrolls right-to-left:
Pass a direction to `horizontal` or `vertical` to change the scroll direction. For instance, you can have a horizontal `Pager` that scrolls end-to-start:

```swift
Pager(...)
.itemSpacing(10)
.alignment(.start)
.horizontal(.rightToLeft)
.horizontal(.endToStart)
.itemAspectRatio(0.6)
```

Expand Down
4 changes: 2 additions & 2 deletions Example/SwiftUIPagerExample/Examples/BizarreExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ struct BizarreExampleView: View {

Spacer()

Text("Right to left, interactive").bold()
Text("Start to End, interactive").bold()
Pager(page: self.page2,
data: self.data,
id: \.self) {
self.pageView($0)
}
.itemSpacing(10)
.horizontal(.rightToLeft)
.horizontal(.endToStart)
.interactive(scale: 0.8)
.itemAspectRatio(0.7)
.background(Color.gray.opacity(0.5))
Expand Down
21 changes: 19 additions & 2 deletions Sources/SwiftUIPager/PageConfiguration/SwipeDirection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@
//

import Foundation
import SwiftUI

/// Swipe direction for a vertical `Pager`
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public enum HorizontalSwipeDirection {

/// Pages move from left to right
case leftToRight
@available(*, deprecated, message: "Use `startToEnd` instead")
static var leftToRight: Self { .startToEnd }

/// Pages move from right to left
case rightToLeft
@available(*, deprecated, message: "Use `endToStart` instead")
static var rightToLeft: Self { .endToStart }

/// Pages move forwards (i.e. left to right in English, right to left in Arabic)
case startToEnd

/// Pages move backwards (i.e. right to left in English, left to right in Arabic)
case endToStart
}

/// Swipe direction for a horizontal `Pager`
Expand All @@ -29,3 +38,11 @@ public enum VerticalSwipeDirection {
/// Pages move from bottom to top
case bottomToTop
}

extension Locale {
var isRightToLeft: Bool {
guard let language = self.languageCode else { return false }
let direction = Locale.characterDirection(forLanguage: language)
return direction == .rightToLeft
}
}
4 changes: 2 additions & 2 deletions Sources/SwiftUIPager/Pager+Buildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ extension Pager: Buildable {

/// Returns a horizontal pager
///
/// - Parameter swipeDirection: direction of the swipe. Defaults to `.leftToRight`
public func horizontal(_ swipeDirection: HorizontalSwipeDirection = .leftToRight) -> Self {
/// - Parameter swipeDirection: direction of the swipe. Defaults to `.startToEnd`
public func horizontal(_ swipeDirection: HorizontalSwipeDirection = .startToEnd) -> Self {
mutating(keyPath: \.isHorizontal, value: true)
.mutating(keyPath: \.horizontalSwipeDirection, value: swipeDirection)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftUIPager/Pager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
var contentLoadingPolicy: ContentLoadingPolicy = .default

/// Swipe direction for horizontal `Pager`
var horizontalSwipeDirection: HorizontalSwipeDirection = .leftToRight
var horizontalSwipeDirection: HorizontalSwipeDirection = .startToEnd

/// Swipe direction for vertical `Pager`
var verticalSwipeDirection: VerticalSwipeDirection = .topToBottom
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftUIPager/PagerContent+Buildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ extension Pager.PagerContent: Buildable {

/// Returns a horizontal pager
///
/// - Parameter swipeDirection: direction of the swipe. Defaults to `.leftToRight`
func horizontal(_ swipeDirection: HorizontalSwipeDirection = .leftToRight) -> Self {
let scrollDirectionAngle: Angle = swipeDirection == .leftToRight ? .zero : Angle(degrees: 180)
/// - Parameter swipeDirection: direction of the swipe. Defaults to `.startToEnd`
func horizontal(_ swipeDirection: HorizontalSwipeDirection = .startToEnd) -> Self {
let scrollDirectionAngle: Angle = swipeDirection == .startToEnd ? .zero : Angle(degrees: 180)
return mutating(keyPath: \.isHorizontal, value: true)
.mutating(keyPath: \.scrollDirectionAngle, value: scrollDirectionAngle)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftUIPager/PagerContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ extension Pager.PagerContent {
self.pagerModel.draggingVelocity = Double(offsetIncrement) / timeIncrement
}

var newOffset = self.draggingOffset + offsetIncrement
var newOffset = self.draggingOffset + offsetIncrement * (Locale.current.isRightToLeft ? -1 : 1)
if !allowsMultiplePagination {
newOffset = self.direction == .forward ? max(newOffset, self.pageRatio * -self.pageDistance) : min(newOffset, self.pageRatio * self.pageDistance)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftUIPagerTests/Pager+Buildable_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class Pager_Buildable_Tests: XCTestCase {
XCTAssertTrue(pager.isHorizontal)
XCTAssertTrue(pager.allowsDragging)
XCTAssertFalse(pager.isInifinitePager)
XCTAssertEqual(pager.horizontalSwipeDirection, HorizontalSwipeDirection.leftToRight)
XCTAssertEqual(pager.horizontalSwipeDirection, HorizontalSwipeDirection.startToEnd)
XCTAssertEqual(pager.verticalSwipeDirection, VerticalSwipeDirection.topToBottom)
XCTAssertEqual(pager.interactiveScale, 1)
XCTAssertEqual(pager.alignment, .center)
Expand Down Expand Up @@ -316,7 +316,7 @@ final class Pager_Buildable_Tests: XCTestCase {

func test_GivenPager_WhenHorizontalRightToLeft_ThenScrollAngle() {
var pager = givenPager.vertical()
pager = pager.horizontal(.rightToLeft)
pager = pager.horizontal(.endToStart)

let pagerContent = pager.content(for: CGSize(width: 100, height: 100))
XCTAssertTrue(pagerContent.isHorizontal)
Expand Down

0 comments on commit 9153fc1

Please sign in to comment.