From 629b91f947b3636ba479c731be7bd3605f0b2266 Mon Sep 17 00:00:00 2001 From: Abhash Kumar Singh Date: Fri, 13 Oct 2023 11:36:50 -0700 Subject: [PATCH 1/3] feat(datastore): Make loadedState public in List+Model --- Amplify/Categories/DataStore/Model/Lazy/List+Model.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift b/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift index 09c3b8a3cd..4833ba07e9 100644 --- a/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift +++ b/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift @@ -19,13 +19,13 @@ public class List: Collection, Codable, ExpressibleByArrayLite public typealias Element = ModelType /// Represents the data state of the `List`. - enum LoadedState { + public enum LoadedState { case notLoaded case loaded([Element]) } /// The current state of lazily loaded list - var loadedState: LoadedState + public internal(set) var loadedState: LoadedState /// The provider for fulfilling list behaviors let listProvider: AnyModelListProvider From e7150dfab2972355c5d41a89f9e343b8689734fa Mon Sep 17 00:00:00 2001 From: Abhash Kumar Singh Date: Fri, 13 Oct 2023 12:01:55 -0700 Subject: [PATCH 2/3] Update unit tests --- .../Tests/AWSDataStorePluginTests/Core/ListTests.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift b/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift index e0a84f58d5..d69639c799 100644 --- a/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift +++ b/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift @@ -8,9 +8,9 @@ import Combine import XCTest -@testable import Amplify -@testable import AmplifyTestCommon -@testable import AWSDataStorePlugin +import Amplify +import AmplifyTestCommon +import AWSDataStorePlugin class ListTests: BaseDataStoreTests { From 259ae9444dfb488365a24bc701c77377712a133c Mon Sep 17 00:00:00 2001 From: Abhash Kumar Singh Date: Fri, 13 Oct 2023 13:04:55 -0700 Subject: [PATCH 3/3] Add a public boolean isLoaded property --- .../DataStore/Model/Lazy/List+Model.swift | 14 ++++++++++++-- .../AWSDataStorePluginTests/Core/ListTests.swift | 10 ++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift b/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift index 4833ba07e9..aee10e9e53 100644 --- a/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift +++ b/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift @@ -19,13 +19,22 @@ public class List: Collection, Codable, ExpressibleByArrayLite public typealias Element = ModelType /// Represents the data state of the `List`. - public enum LoadedState { + enum LoadedState { case notLoaded case loaded([Element]) } /// The current state of lazily loaded list - public internal(set) var loadedState: LoadedState + var loadedState: LoadedState + + /// Boolean property to check if list is loaded + public var isLoaded: Bool { + if case .loaded = loadedState { + return true + } + + return false + } /// The provider for fulfilling list behaviors let listProvider: AnyModelListProvider @@ -61,6 +70,7 @@ public class List: Collection, Codable, ExpressibleByArrayLite } } + // MARK: - Initializers public init(listProvider: AnyModelListProvider) { diff --git a/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift b/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift index d69639c799..d94be55588 100644 --- a/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift +++ b/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift @@ -25,15 +25,9 @@ class ListTests: BaseDataStoreTests { let postId = preparePostDataForTest() func checkComments(_ comments: List) async throws { - guard case .notLoaded = comments.loadedState else { - XCTFail("Should not be loaded") - return - } + XCTAssertFalse(comments.isLoaded) try await comments.fetch() - guard case .loaded = comments.loadedState else { - XCTFail("Should be loaded") - return - } + XCTAssertTrue(comments.isLoaded) XCTAssertEqual(comments.count, 2) expect.fulfill() }