Expose network state to iOS bridge
This commit is contained in:
@@ -2,6 +2,7 @@ import Foundation
|
|||||||
|
|
||||||
public protocol SessionBridge: Sendable {
|
public protocol SessionBridge: Sendable {
|
||||||
func authState() async throws -> AuthState
|
func authState() async throws -> AuthState
|
||||||
|
func networkState() async throws -> NetworkState
|
||||||
func pollEvents() async throws -> [SessionEvent]
|
func pollEvents() async throws -> [SessionEvent]
|
||||||
func sendPhoneNumber(_ phone: String) async throws
|
func sendPhoneNumber(_ phone: String) async throws
|
||||||
func sendCode(_ code: String) async throws
|
func sendCode(_ code: String) async throws
|
||||||
@@ -66,6 +67,10 @@ public actor FakeSessionBridge: SessionBridge {
|
|||||||
auth
|
auth
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func networkState() async throws -> NetworkState {
|
||||||
|
.ready
|
||||||
|
}
|
||||||
|
|
||||||
public func pollEvents() async throws -> [SessionEvent] {
|
public func pollEvents() async throws -> [SessionEvent] {
|
||||||
let drained = events
|
let drained = events
|
||||||
events.removeAll()
|
events.removeAll()
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ public actor UniFfiSessionBridge: SessionBridge {
|
|||||||
Self.mapAuthState(handle.authState())
|
Self.mapAuthState(handle.authState())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func networkState() async throws -> NetworkState {
|
||||||
|
Self.mapNetworkState(handle.networkState())
|
||||||
|
}
|
||||||
|
|
||||||
public func pollEvents() async throws -> [SessionEvent] {
|
public func pollEvents() async throws -> [SessionEvent] {
|
||||||
handle.pollEvents().map(Self.mapEvent)
|
handle.pollEvents().map(Self.mapEvent)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,10 +24,24 @@ public final class SessionStore: ObservableObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func refreshNetworkState() async {
|
||||||
|
do {
|
||||||
|
networkState = try await bridge.networkState()
|
||||||
|
errorMessage = nil
|
||||||
|
} catch {
|
||||||
|
errorMessage = error.localizedDescription
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public func apply(events: [SessionEvent]) {
|
public func apply(events: [SessionEvent]) {
|
||||||
for event in events {
|
for event in events {
|
||||||
if case let .authChanged(state) = event {
|
switch event {
|
||||||
|
case let .authChanged(state):
|
||||||
authState = state
|
authState = state
|
||||||
|
case let .networkChanged(state):
|
||||||
|
networkState = state
|
||||||
|
default:
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ struct TeleTuiIOSSmokeTests {
|
|||||||
|
|
||||||
await store.refreshAuthState()
|
await store.refreshAuthState()
|
||||||
precondition(store.authState == .waitPhoneNumber)
|
precondition(store.authState == .waitPhoneNumber)
|
||||||
|
await store.refreshNetworkState()
|
||||||
|
precondition(store.networkState == .ready)
|
||||||
|
|
||||||
viewModel.phone = "+10000000000"
|
viewModel.phone = "+10000000000"
|
||||||
await viewModel.submitCurrentStep()
|
await viewModel.submitCurrentStep()
|
||||||
|
|||||||
@@ -493,6 +493,10 @@ impl SessionHandle {
|
|||||||
self.with_session(|session| session.auth_state().into())
|
self.with_session(|session| session.auth_state().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn network_state(&self) -> IosNetworkState {
|
||||||
|
self.with_session(|session| session.network_state().into())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn poll_events(&self) -> Vec<IosEvent> {
|
pub fn poll_events(&self) -> Vec<IosEvent> {
|
||||||
self.with_session(|session| {
|
self.with_session(|session| {
|
||||||
session.drain_client_events();
|
session.drain_client_events();
|
||||||
@@ -909,6 +913,10 @@ impl SessionHandle {
|
|||||||
.clone()
|
.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn network_state(&self) -> IosNetworkState {
|
||||||
|
IosNetworkState::Ready
|
||||||
|
}
|
||||||
|
|
||||||
pub fn poll_events(&self) -> Vec<IosEvent> {
|
pub fn poll_events(&self) -> Vec<IosEvent> {
|
||||||
let mut state = self.state.lock().expect("session mutex poisoned");
|
let mut state = self.state.lock().expect("session mutex poisoned");
|
||||||
std::mem::take(&mut state.events)
|
std::mem::take(&mut state.events)
|
||||||
@@ -1296,6 +1304,7 @@ mod tests {
|
|||||||
|
|
||||||
let chats = session.load_chats(20).unwrap();
|
let chats = session.load_chats(20).unwrap();
|
||||||
assert_eq!(chats.len(), 1);
|
assert_eq!(chats.len(), 1);
|
||||||
|
assert_eq!(session.network_state(), IosNetworkState::Ready);
|
||||||
|
|
||||||
let history = session.load_history(chats[0].id, 20).unwrap();
|
let history = session.load_history(chats[0].id, 20).unwrap();
|
||||||
assert_eq!(history[0].text, "Hello from fake TDLib");
|
assert_eq!(history[0].text, "Hello from fake TDLib");
|
||||||
|
|||||||
@@ -57,6 +57,14 @@ struct Smoke {
|
|||||||
require(chats.count == 1, "expected one fake chat")
|
require(chats.count == 1, "expected one fake chat")
|
||||||
let chat = chats[0]
|
let chat = chats[0]
|
||||||
|
|
||||||
|
let network = session.networkState()
|
||||||
|
require({
|
||||||
|
if case .ready = network {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}(), "expected ready network state")
|
||||||
|
|
||||||
let history = try session.loadHistory(chatId: chat.id, limit: 20)
|
let history = try session.loadHistory(chatId: chat.id, limit: 20)
|
||||||
require(history.first?.text == "Hello from fake TDLib", "expected seeded history")
|
require(history.first?.text == "Hello from fake TDLib", "expected seeded history")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user