Skip to content

Commit

Permalink
[CM-1318] Add key command for navigation (#7)
Browse files Browse the repository at this point in the history
* rebase

* [UPDATE] review comments resolved

* [UPDATE] resolved review comment

* [UPDATE] added test file
  • Loading branch information
SahilSainiYML authored Apr 10, 2023
1 parent 6da3f5b commit 358f0ab
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PackageDescription

let package = Package(
name: "YCarousel",
defaultLocalization: "en",
platforms: [
.iOS(.v14)
],
Expand All @@ -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: [
Expand Down
9 changes: 9 additions & 0 deletions Sources/YCarousel/Assets/Strings/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Localizable.strings


Created by Sahil Saini on 05/04/23.

*/
"Previous_Arrow_Button" = "Previous";
"Next_Arrow_Button" = "Next";
12 changes: 11 additions & 1 deletion Sources/YCarousel/CarouselView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class CarouselView: UIView {
}
}
}

private let pageControlBottomSpacing: CGFloat = 16
private var viewProvider: CarouselViewProvider?

Expand Down Expand Up @@ -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..<numberOfPages ~= index else {
return
}
currentPage = index
gotoPage(at: index)
}
}

internal extension CarouselView {
Expand Down
33 changes: 33 additions & 0 deletions Sources/YCarousel/CarouselViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import UIKit
public class CarouselViewController: UIViewController, CarouselViewDelegate, CarouselViewDataSource {
private let pages: [CarouselPage]

/// Enables keyboard navigation. Default is `true`.
public var isKeyboardNavigationEnabled: Bool = true

/// The carousel view managed by this controller
public var carouselView: CarouselView! { view as? CarouselView }

Expand All @@ -37,6 +40,7 @@ public class CarouselViewController: UIViewController, CarouselViewDelegate, Car
carouselView.dataSource = self
carouselView.delegate = self
self.view = carouselView
configureKeys()
}

// MARK: - CarouselViewDelegate
Expand Down Expand Up @@ -95,3 +99,32 @@ public class CarouselViewController: UIViewController, CarouselViewDelegate, Car
/// - Returns: UIView
public func carouselView(pageAt index: Int) -> 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)
}
}
22 changes: 22 additions & 0 deletions Sources/YCarousel/Enums/CarouselViewController+Strings.swift
Original file line number Diff line number Diff line change
@@ -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 }
}
}
61 changes: 61 additions & 0 deletions Tests/YCarouselTests/CarouselViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 358f0ab

Please sign in to comment.