diff --git a/Package.swift b/Package.swift index 81eb90f..d939453 100644 --- a/Package.swift +++ b/Package.swift @@ -4,6 +4,7 @@ import PackageDescription let package = Package( name: "YCarousel", + defaultLocalization: "en", platforms: [ .iOS(.v14) ], @@ -16,7 +17,7 @@ let package = Package( dependencies: [ .package( url: "https://github.com/yml-org/YCoreUI.git", - from: "1.5.0" + from: "1.6.0" ) ], targets: [ diff --git a/Sources/YCarousel/Assets/Strings/en.lproj/Localizable.strings b/Sources/YCarousel/Assets/Strings/en.lproj/Localizable.strings new file mode 100644 index 0000000..425e972 --- /dev/null +++ b/Sources/YCarousel/Assets/Strings/en.lproj/Localizable.strings @@ -0,0 +1,9 @@ +/* + Localizable.strings + + + Created by Sahil Saini on 05/04/23. + +*/ +"Previous_Arrow_Button" = "Previous"; +"Next_Arrow_Button" = "Next"; diff --git a/Sources/YCarousel/CarouselView.swift b/Sources/YCarousel/CarouselView.swift index f817a78..dbe2df5 100644 --- a/Sources/YCarousel/CarouselView.swift +++ b/Sources/YCarousel/CarouselView.swift @@ -50,7 +50,7 @@ public class CarouselView: UIView { } } } - + private let pageControlBottomSpacing: CGFloat = 16 private var viewProvider: CarouselViewProvider? @@ -118,6 +118,16 @@ public class CarouselView: UIView { } return nil } + + /// Load view at index + /// - Parameter index: index of view to load + public func loadView(at index: Int) { + guard 0.. UIView { pages[index].view } } + +// MARK: - UIKeyCommand + +internal extension CarouselViewController { + func configureKeys() { + let rightArrow = UIKeyCommand( + title: CarouselViewController.Strings.next.localized, + action: #selector(rightArrowKeyPressed), + input: UIKeyCommand.inputRightArrow + ) + let leftArrow = UIKeyCommand( + title: CarouselViewController.Strings.previous.localized, + action: #selector(leftArrowKeyPressed), + input: UIKeyCommand.inputLeftArrow + ) + addKeyCommand(leftArrow) + addKeyCommand(rightArrow) + } + + @objc func leftArrowKeyPressed() { + guard isKeyboardNavigationEnabled else { return } + carouselView.loadView(at: carouselView.currentPage - 1) + } + + @objc func rightArrowKeyPressed() { + guard isKeyboardNavigationEnabled else { return } + carouselView.loadView(at: carouselView.currentPage + 1) + } +} diff --git a/Sources/YCarousel/Enums/CarouselViewController+Strings.swift b/Sources/YCarousel/Enums/CarouselViewController+Strings.swift new file mode 100644 index 0000000..bb79abc --- /dev/null +++ b/Sources/YCarousel/Enums/CarouselViewController+Strings.swift @@ -0,0 +1,22 @@ +// +// CarouselViewController+Strings.swift +// YCarousel +// +// Created by Sahil Saini on 05/04/23. +// Copyright © 2023 Y Media Labs. All rights reserved. +// + +import Foundation +import YCoreUI + +extension CarouselViewController { + /// Strings + enum Strings: String, Localizable, CaseIterable { + /// Buttons + case previous = "Previous_Arrow_Button" + case next = "Next_Arrow_Button" + + /// Bundle + static var bundle: Bundle { .module } + } +} diff --git a/Tests/YCarouselTests/CarouselViewControllerTests.swift b/Tests/YCarouselTests/CarouselViewControllerTests.swift index cfe7e18..4a41f3e 100644 --- a/Tests/YCarouselTests/CarouselViewControllerTests.swift +++ b/Tests/YCarouselTests/CarouselViewControllerTests.swift @@ -82,6 +82,67 @@ final class CarouselViewControllerTests: XCTestCase { XCTAssertTrue(sut.pageWillUnload) XCTAssertTrue(sut.pageWillUnload) } + + func test_pressKeyboardLeft_deliversPreviousPage() { + // Given + let sut = makeSUT(withViews: [UIView(), UIView()]) + sut.carouselView.loadPage(at: 1) + // When + sut.leftArrowKeyPressed() + // Then + XCTAssertEqual(sut.carouselView.currentPage, 0) + } + + func test_pressKeyboardLeftOnFirstPage_deliversNothing() { + // Given + let sut = makeSUT(withViews: [UIView(), UIView()]) + XCTAssertEqual(sut.carouselView.currentPage, 0) + // When + sut.leftArrowKeyPressed() + // Then + XCTAssertEqual(sut.carouselView.currentPage, 0) + } + + func test_pressKeyboardRight_deliversNextPage() { + // Given + let sut = makeSUT(withViews: [UIView(), UIView()]) + XCTAssertEqual(sut.carouselView.currentPage, 0) + // When + sut.rightArrowKeyPressed() + // Then + XCTAssertEqual(sut.carouselView.currentPage, 1) + } + + func test_pressKeyboardRightFromLastPage_deliversNothing() { + // Given + let sut = makeSUT(withViews: [UIView(), UIView()]) + sut.carouselView.loadView(at: 1) + // When + sut.rightArrowKeyPressed() + // Then + XCTAssertEqual(sut.carouselView.currentPage, 1) + } + + func test_pressKeyboardLeftWhenDisabled_deliversNothing() { + // Given + let sut = makeSUT(withViews: [UIView(), UIView()]) + sut.carouselView.loadView(at: 1) + sut.isKeyboardNavigationEnabled = false + // When + sut.leftArrowKeyPressed() + // Then + XCTAssertEqual(sut.carouselView.currentPage, 1) + } + + func test_pressKeyboardRightWhenDisabled_deliversNothing() { + // Given + let sut = makeSUT(withViews: [UIView(), UIView()]) + sut.isKeyboardNavigationEnabled = false + // When + sut.rightArrowKeyPressed() + // Then + XCTAssertEqual(sut.carouselView.currentPage, 0) + } } private extension CarouselViewControllerTests { diff --git a/Tests/YCarouselTests/Enums/CarouselViewController+StringsTests.swift b/Tests/YCarouselTests/Enums/CarouselViewController+StringsTests.swift new file mode 100644 index 0000000..46e19f8 --- /dev/null +++ b/Tests/YCarouselTests/Enums/CarouselViewController+StringsTests.swift @@ -0,0 +1,23 @@ +// +// CarouselViewController+StringsTests.swift +// YCarousel +// +// Created by Sahil Saini on 07/04/23. +// Copyright © 2023 Y Media Labs. All rights reserved. +// + +import XCTest +@testable import YCarousel + +final class CarouselViewControllerStringsTests: XCTestCase { + func testLoadStrings() { + CarouselViewController.Strings.allCases.forEach { + // Given a localized string constant + let string = $0.localized + // should not be empty + XCTAssertFalse(string.isEmpty) + // should not equal its key + XCTAssertNotEqual($0.rawValue, string) + } + } +}