Skip to content

Commit

Permalink
test of goalvc / add data initial stepper value
Browse files Browse the repository at this point in the history
  • Loading branch information
krugerk committed Nov 25, 2024
1 parent 05d65e7 commit 9bd9a5d
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 14 deletions.
8 changes: 8 additions & 0 deletions BeeSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
9B8CA57D24B120CA009C86C2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9B8CA57C24B120CA009C86C2 /* LaunchScreen.storyboard */; };
9BEB2D2B2CF3ED9A00D36ED1 /* GoalViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BEB2D2A2CF3ED9A00D36ED1 /* GoalViewModelTests.swift */; };
9BEB2D2D2CF3EF7E00D36ED1 /* GoalViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BEB2D2C2CF3EF7E00D36ED1 /* GoalViewModel.swift */; };
A10D4E931B07948500A72D29 /* DatapointsTableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A10D4E921B07948500A72D29 /* DatapointsTableView.swift */; };
A10DC2DF207BFCBA00FB7B3A /* RemoveHKMetricViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A10DC2DE207BFCBA00FB7B3A /* RemoveHKMetricViewController.swift */; };
A11A87C61FEBFF7200A43E47 /* ChooseGoalSortViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A11A87C51FEBFF7200A43E47 /* ChooseGoalSortViewController.swift */; };
Expand Down Expand Up @@ -225,6 +227,8 @@

/* Begin PBXFileReference section */
9B8CA57C24B120CA009C86C2 /* LaunchScreen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = "<group>"; };
9BEB2D2A2CF3ED9A00D36ED1 /* GoalViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoalViewModelTests.swift; sourceTree = "<group>"; };
9BEB2D2C2CF3EF7E00D36ED1 /* GoalViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoalViewModel.swift; sourceTree = "<group>"; };
A10D4E921B07948500A72D29 /* DatapointsTableView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatapointsTableView.swift; sourceTree = "<group>"; };
A10DC2DE207BFCBA00FB7B3A /* RemoveHKMetricViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoveHKMetricViewController.swift; sourceTree = "<group>"; };
A11A87C51FEBFF7200A43E47 /* ChooseGoalSortViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChooseGoalSortViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -521,6 +525,7 @@
E43BEA852A036D4300FC3A38 /* LogReaderTests.swift */,
E417572C2A6446FE0029CDDA /* CurrentUserManagerTests.swift */,
E4B6FEC52A776A2900690376 /* GoalTests.swift */,
9BEB2D2A2CF3ED9A00D36ED1 /* GoalViewModelTests.swift */,
);
path = BeeSwiftTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -612,6 +617,7 @@
children = (
A1F9D1E9211B9B7600E2BC93 /* EditDatapointViewController.swift */,
A1BD0D171AEB30A5001EDE8B /* GoalViewController.swift */,
9BEB2D2C2CF3EF7E00D36ED1 /* GoalViewModel.swift */,
A11BC2D81FFAD5BC00E56064 /* TimerViewController.swift */,
);
name = GoalView;
Expand Down Expand Up @@ -1021,6 +1027,7 @@
A1453B3F1AEDFCC8006F48DA /* SignInViewController.swift in Sources */,
A1E618E41E7934C700D8ED93 /* HealthKitConfigTableViewCell.swift in Sources */,
E4B083392932F90400A71564 /* ConfigureHKMetricViewController.swift in Sources */,
9BEB2D2D2CF3EF7E00D36ED1 /* GoalViewModel.swift in Sources */,
E43BEA842A036A9C00FC3A38 /* LogReader.swift in Sources */,
A196CB1F1AE4142F00B90A3E /* GalleryViewController.swift in Sources */,
A1BE73AA1E8B45BF00DEC4DB /* ChooseHKMetricViewController.swift in Sources */,
Expand Down Expand Up @@ -1062,6 +1069,7 @@
E4B0A32E28C194C800055EA7 /* AddDataIntents.intentdefinition in Sources */,
E48E2714296B75E4008013C0 /* TotalSleepMinutesTests.swift in Sources */,
A196CB331AE4142F00B90A3E /* BeeSwiftTests.swift in Sources */,
9BEB2D2B2CF3ED9A00D36ED1 /* GoalViewModelTests.swift in Sources */,
E4B6FEC62A776A2900690376 /* GoalTests.swift in Sources */,
E43BEA862A036D4300FC3A38 /* LogReaderTests.swift in Sources */,
);
Expand Down
3 changes: 2 additions & 1 deletion BeeSwift/Gallery/GalleryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,8 @@ class GalleryViewController: UIViewController, UICollectionViewDelegateFlowLayou
}

func openGoal(_ goal: Goal) {
let goalViewController = GoalViewController(goal: goal)
let viewModel = GoalViewModel(goal: goal)
let goalViewController = GoalViewController(viewModel: viewModel)
self.navigationController?.pushViewController(goalViewController, animated: true)
}

Expand Down
21 changes: 8 additions & 13 deletions BeeSwift/GoalViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ class GoalViewController: UIViewController, UIScrollViewDelegate, DatapointTabl

private let logger = Logger(subsystem: "com.beeminder.com", category: "GoalViewController")

let goal: Goal
var goal: Goal {
viewModel.goal
}

private let viewModel: GoalViewModel

fileprivate var goalImageView = GoalImageView(isThumbnail: false)
fileprivate var datapointTableController = DatapointTableViewController()
Expand All @@ -43,8 +47,8 @@ class GoalViewController: UIViewController, UIScrollViewDelegate, DatapointTabl
// date corresponding to the datapoint to be created
private var date: Date = Date()

init(goal: Goal) {
self.goal = goal
init(viewModel: GoalViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
}

Expand Down Expand Up @@ -212,7 +216,7 @@ class GoalViewController: UIViewController, UIScrollViewDelegate, DatapointTabl
self.dateStepper.tintColor = UIColor.Beeminder.gray
dataEntryView.addSubview(self.dateStepper)
self.dateStepper.addTarget(self, action: #selector(GoalViewController.dateStepperValueChanged), for: .valueChanged)
self.dateStepper.value = Self.makeInitialDateStepperValue(for: goal)
self.dateStepper.value = viewModel.initialDateStepperValue()

self.dateStepper.snp.makeConstraints { (make) -> Void in
make.top.equalTo(self.dateTextField.snp.bottom).offset(elementSpacing)
Expand Down Expand Up @@ -495,15 +499,6 @@ class GoalViewController: UIViewController, UIScrollViewDelegate, DatapointTabl
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.goalImageView
}

private static func makeInitialDateStepperValue(date: Date = Date(), for goal: Goal) -> Double {
let daystampAccountingForTheGoalsDeadline = Daystamp(fromDate: date,
deadline: goal.deadline)
let daystampAssumingMidnightDeadline = Daystamp(fromDate: date,
deadline: 0)

return Double(daystampAssumingMidnightDeadline.distance(to: daystampAccountingForTheGoalsDeadline))
}

// MARK: - SFSafariViewControllerDelegate

Expand Down
24 changes: 24 additions & 0 deletions BeeSwift/GoalViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// GoalViewModel.swift
// BeeSwift
//
// Created by krugerk on 2024-11-25.
//

import Foundation
import Intents

import BeeKit

struct GoalViewModel {
let goal: Goal

public func initialDateStepperValue(date: Date = Date()) -> Double {
let daystampAccountingForTheGoalsDeadline = Daystamp(fromDate: date,
deadline: goal.deadline)
let daystampAssumingMidnightDeadline = Daystamp(fromDate: date,
deadline: 0)

return Double(daystampAssumingMidnightDeadline.distance(to: daystampAccountingForTheGoalsDeadline))
}
}
86 changes: 86 additions & 0 deletions BeeSwiftTests/GoalViewModelTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// GoalViewModelTests.swift
// BeeSwiftTests
//
// Created by krugerk on 2024-11-25.
//

import Testing

@testable import BeeSwift
@testable import BeeKit

struct GoalViewModelTests {

@Test func initialStepperIsMinusOneWhenSubmissionDateIsAfterMidnightAndBeforeDeadline() async throws {
let goal = Self.makeGoalWithDeadline(3600 * 3)
let viewModel = GoalViewModel(goal: goal)
let submissionDate = Calendar.current.date(bySettingHour: 1, minute: 30, second: 0, of: Date())!
let actual = viewModel.initialDateStepperValue(date: submissionDate)
#expect(actual == -1)
}

@Test func initialStepperIsZeroWhenSubmissionDateIsBeforeMidnightAndBeforeDeadline() async throws {
let goal = Self.makeGoalWithDeadline(0)
let viewModel = GoalViewModel(goal: goal)
let submissionDate = Calendar.current.date(bySettingHour: 20, minute: 30, second: 0, of: Date())!
let actual = viewModel.initialDateStepperValue(date: submissionDate)
#expect(actual == 0)
}

@Test func initialStepperIsPlusOneWhenSubmissionDateIsAfterDeadline() async throws {
let goal = Self.makeGoalWithDeadline(3600 * -3)
let viewModel = GoalViewModel(goal: goal)
let submissionDate = Calendar.current.date(bySettingHour: 22, minute: 30, second: 0, of: Date())!
let actual = viewModel.initialDateStepperValue(date: submissionDate)
#expect(actual == 1)
}
}



private extension GoalViewModelTests {
static func makeGoalWithDeadline(_ deadline: Int) -> Goal {
let context = BeeminderPersistentContainer.createMemoryBackedForTests().newBackgroundContext()

let user = User(context: context,
username: "user123",
deadbeat: false,
timezone: "",
defaultAlertStart: 0,
defaultDeadline: 0,
defaultLeadTime: 0)

let goal = Goal(context: context,
owner: user,
id: "goalid",
slug: "goalname",
alertStart: 0,
autodata: nil,
deadline: deadline,
graphUrl: "",
healthKitMetric: "",
hhmmFormat: false,
initDay: 0,
lastTouch: "",
limSum: "",
leadTime: 0,
pledge: 801,
queued: false,
safeBuf: 0,
safeSum: "",
thumbUrl: "",
title: "goaldescription",
todayta: false,
urgencyKey: "urgencyKey",
useDefaults: false,
won: false,
yAxis: "units")

context.perform {
try! context.save()
}

return goal
}
}

0 comments on commit 9bd9a5d

Please sign in to comment.