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 Simple and Detailed Contact Views in SwiftUI #477

Open
wants to merge 14 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
38 changes: 38 additions & 0 deletions CareKit/CareKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@
64EABE022321B1AF00CFBB9F /* OCKSynchronizedStoreManager+Publishers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64EABDBF2321B1AF00CFBB9F /* OCKSynchronizedStoreManager+Publishers.swift */; };
64EABE0B2321B1AF00CFBB9F /* OCKContactUtility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64EABDCB2321B1AF00CFBB9F /* OCKContactUtility.swift */; };
64EABE202321B1AF00CFBB9F /* locversion.plist in Resources */ = {isa = PBXBuildFile; fileRef = 64EABDE72321B1AF00CFBB9F /* locversion.plist */; };
B046D07824B3BD630005F316 /* OCKContactController+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B046D07724B3BD630005F316 /* OCKContactController+Extensions.swift */; };
B046D07924B3BD630005F316 /* OCKContactController+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B046D07724B3BD630005F316 /* OCKContactController+Extensions.swift */; };
B046D07B24B3D65B0005F316 /* TestSimpleContactView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B046D07A24B3D65B0005F316 /* TestSimpleContactView.swift */; };
B046D07D24B3D6670005F316 /* TestDetailedContactView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B046D07C24B3D6670005F316 /* TestDetailedContactView.swift */; };
B046D07F24B3DD5B0005F316 /* TestDetailedContactViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = B046D07E24B3DD5B0005F316 /* TestDetailedContactViewModel.swift */; };
B09EFD9C24AB711500FF5C4F /* SimpleContactView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B09EFD9B24AB711500FF5C4F /* SimpleContactView.swift */; };
B0A3840524B0B73100384A64 /* DetailedContactView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0A3840424B0B73100384A64 /* DetailedContactView.swift */; };
B0B50D3424ABCD4900AC8192 /* SynchronizedContactView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0B50D3324ABCD4900AC8192 /* SynchronizedContactView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -286,6 +294,13 @@
64EABDE52321B1AF00CFBB9F /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
64EABDE82321B1AF00CFBB9F /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = en; path = en.lproj/locversion.plist; sourceTree = "<group>"; };
8605A5BA1C4F04EC00DD65FF /* CareKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CareKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B046D07724B3BD630005F316 /* OCKContactController+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OCKContactController+Extensions.swift"; sourceTree = "<group>"; };
B046D07A24B3D65B0005F316 /* TestSimpleContactView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestSimpleContactView.swift; sourceTree = "<group>"; };
B046D07C24B3D6670005F316 /* TestDetailedContactView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestDetailedContactView.swift; sourceTree = "<group>"; };
B046D07E24B3DD5B0005F316 /* TestDetailedContactViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestDetailedContactViewModel.swift; sourceTree = "<group>"; };
B09EFD9B24AB711500FF5C4F /* SimpleContactView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimpleContactView.swift; sourceTree = "<group>"; };
B0A3840424B0B73100384A64 /* DetailedContactView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailedContactView.swift; sourceTree = "<group>"; };
B0B50D3324ABCD4900AC8192 /* SynchronizedContactView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SynchronizedContactView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -342,6 +357,7 @@
0346743D2397026A0074891C /* OCKLog.swift */,
64EABDBC2321B1AF00CFBB9F /* Synchronization */,
510466FE24A2873A00D0FD53 /* Task */,
B09EFD9A24AB6D9700FF5C4F /* Contact */,
510466FD24A2871800D0FD53 /* Extensions */,
5104670024A28C7600D0FD53 /* Utilities */,
);
Expand All @@ -351,6 +367,7 @@
510466FD24A2871800D0FD53 /* Extensions */ = {
isa = PBXGroup;
children = (
B046D07724B3BD630005F316 /* OCKContactController+Extensions.swift */,
515C0A1523BF9D3C009A9774 /* OCKTaskControllerProtocol+Extension.swift */,
5125A214248F2E00009C9643 /* OCKTaskEvents+Extension.swift */,
032C86EF2326B68D00D0A0EA /* Calendar+Extensions.swift */,
Expand Down Expand Up @@ -562,6 +579,9 @@
518E153A237265930018541B /* TestSimpleContactViewSynchronizer.swift */,
518E153F23726A690018541B /* TestDetailedContactViewSynchronizer.swift */,
518E154123726F590018541B /* TestCustomContactViewSynchronizer.swift */,
B046D07A24B3D65B0005F316 /* TestSimpleContactView.swift */,
B046D07C24B3D6670005F316 /* TestDetailedContactView.swift */,
B046D07E24B3DD5B0005F316 /* TestDetailedContactViewModel.swift */,
);
path = Contact;
sourceTree = "<group>";
Expand Down Expand Up @@ -828,6 +848,16 @@
name = Products;
sourceTree = "<group>";
};
B09EFD9A24AB6D9700FF5C4F /* Contact */ = {
isa = PBXGroup;
children = (
B0B50D3324ABCD4900AC8192 /* SynchronizedContactView.swift */,
B09EFD9B24AB711500FF5C4F /* SimpleContactView.swift */,
B0A3840424B0B73100384A64 /* DetailedContactView.swift */,
);
path = Contact;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -995,6 +1025,8 @@
51FF9B8E2373377500BAEDB2 /* TestWeekCalendarViewSynchronizer.swift in Sources */,
51757C7B2450CF6E0081F133 /* TestTaskEvents.swift in Sources */,
510591232378FF24004EDC84 /* TestGridTaskViewSynchronizer.swift in Sources */,
B046D07B24B3D65B0005F316 /* TestSimpleContactView.swift in Sources */,
B046D07F24B3DD5B0005F316 /* TestDetailedContactViewModel.swift in Sources */,
519990712448B864005CD581 /* TestNumericProgressTaskViewModel.swift in Sources */,
517C784A239860C1005B2549 /* TestCustomTaskViewSynchronizer.swift in Sources */,
5105912523790D06004EDC84 /* TestMockTaskEvents.swift in Sources */,
Expand All @@ -1004,6 +1036,7 @@
51168D97246E1EFB0002CC69 /* TestSimpleTaskView.swift in Sources */,
51D8E27F24115D7D0026C716 /* TestListView.swift in Sources */,
51AF51542498420D005D385F /* TestLabeledValueTaskViewModel.swift in Sources */,
B046D07D24B3D6670005F316 /* TestDetailedContactView.swift in Sources */,
511372462374DFBD00831191 /* TestSynchronizedContext.swift in Sources */,
51168D99246E20B40002CC69 /* TestNumericProgressTaskView.swift in Sources */,
51D544932397584D00898683 /* Array+Extension.swift in Sources */,
Expand All @@ -1025,6 +1058,7 @@
51178E0D23AAAA2A0068BAB1 /* OCKSynchronizationContext.swift in Sources */,
515862CA23A9C4D600630AB5 /* InstructionsTaskView.swift in Sources */,
51178E0323AA95270068BAB1 /* OCKChecklistTaskController.swift in Sources */,
B046D07924B3BD630005F316 /* OCKContactController+Extensions.swift in Sources */,
51355EEA2499852D009DE0A4 /* OCKTaskEvents+Extension.swift in Sources */,
51178E0123AA95270068BAB1 /* OCKTaskController.swift in Sources */,
51178E1023AAAA2A0068BAB1 /* OCKSynchronizedStoreManager+Publishers.swift in Sources */,
Expand Down Expand Up @@ -1058,6 +1092,7 @@
517F3E4523345872004FE251 /* OCKCartesianChartView+Updatable.swift in Sources */,
5112730E235E3B12007B18DF /* OCKDetailedContactViewController.swift in Sources */,
51AF514E24983B86005D385F /* LabeledValueTaskView.swift in Sources */,
B09EFD9C24AB711500FF5C4F /* SimpleContactView.swift in Sources */,
515E17D22351262100637153 /* OCKTaskEvents.swift in Sources */,
5167B92F23343A64002BC69C /* OCKHeaderView+Updatable.swift in Sources */,
5112731A235E588B007B18DF /* OCKCartesianChartController.swift in Sources */,
Expand All @@ -1072,11 +1107,14 @@
51127322235E58E8007B18DF /* OCKInstructionsTaskController.swift in Sources */,
64EABDF12321B1AF00CFBB9F /* UIViewController+Extensions.swift in Sources */,
51108C6523596BBD0029F7A2 /* OCKSynchronizationContext.swift in Sources */,
B046D07824B3BD630005F316 /* OCKContactController+Extensions.swift in Sources */,
51127320235E58D5007B18DF /* OCKSimpleTaskController.swift in Sources */,
B0A3840524B0B73100384A64 /* DetailedContactView.swift in Sources */,
51AA2304234E687B00A90DA2 /* OCKSimpleContactViewSynchronizer.swift in Sources */,
64EABE002321B1AF00CFBB9F /* OCKStoreNotifications.swift in Sources */,
51AF515024983BE8005D385F /* OCKLabeledValueTaskController.swift in Sources */,
51AF515824984B6F005D385F /* Number+Extension.swift in Sources */,
B0B50D3424ABCD4900AC8192 /* SynchronizedContactView.swift in Sources */,
5183EA8C2353BC7600C46113 /* OCKScheduleUtility.swift in Sources */,
510A6656236147FF00074275 /* OCKAnyEvent+Extension.swift in Sources */,
517F3E4A23345BE9004FE251 /* OCKDetailedContactView+Updatable.swift in Sources */,
Expand Down
138 changes: 138 additions & 0 deletions CareKit/CareKit/Shared/Contact/DetailedContactView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//
kutakmir marked this conversation as resolved.
Show resolved Hide resolved
/*
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 Foundation
import SwiftUI

/// A card that displays information for a contact. The header is an `OCKHeaderView`
/// The body contains a multi-line istructions label, and four buttons; call, message,
/// email, and address. The first three buttons have title labels and image views that can
/// be modified, while the last has a title label, body label, and image view.
///
/// +-------------------------------------------------------+
/// | +------+ |
/// | | icon | [title] |
/// | | img | [detail] |
/// | +------+ |
/// | |
/// | -------------------------------------------------- |
/// | |
/// | [Instructions] |
/// | |
/// | +------------+ +------------+ +------------+ |
/// | | [title] | | [title] | | [title] | |
/// | | | | | | | |
/// | +------------+ +------------+ +------------+ |
/// | |
/// | +---------------------------------------------------+ |
/// | | [title] | |
/// | | [detail] | |
/// | | | |
/// | +---------------------------------------------------+ |
/// +-------------------------------------------------------+
///
@available(iOS 14.0, watchOS 7.0, *)
public struct DetailedContactView<Header: View, Footer: View>: View {
private typealias ContactView = SynchronizedContactView<OCKDetailedContactController, CareKitUI.DetailedContactView<Header, Footer>>

private let contactView: ContactView

public var body: some View {
contactView
}

private init(contactView: ContactView) {
self.contactView = contactView
}

public init(contactID: String, storeManager: OCKSynchronizedStoreManager, content: @escaping (_ controller: OCKDetailedContactController) -> CareKitUI.DetailedContactView<Header, Footer>) {
contactView = .init(controller: .init(storeManager: storeManager), query: .contactID(contactID), content: content)
}

public init(contact: OCKAnyContact, contactQuery: OCKContactQuery, storeManager: OCKSynchronizedStoreManager,
content: @escaping (_ controller: OCKDetailedContactController) -> CareKitUI.DetailedContactView<Header, Footer>) {
contactView = .init(controller: .init(storeManager: storeManager), query: .init(.contact(contact)), content: content)
}

public init(controller: OCKDetailedContactController, content: @escaping (_ controller: OCKDetailedContactController) -> CareKitUI.DetailedContactView<Header, Footer>) {
contactView = .init(controller: controller, content: content)
}

public func onError(_ perform: @escaping (Error) -> Void) -> DetailedContactView<Header, Footer> {
.init(contactView: .init(copying: contactView, settingErrorHandler: perform))
}
}

@available(iOS 14.0, watchOS 7.0, *)
public extension DetailedContactView where Header == _DetailedContactViewHeader, Footer == _DetailedContactViewFooter {
init(contactID: String, storeManager: OCKSynchronizedStoreManager) {
self.init(contactID: contactID, storeManager: storeManager) {
.init(viewModel: $0.viewModel)
}
}

init(contact: OCKAnyContact,contactQuery: OCKContactQuery, storeManager: OCKSynchronizedStoreManager) {
self.init(contact: contact, contactQuery: contactQuery, storeManager: storeManager) {
.init(viewModel: $0.viewModel)
}
}

init(controller: OCKDetailedContactController) {
contactView = .init(controller: controller) {
.init(viewModel: $0.viewModel)
}
}
}

// CODE REVIEW: Let's add actions - they are nil right now, see UIKit version
kutakmir marked this conversation as resolved.
Show resolved Hide resolved
private extension CareKitUI.DetailedContactView where Header == _DetailedContactViewHeader, Footer == _DetailedContactViewFooter {
init(viewModel: ContactViewModel?) {
self.init(title: Text(viewModel?.title ?? ""), detail: Text(viewModel?.detail ?? ""), instructions: Text(viewModel?.instructions ?? ""), image: Image(systemName: "person.crop.circle"), disclosureImage: nil, callButton: ContactButton(title: Text("Call"), image: Image(systemName: "phone"), action: nil), messageButton: ContactButton(title: Text("Message"), image: Image(systemName: "text.bubble"), action: nil), emailButton: ContactButton(title: Text("E-mail"), image: Image(systemName: "envelope"), action: nil), addressButton: AddressButton(title: Text("Address"), detail: Text(viewModel?.address ?? ""), image: Image(systemName: "location"), action: nil))
kutakmir marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Data used to create a `CareKitUI.DetailedContactView`.
public struct ContactViewModel {

/// The title text to display in the header.
public let title: String

/// The detail text to display in the header.
public let detail: String?

/// The instructions text to display under the header.
public let instructions: String?

/// The address text to display under the address button.
public let address: String?
}
kutakmir marked this conversation as resolved.
Show resolved Hide resolved
Loading