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

Implemented TaskCategory entity #355 #386

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
120 changes: 120 additions & 0 deletions CareKit/CareKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,18 @@ open class OCKDailyTasksPageViewController: OCKDailyPageViewController, OCKDaily
private let emptyLabelMargin: CGFloat = 4

// MARK: Properties
private (set) var taskCategory: OCKTaskCategory?

/// If set, the delegate will receive callbacks when important events happen at the task view controller level.
public weak var tasksDelegate: OCKDailyTasksPageViewControllerDelegate?

// MARK: - Life Cycle

public init(storeManager: OCKSynchronizedStoreManager, taskCategory: OCKTaskCategory? = nil) {
self.taskCategory = taskCategory
super.init(storeManager: storeManager)
}

override open func viewDidLoad() {
super.viewDidLoad()
tasksDelegate = self
Expand All @@ -66,7 +72,10 @@ open class OCKDailyTasksPageViewController: OCKDailyPageViewController, OCKDaily
// MARK: - Methods

private func fetchTasks(for date: Date, andPopulateIn listViewController: OCKListViewController) {
let taskQuery = OCKTaskQuery(for: date)
var taskQuery = OCKTaskQuery(for: date)
if let category = taskCategory {
taskQuery.taskCategoryIDs = [category.id]
}
storeManager.store.fetchAnyTasks(query: taskQuery, callbackQueue: .main) { [weak self] result in
guard let self = self else { return }
switch result {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
/*
Copyright (c) 2020, Apple Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3. Neither the name of the copyright holder(s) nor the names of any contributors
may be used to endorse or promote products derived from this software without
specific prior written permission. No license is granted to the trademarks of
the copyright holders even if such marks are included in this software.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import CareKitStore
import CareKitUI
import Combine
import Foundation
import UIKit

/// Classes that conform to this protocol can recieve updates about the state of
/// the `OCKTaskCategoriesListViewControllerDelegate`.
public protocol OCKTaskCategoriesListViewControllerDelegate: AnyObject {
func taskCategoriesListViewController(_ viewController: OCKTaskCategoriesListViewController, didEncounterError: Error)
}

/// An `OCKListViewController` that automatically queries and displays taskCategories in the `Store` using
/// `OCKDetailedTaskCategoryViewController`s.
open class OCKTaskCategoriesListViewController: OCKListViewController {

// MARK: Properties

/// The manager of the `Store` from which the `TaskCategory` data is fetched.
public let storeManager: OCKSynchronizedStoreManager

/// If set, the delegate will receive callbacks when important events happen at the list view controller level.
public weak var delegate: OCKTaskCategoriesListViewControllerDelegate?

/// If set, the delegate will recdeive callbacks when important events happen inside the taskCategory view controllers.
public weak var taskCategoryDelegate: OCKTaskCategoryViewControllerDelegate?

private var subscription: Cancellable?

// MARK: - Life Cycle

/// Initialize using a store manager. All of the taskCategories in the store manager will be queried and dispalyed.
///
/// - Parameters:
/// - storeManager: The store manager owning the store whose taskCategories should be displayed.
public init(storeManager: OCKSynchronizedStoreManager) {
self.storeManager = storeManager
super.init(nibName: nil, bundle: nil)
}

@available(*, unavailable)
public required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override open func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
title = loc("TASKCATEGORIES")
kutakmir marked this conversation as resolved.
Show resolved Hide resolved
subscribe()
fetchTaskCategories()
}

// MARK: - Methods

private func subscribe() {
subscription?.cancel()
subscription = storeManager.taskCategoriesPublisher(categories: [.add, .update]).sink { _ in
self.fetchTaskCategories()
}
}

/// `fetchTaskCategories` asynchronously retrieves an array of taskCategories stored in a `Result`
/// and makes corresponding `OCKDetailedTaskCategoryViewController`s.
private func fetchTaskCategories() {
storeManager.store.fetchAnyTaskCategories(query: OCKTaskCategoryQuery(), callbackQueue: .main) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
self.delegate?.taskCategoriesListViewController(self, didEncounterError: error)
case .success(let taskCategories):
self.clear()
for taskCategory in taskCategories {
let taskCategoryViewController = OCKDetailedTaskCategoryViewController(taskCategory: taskCategory, storeManager: self.storeManager)
taskCategoryViewController.delegate = self.taskCategoryDelegate
self.appendViewController(taskCategoryViewController, animated: false)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public struct OCKContactNotification: OCKStoreNotification {
public let storeManager: OCKSynchronizedStoreManager
}

public struct OCKTaskCategoryNotification: OCKStoreNotification {
public let taskCategory: OCKAnyTaskCategory
public let category: OCKStoreNotificationCategory
public let storeManager: OCKSynchronizedStoreManager
}
public struct OCKTaskNotification: OCKStoreNotification {
public let task: OCKAnyTask
public let category: OCKStoreNotificationCategory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,41 @@ extension OCKSynchronizedStoreManager {
return fetchImmediately ? AnyPublisher(changePublisher.prepend(presentValuePublisher)) : AnyPublisher(changePublisher)
}

// MARK: TaskCategories

func taskCategoriesPublisher(categories: [OCKStoreNotificationCategory]) -> AnyPublisher<OCKAnyTaskCategory, Never> {
return AnyPublisher(notificationPublisher
.compactMap { $0 as? OCKTaskCategoryNotification }
.filter { categories.contains($0.category) }
.map { $0.taskCategory })
}

func publisher(forTaskCategoryID id: String,
categories: [OCKStoreNotificationCategory]) -> AnyPublisher<OCKAnyTaskCategory, Never> {
return notificationPublisher
.compactMap { $0 as? OCKTaskCategoryNotification }
.filter { $0.taskCategory.id == id && categories.contains($0.category) }
.map { $0.taskCategory }
.eraseToAnyPublisher()
}

func publisher(forTaskCategory taskCategory: OCKAnyTaskCategory,
categories: [OCKStoreNotificationCategory],
fetchImmediately: Bool = true) -> AnyPublisher<OCKAnyTaskCategory, Never> {
let presentValuePublisher = Future<OCKAnyTaskCategory, Never>({ completion in
self.store.fetchAnyTaskCategory(withID: taskCategory.id) { result in
completion(.success((try? result.get()) ?? taskCategory))
}
})

let changePublisher = notificationPublisher
.compactMap { $0 as? OCKTaskCategoryNotification }
.filter { $0.taskCategory.id == taskCategory.id && categories.contains($0.category) }
.map { $0.taskCategory }

return fetchImmediately ? AnyPublisher(changePublisher.prepend(presentValuePublisher)) : AnyPublisher(changePublisher)
}

// MARK: Tasks

func publisher(forTask task: OCKAnyTask, categories: [OCKStoreNotificationCategory],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Foundation
/// An `OCKSynchronizedStoreManager` wraps any store that conforms to `OCKStore` and provides synchronization to CareKit view
/// controllers by listening in on the store's activity by setting itself as the store delegate.
open class OCKSynchronizedStoreManager:
OCKPatientStoreDelegate, OCKCarePlanStoreDelegate, OCKContactStoreDelegate, OCKTaskStoreDelegate, OCKOutcomeStoreDelegate {
OCKPatientStoreDelegate, OCKCarePlanStoreDelegate, OCKContactStoreDelegate, OCKTaskCategoryStoreDelegate, OCKTaskStoreDelegate, OCKOutcomeStoreDelegate {

/// The underlying database.
public let store: OCKAnyStoreProtocol
Expand Down Expand Up @@ -130,6 +130,30 @@ OCKPatientStoreDelegate, OCKCarePlanStoreDelegate, OCKContactStoreDelegate, OCKT
}
}

// MARK: OCKStoreDelegate TaskCategory

open func taskCategoryStore(_ store: OCKAnyReadOnlyTaskCategoryStore, didAddTaskCategories taskCategories: [OCKAnyTaskCategory]) {
dispatchTaskCategoryNotifications(category: .add, taskCategories: taskCategories)
}

open func taskCategoryStore(_ store: OCKAnyReadOnlyTaskCategoryStore, didUpdateTaskCategories taskCategories: [OCKAnyTaskCategory]) {
dispatchTaskCategoryNotifications(category: .update, taskCategories: taskCategories)
}

open func taskCategoryStore(_ store: OCKAnyReadOnlyTaskCategoryStore, didDeleteTaskCategories taskCategories: [OCKAnyTaskCategory]) {
dispatchTaskCategoryNotifications(category: .delete, taskCategories: taskCategories)
}

private func dispatchTaskCategoryNotifications(category: OCKStoreNotificationCategory, taskCategories: [OCKAnyTaskCategory]) {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
taskCategories.forEach {
let notification = OCKTaskCategoryNotification(taskCategory: $0, category: category, storeManager: self)
self.subject.send(notification)
}
}
}

// MARK: OCKStoreDelegate Tasks

open func taskStore(_ store: OCKAnyReadOnlyTaskStore, didAddTasks tasks: [OCKAnyTask]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
/*
Copyright (c) 2020, Apple Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3. Neither the name of the copyright holder(s) nor the names of any contributors
may be used to endorse or promote products derived from this software without
specific prior written permission. No license is granted to the trademarks of
the copyright holders even if such marks are included in this software.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import Foundation

public typealias OCKDetailedTaskCategoryController = OCKTaskCategoryController
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
/*
Copyright (c) 2020, Apple Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3. Neither the name of the copyright holder(s) nor the names of any contributors
may be used to endorse or promote products derived from this software without
specific prior written permission. No license is granted to the trademarks of
the copyright holders even if such marks are included in this software.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import Foundation

public typealias OCKSimpleTaskCategoryController = OCKTaskCategoryController
Loading