Skip to content

Commit

Permalink
fix: find falcone button stays disabled (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabmaiti authored Mar 16, 2024
1 parent 15755e1 commit 5e9dbde
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,8 @@ final class Destination: ObservableObject, Identifiable {

final class SelectDestinationsDataModel: ObservableObject {
@Published var destinations: [Destination]

var totalTimeTaken: Int {
return destinations.map{ $0.timeTaken }.reduce(0) { $0 + $1 }
}

var shouldButtonDisabled: Bool {
var count = 0
while count < destinations.count {
if (destinations[count].selectedVehicle == nil) {
return true
}
count += 1
}
return false
}
@Published var totalTimeTaken: Int = 0
@Published var shouldFindFalconeDisabled = true

required init(destinations: [Destination] = [
Destination(name: "Destination 1"),
Expand All @@ -83,6 +70,8 @@ final class SelectDestinationsDataModel: ObservableObject {

func reset() {
destinations.forEach{ $0.reset() }
totalTimeTaken = 0
shouldFindFalconeDisabled = true
}

func updateNextDestinationVehicleList(destination: Destination) {
Expand All @@ -99,6 +88,8 @@ final class SelectDestinationsDataModel: ObservableObject {
) }
destinations[index + 1] = nextDestination
}
totalTimeTaken = destinations.map{ $0.timeTaken }.reduce(0) { $0 + $1 }
shouldFindFalconeDisabled = shouldDisabled()
}

func updateNextDestinationPlanetList(destination: Destination) {
Expand All @@ -113,4 +104,15 @@ final class SelectDestinationsDataModel: ObservableObject {
destinations[index + 1] = nextDestination
}
}

private func shouldDisabled() -> Bool {
var count = 0
while count < destinations.count {
if (destinations[count].selectedVehicle == nil) {
return true
}
count += 1
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import SwiftUI
import Combine

protocol SelectDestinationsDisplayLogic {
func displayPlanetsAndVehicles(viewModel: SelectDestinations.LoadPlanetsAndVehicles.ViewModel)
Expand Down Expand Up @@ -52,7 +51,7 @@ struct SelectDestinationsView: View {
} label: {
Text("Find Falcone")
}
.disabled(dataModel.shouldButtonDisabled)
.disabled(dataModel.shouldFindFalconeDisabled)
}
}
.onAppear{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import SwiftUI
import Combine

struct DestinationSelectionView: View {
@ObservedObject var destination: Destination
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class SelectDestinationsDataModelTests: XCTestCase {
let destination2: Destination = .mockRandom(name: .mockRandom(), planetList: [], vehicleList: [], selectedVehicle: nil, selectedPlanet: nil)
let dataModel = SelectDestinationsDataModel(destinations: [destination1, destination2])

XCTAssertTrue(dataModel.shouldButtonDisabled)
XCTAssertTrue(dataModel.shouldFindFalconeDisabled)
XCTAssertEqual(dataModel.totalTimeTaken, 0)
XCTAssertEqual(destination1.planetList, [])
XCTAssertEqual(destination1.vehicleList, [])
Expand Down Expand Up @@ -86,7 +86,7 @@ final class SelectDestinationsDataModelTests: XCTestCase {

destination2.selectedPlanet = planet1
destination2.selectedVehicle = vehicle1
XCTAssertFalse(dataModel.shouldButtonDisabled)
XCTAssertFalse(dataModel.shouldFindFalconeDisabled)

dataModel.reset()

Expand Down

0 comments on commit 5e9dbde

Please sign in to comment.