Created
January 6, 2023 15:18
-
-
Save MaxenceMottard/f33599312af0b5a13b01ccc4c5264020 to your computer and use it in GitHub Desktop.
StatelessData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// StatelessData.swift | |
// ticketchain | |
// | |
// Created by Maxence Mottard on 28/08/2022. | |
// | |
import Foundation | |
public enum StatelessData<T> { | |
case redacted(_ sampleData: T) | |
case loading | |
case value(_ data: T) | |
case empty | |
public var isRedacted: Bool { | |
if case .redacted = self { | |
return true | |
} | |
return false | |
} | |
public var isLoading: Bool { | |
if case .loading = self { | |
return true | |
} | |
return false | |
} | |
public var isEmpty: Bool { | |
if case .empty = self { | |
return true | |
} | |
return false | |
} | |
public var value: T? { | |
if case let .value(value) = self { | |
return value | |
} | |
return nil | |
} | |
} | |
extension StatelessData where T: RangeReplaceableCollection { | |
public var valueOrEmpty: T { | |
if case let .redacted(value) = self { | |
return value | |
} else if case let .value(value) = self { | |
return value | |
} | |
return .init() | |
} | |
public var isEmpty: Bool { | |
if case let .value(value) = self { | |
return value.isEmpty | |
} | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment