-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature]Introduce CallKit availability policies (#611)
- Loading branch information
1 parent
d4531cf
commit 2ee1c89
Showing
11 changed files
with
354 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
13 changes: 13 additions & 0 deletions
13
Sources/StreamVideo/CallKit/AvailabilityPolicy/CallKitAlwaysAvailabilityPolicy.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// Copyright © 2024 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A policy implementation where CallKit is always available. | ||
/// | ||
/// This policy ignores regional or other constraints. | ||
struct CallKitAlwaysAvailabilityPolicy: CallKitAvailabilityPolicyProtocol { | ||
/// CallKit is always available with this policy. | ||
var isAvailable: Bool { true } | ||
} |
49 changes: 49 additions & 0 deletions
49
Sources/StreamVideo/CallKit/AvailabilityPolicy/CallKitAvailabilityPolicy.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// Copyright © 2024 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A policy that defines when CallKit is available. | ||
/// It can be configured to always enable CallKit, enable it based on the user's | ||
/// region, or use a custom implementation. | ||
public enum CallKitAvailabilityPolicy: CustomStringConvertible { | ||
|
||
/// CallKit is always available, regardless of conditions. | ||
case always | ||
|
||
/// CallKit availability is determined based on the user's region. | ||
case regionBased | ||
|
||
/// CallKit availability is determined by a custom policy. | ||
/// - Parameter policy: A custom policy implementing `CallKitAvailabilityPolicyProtocol`. | ||
case custom(CallKitAvailabilityPolicyProtocol) | ||
|
||
/// A textual description of the availability policy. | ||
/// | ||
/// - Returns: A string representation of the policy. | ||
public var description: String { | ||
switch self { | ||
case .always: | ||
return ".always" | ||
case .regionBased: | ||
return ".regionBased" | ||
case let .custom(policy): | ||
return ".custom(\(policy))" | ||
} | ||
} | ||
|
||
/// The underlying policy implementation based on the selected availability. | ||
/// | ||
/// - Returns: An instance conforming to `CallKitAvailabilityPolicyProtocol`. | ||
var policy: CallKitAvailabilityPolicyProtocol { | ||
switch self { | ||
case .always: | ||
return CallKitAlwaysAvailabilityPolicy() | ||
case .regionBased: | ||
return CallKitRegionBasedAvailabilityPolicy() | ||
case let .custom(policy): | ||
return policy | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Sources/StreamVideo/CallKit/AvailabilityPolicy/CallKitAvailabilityPolicyProtocol.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// Copyright © 2024 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A protocol defining the requirements for CallKit availability policies. | ||
public protocol CallKitAvailabilityPolicyProtocol { | ||
/// Indicates whether CallKit is available under the policy. | ||
var isAvailable: Bool { get } | ||
} |
38 changes: 38 additions & 0 deletions
38
Sources/StreamVideo/CallKit/AvailabilityPolicy/CallKitRegionBasedAvailabilityPolicy.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// Copyright © 2024 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A policy implementation where CallKit availability depends on the region. | ||
/// | ||
/// This policy disables CallKit in specific regions, identified by their region | ||
/// codes, to comply with regional regulations or restrictions. It utilizes the | ||
/// injected `StreamLocaleProvider` to retrieve the current locale information. | ||
struct CallKitRegionBasedAvailabilityPolicy: CallKitAvailabilityPolicyProtocol { | ||
|
||
/// A provider for locale information. | ||
@Injected(\.localeProvider) private var localeProvider | ||
|
||
/// A set of region identifiers where CallKit is unavailable. | ||
/// | ||
/// This includes both two-letter and three-letter region codes. | ||
private var unavailableRegions: Set<String> = [ | ||
"CN", // China (two-letter code) | ||
"CHN" // China (three-letter code) | ||
] | ||
|
||
/// Determines if CallKit is available based on the current region. | ||
/// | ||
/// - Returns: `true` if CallKit is available; otherwise, `false`. | ||
/// - Note: If the region cannot be determined, CallKit is considered unavailable. | ||
var isAvailable: Bool { | ||
// Retrieve the current region identifier from the locale provider. | ||
guard let identifier = localeProvider.identifier else { | ||
return false | ||
} | ||
|
||
// CallKit is unavailable if the region is part of the restricted set. | ||
return !unavailableRegions.contains(identifier) | ||
} | ||
} |
This file contains 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
58 changes: 58 additions & 0 deletions
58
Sources/StreamVideo/Utils/LocaleProvider/StreamLocaleProvider.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// Copyright © 2024 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A protocol that defines the requirements for a locale provider. | ||
/// | ||
/// This protocol abstracts the retrieval of a region identifier, allowing | ||
/// flexibility and testability in components that depend on locale information. | ||
protocol LocaleProviding { | ||
|
||
/// The region identifier of the current locale. | ||
/// | ||
/// - Returns: A string representing the region identifier (e.g., "US" or "GB"), | ||
/// or `nil` if the region cannot be determined. | ||
var identifier: String? { get } | ||
} | ||
|
||
/// A provider for accessing the current locale's region identifier. | ||
/// | ||
/// This class abstracts locale information, offering compatibility for different | ||
/// iOS versions. | ||
final class StreamLocaleProvider: LocaleProviding { | ||
|
||
/// Retrieves the region identifier for the current locale. | ||
/// | ||
/// - For iOS 16 and later, it uses the `region` property. | ||
/// - For earlier versions, it falls back to `regionCode`. | ||
/// | ||
/// - Returns: A string representing the region identifier, or `nil` if unavailable. | ||
var identifier: String? { | ||
if #available(iOS 16, *) { | ||
// Retrieve the region identifier for iOS 16 and later. | ||
return NSLocale.current.region?.identifier | ||
} else { | ||
// Retrieve the region code for earlier iOS versions. | ||
return NSLocale.current.regionCode | ||
} | ||
} | ||
} | ||
|
||
enum LocaleProvidingKey: InjectionKey { | ||
/// The current value of the `StreamLocaleProvider` used for dependency injection. | ||
static var currentValue: LocaleProviding = StreamLocaleProvider() | ||
} | ||
|
||
/// Extension of `InjectedValues` to provide access to the `StreamLocaleProvider`. | ||
extension InjectedValues { | ||
|
||
/// The locale provider, used to access region information within the app. | ||
/// | ||
/// This value can be overridden for testing or specific use cases. | ||
var localeProvider: LocaleProviding { | ||
get { Self[LocaleProvidingKey.self] } | ||
set { Self[LocaleProvidingKey.self] = newValue } | ||
} | ||
} |
This file contains 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
Oops, something went wrong.