Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 3.2.1 (5) #38

Merged
merged 2 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DailyDozen/DailyDozen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = DailyDozen/DailyDozen.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 7;
DEVELOPMENT_TEAM = 2MG57YUZL5;
INFOPLIST_FILE = "$(SRCROOT)/DailyDozen/App/SupportingFiles/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 12.4;
Expand All @@ -1924,7 +1924,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = DailyDozen/DailyDozen.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 7;
DEVELOPMENT_TEAM = 2MG57YUZL5;
INFOPLIST_FILE = "$(SRCROOT)/DailyDozen/App/SupportingFiles/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 12.4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class FirstLaunchViewController: UIViewController {
userInfo: nil)
}

// :???: Main.storyboard appears to no longer be in use.
func prepareNextViewController() {
UserDefaults.standard.set(true, forKey: "didSee")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
Expand Down
8 changes: 2 additions & 6 deletions DailyDozen/DailyDozen/Database/DataRecords/DailyTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@ struct DailyTracker {
if let value = Int(countText) {
setCount(typeKey: typeKey, count: value)
} else {
LogService.shared.error(
"DailyTracker setCount() countText \(countText) not convertable"
)
LogService.shared.error("DailyTracker setCount() countText \(countText) not convertable")
}
}

func setCount(typeKey: DataCountType, count: Int) {
if let dataCountRecord = itemsDict[typeKey] {
dataCountRecord.setCount(count)
} else {
LogService.shared.error(
"DailyTracker setCount() type not found \(typeKey.typeKey)"
)
LogService.shared.error("DailyTracker setCount() type not found \(typeKey.typeKey)")
}
}

Expand Down
20 changes: 12 additions & 8 deletions DailyDozen/DailyDozen/Database/RealmProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class RealmProvider {
.sorted(byKeyPath: "pid")

if counterResultsById.count > 0 && weightResultsById.count > 0 {
return getDailyTrackersAll(counterResults: counterResultsById, weightResults: weightResultsById)
return getDailyTrackersMerged(counterResults: counterResultsById, weightResults: weightResultsById)
} else if counterResultsById.count > 0 {
return getDailyTrackersCountersOnly(counterResults: counterResultsById)
} else if weightResultsById.count > 0 {
Expand All @@ -156,24 +156,28 @@ class RealmProvider {
}
}

/// requires presort from lower to higher datestamps
private func getDailyTrackersAll(counterResults: Results<DataCountRecord>, weightResults: Results<DataWeightRecord>) -> [DailyTracker] {
/// Merges counts and weights in to single [DailyTracker] array
private func getDailyTrackersMerged(counterResults: Results<DataCountRecord>, weightResults: Results<DataWeightRecord>) -> [DailyTracker] {
var allTrackers = [DailyTracker]()

let counterResults = counterResults.sorted(byKeyPath: "pid")
let weightResults = weightResults.sorted(byKeyPath: "pid")

var thisCounterRecord: DataCountRecord = counterResults[0]
var thisCounterDatestamp = thisCounterRecord.pidKeys.datestampKey
var thisCounterDatestamp = thisCounterRecord.pidKeys.datestampKey // yyyyMMdd
guard let thisCounterDate = Date(datestampKey: thisCounterDatestamp) else { return allTrackers }

var thisWeightRecord: DataWeightRecord = weightResults[0]
var thisWeightDatestamp = thisWeightRecord.pidKeys.datestampKey
var thisWeightDatestamp = thisWeightRecord.pidKeys.datestampKey // yyyyMMdd
guard let thisWeightDate = Date(datestampKey: thisWeightDatestamp) else { return allTrackers }

var counterIndex = 0
var weightIndex = 0
var lastDatestamp = thisCounterDatestamp
var tracker = DailyTracker(date: thisCounterDate)

if thisWeightDatestamp > thisCounterDatestamp {
if thisCounterDatestamp > thisWeightDatestamp {
// start with earliest datestamp
lastDatestamp = thisWeightDatestamp
tracker = DailyTracker(date: thisWeightDate)
}
Expand All @@ -196,7 +200,7 @@ class RealmProvider {
weightIndex += 1
} else {
allTrackers.append(tracker)
// take the lesser of CounterDatestamp or WeightDatestamp
// take the earlier of CounterDatestamp or WeightDatestamp
if thisCounterDatestamp <= thisWeightDatestamp {
lastDatestamp = thisCounterDatestamp
if let date = Date(datestampKey: thisCounterDatestamp) {
Expand All @@ -208,9 +212,9 @@ class RealmProvider {
tracker = DailyTracker(date: date)
} else { return allTrackers } // early fail if datestamp invalid
}

}
}

// Append remaining counters
if counterIndex < counterResults.count {
while counterIndex < counterResults.count {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,22 @@ extension DozeEntryViewController: UICollectionViewDelegate {
cell.configure(with: checkmarkStates[indexPath.row])
let itemType = dataProvider.viewModel.itemType(rowIndex: rowIndex)

// Update Streak
let countMax = checkmarkStates.count
// Update Tracker Count
let countNow = checkmarkStates.filter { $0 }.count
var streak = countMax == countNow ? 1 : 0
realm.saveCount(countNow, pid: itemPid)

// :!!!: streak needs to include more than today+yesterday

// Update Tracker Streak
// :NYI: streak needs to include more than today+yesterday
var streak = checkmarkStates.count == countNow ? 1 : 0
if streak > 0 {
let yesterday = dataProvider.viewModel.trackerDate.adding(.day, value: -1)!
// previous day's streak +1
// :NYI: just read the streak for item from Realm given PID
let yesterdayTracker = realm.getDailyTracker(date: yesterday)
if let yesterdayStreak = yesterdayTracker.itemsDict[itemType]?.streak {
streak += yesterdayStreak
}
}

realm.updateStreak(streak, pid: itemPid)

tableView.reloadData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,22 @@ class DozeEntryDataProvider: NSObject, UITableViewDataSource {
if servingsSection == .supplements {
rowIndex += tableView.numberOfRows(inSection: 0)
}
let itemType = viewModel.itemType(rowIndex: rowIndex)

// Determine Tracker Streak value for this itemType
let states = viewModel.dozeItemStates(rowIndex: rowIndex)
let countMax = states.count
let countNow = states.filter { $0 }.count
var streak = countMax == countNow ? 1 : 0

var streak = states.count == countNow ? 1 : 0
if streak > 0 {
let yesterday = viewModel.trackerDate.adding(.day, value: -1)!
// previous streak +1
// :NYI: just read the streak for item from Realm given PID
let yesterdayItems = realm.getDailyTracker(date: yesterday).itemsDict
let itemType = viewModel.itemType(rowIndex: rowIndex)
if let yesterdayStreak = yesterdayItems[itemType]?.streak {
streak += yesterdayStreak
}
}

let itemType = viewModel.itemInfo(rowIndex: rowIndex).itemType
dozeTableViewCell.configure(
heading: itemType.headingDisplay,
tag: rowIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class TweakEntryPagerViewController: UIViewController {
// MARK: - Properties
private var currentDate = Date() {
didSet {
LogService.shared.debug("@DATE \(currentDate.datestampKey) TweakEntryPagerViewController")
if currentDate.isInCurrentDayWith(Date()) {
backButton.superview?.isHidden = true
dateButton.setTitle("Today", for: .normal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class TweakEntryViewController: UIViewController {
///
/// - Parameter item: The current date.
func setViewModel(date: Date) {
LogService.shared.debug("TweakEntryViewController setViewModel \(date.datestampyyyyMMddHHmmss)")
LogService.shared.debug("@DATE \(date.datestampKey) TweakEntryViewController setViewModel")
dataProvider.viewModel = TweakEntryViewModel(tracker: realm.getDailyTracker(date: date))

// Update N/MAX daily checked items count
Expand Down Expand Up @@ -199,22 +199,22 @@ extension TweakEntryViewController: UICollectionViewDelegate {
cell.configure(with: checkmarkStates[indexPath.row])
let dataCountType = dataProvider.viewModel.itemType(rowIndex: rowIndex)

// Update Streak
let countMax = checkmarkStates.count
// Update Tracker Count
let countNow = checkmarkStates.filter { $0 }.count
var streak = countMax == countNow ? 1 : 0
realm.saveCount(countNow, pid: itemPid)

// :!!!: streak needs to include more than today+yesterday
// Update Tracker Streak
// :NYI: streak needs to include more than today+yesterday
var streak = checkmarkStates.count == countNow ? 1 : 0
if streak > 0 {
let yesterday = dataProvider.viewModel.trackerDate.adding(.day, value: -1)!
// previous day's streak +1
// :NYI: just read the streak for item from Realm given PID
let yesterdayTracker = realm.getDailyTracker(date: yesterday)
if let yesterdayStreak = yesterdayTracker.itemsDict[dataCountType]?.streak {
streak += yesterdayStreak
}
}

realm.updateStreak(streak, pid: itemPid)

tableView.reloadData()
Expand All @@ -223,13 +223,14 @@ extension TweakEntryViewController: UICollectionViewDelegate {

tweakDailyStateCount += stateTrueCounterNew - stateTrueCounterOld

// If state was toggled on then go to weight editor
if stateNew && HealthManager.shared.isAuthorized() {
let dataCountType = dataProvider.viewModel.itemType(rowIndex: rowIndex)
if dataCountType == .tweakWeightTwice {
let viewController = WeightEntryPagerBuilder.instantiateController()
navigationController?.pushViewController(viewController, animated: true)
}
// Weight Editor
if dataCountType == .tweakWeightTwice {
// Go to the weight editor
let date = dataProvider.viewModel.trackerDate
let viewController = WeightEntryPagerBuilder.instantiateController(date: date)
if let navigationController = navigationController {
navigationController.pushViewController(viewController, animated: true)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ class TweakEntryDataProvider: NSObject, UITableViewDataSource {
}

let rowIndex = indexPath.row
let itemType = viewModel.itemType(rowIndex: rowIndex)

// Determine Tracker Streak value for this itemType
let states = viewModel.tweakItemStates(rowIndex: rowIndex)
let countMax = states.count
let countNow = states.filter { $0 }.count
var streak = countMax == countNow ? 1 : 0

let itemType = viewModel.itemType(rowIndex: rowIndex)
var streak = states.count == countNow ? 1 : 0
if streak > 0 {
let yesterday = viewModel.trackerDate.adding(.day, value: -1)!
// previous streak +1
// :NYI: just read the streak for item from Realm given PID
let yesterdayItems = realm.getDailyTracker(date: yesterday).itemsDict
if let yesterdayStreak = yesterdayItems[itemType]?.streak {
streak += yesterdayStreak
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class TweakEntryViewModel {
// MARK: - Inits
init(tracker: DailyTracker) {
self.tracker = tracker
LogService.shared.debug("@DATE \(tracker.date.datestampKey) TweakEntryViewModel.init()")
}

// MARK: - Methods
Expand Down Expand Up @@ -94,9 +95,7 @@ class TweakEntryViewModel {
}
}
if rowIndex == 16 {
LogService.shared.verbose(
"# TweakEntryViewModel itemStates \(rowIndex):\(itemPid(rowIndex: rowIndex)) \(states)"
)
LogService.shared.verbose("# TweakEntryViewModel itemStates \(rowIndex):\(itemPid(rowIndex: rowIndex)) \(states)")
}
return states
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class UtilityTableViewController: UITableViewController {
/// Presents share services.
private func doUtilityDBExportData() { // see also presentShareServices() { // Backup
let realmMngr = RealmManager()
let backupFilename = realmMngr.csvExport(marker: "export_data")
let backupFilename = realmMngr.csvExport(marker: "db_export_data")
#if DEBUG
_ = realmMngr.csvExportWeight(marker: "export_weight")
HealthSynchronizer.shared.syncWeightExport(marker: "export_weight_hk")
_ = realmMngr.csvExportWeight(marker: "db_export_weight")
HealthSynchronizer.shared.syncWeightExport(marker: "hk_export_weight")
#endif

let str = "\(Strings.utilityDbExportMsg): \"\(backupFilename)\"."
Expand Down
Loading