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

Reducer-based state management experiment (using Mobius.swift) #5866

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions ios/MullvadVPN.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
06AC116228F94C450037AF9A /* ApplicationConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58BFA5CB22A7CE1F00A6173D /* ApplicationConfiguration.swift */; };
449872E12B7BBC5400094DDC /* TunnelSettingsUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 449872E02B7BBC5400094DDC /* TunnelSettingsUpdate.swift */; };
449872E42B7CB96300094DDC /* TunnelSettingsUpdateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 449872E32B7CB96300094DDC /* TunnelSettingsUpdateTests.swift */; };
449872E82B8CC3DF00094DDC /* MobiusCore in Frameworks */ = {isa = PBXBuildFile; productRef = 449872E72B8CC3DF00094DDC /* MobiusCore */; };
44DD7D242B6CFFD70005F67F /* StartTunnelOperationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 44DD7D232B6CFFD70005F67F /* StartTunnelOperationTests.swift */; };
44DD7D272B6D18FB0005F67F /* MockTunnelInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 44DD7D262B6D18FB0005F67F /* MockTunnelInteractor.swift */; };
44DD7D292B7113CA0005F67F /* MockTunnel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 44DD7D282B7113CA0005F67F /* MockTunnel.swift */; };
Expand Down Expand Up @@ -2065,6 +2066,7 @@
58D223E6294C8F120029F5F8 /* MullvadTypes.framework in Frameworks */,
7ABCA5B32A9349F20044A708 /* Routing.framework in Frameworks */,
58D223CC294C8BCB0029F5F8 /* Operations.framework in Frameworks */,
449872E82B8CC3DF00094DDC /* MobiusCore in Frameworks */,
06799AD128F98E1D00ACD94E /* MullvadREST.framework in Frameworks */,
58B2FDD92AA71D2A003EB5C6 /* MullvadSettings.framework in Frameworks */,
);
Expand Down Expand Up @@ -4036,6 +4038,7 @@
name = MullvadVPN;
packageProductDependencies = (
58F0974D2A20C31100DA2DAD /* WireGuardKitTypes */,
449872E72B8CC3DF00094DDC /* MobiusCore */,
);
productName = MullvadVPN;
productReference = 58CE5E60224146200008646E /* MullvadVPN.app */;
Expand Down Expand Up @@ -4329,6 +4332,7 @@
packageReferences = (
585834F624D2BC1F00A8AF56 /* XCRemoteSwiftPackageReference "swift-log" */,
58F097482A20C30000DA2DAD /* XCRemoteSwiftPackageReference "wireguard-apple" */,
449872E62B8CC3DF00094DDC /* XCRemoteSwiftPackageReference "Mobius" */,
);
productRefGroup = 58CE5E61224146200008646E /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -8276,6 +8280,14 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
449872E62B8CC3DF00094DDC /* XCRemoteSwiftPackageReference "Mobius" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/spotify/Mobius.swift";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.5.2;
};
};
585834F624D2BC1F00A8AF56 /* XCRemoteSwiftPackageReference "swift-log" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-log.git";
Expand All @@ -8295,6 +8307,11 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
449872E72B8CC3DF00094DDC /* MobiusCore */ = {
isa = XCSwiftPackageProductDependency;
package = 449872E62B8CC3DF00094DDC /* XCRemoteSwiftPackageReference "Mobius" */;
productName = MobiusCore;
};
586A0DD02A20E371006C731C /* WireGuardKitTypes */ = {
isa = XCSwiftPackageProductDependency;
package = 58F097482A20C30000DA2DAD /* XCRemoteSwiftPackageReference "wireguard-apple" */;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
{
"pins" : [
{
"identity" : "cwlcatchexception",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattgallagher/CwlCatchException.git",
"state" : {
"revision" : "3b123999de19bf04905bc1dfdb76f817b0f2cc00",
"version" : "2.1.2"
}
},
{
"identity" : "cwlpreconditiontesting",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattgallagher/CwlPreconditionTesting.git",
"state" : {
"revision" : "dc9af4781f2afdd1e68e90f80b8603be73ea7abc",
"version" : "2.2.0"
}
},
{
"identity" : "mobius.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/spotify/Mobius.swift",
"state" : {
"revision" : "8606126a33258c290ad1cfbcccd0a7becb3cff52",
"version" : "0.5.2"
}
},
{
"identity" : "nimble",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Quick/Nimble",
"state" : {
"revision" : "1f3bde57bde12f5e7b07909848c071e9b73d6edc",
"version" : "10.0.0"
}
},
{
"identity" : "quick",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Quick/Quick",
"state" : {
"revision" : "f9d519828bb03dfc8125467d8f7b93131951124c",
"version" : "5.0.1"
}
},
{
"identity" : "swift-case-paths",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-case-paths",
"state" : {
"revision" : "bb436421f57269fbcfe7360735985321585a86e5",
"version" : "0.10.1"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
Expand Down
43 changes: 41 additions & 2 deletions ios/MullvadVPN/TunnelManager/TunnelManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import MobiusCore
import MullvadLogging
import MullvadREST
import MullvadSettings
Expand All @@ -18,6 +19,7 @@ import StoreKit
import UIKit
import WireGuardKitTypes


/// Interval used for periodic polling of tunnel relay status when tunnel is establishing
/// connection.
private let establishingTunnelStatusPollInterval: Duration = .seconds(3)
Expand Down Expand Up @@ -76,6 +78,36 @@ final class TunnelManager: StorePaymentObserver {
/// Last processed device check.
private var lastPacketTunnelKeyRotation: Date?

/// Experimental Mobius types
struct Model {
var isRunningPeriodicPrivateKeyRotation = false
}

enum Event {
case applicationBecameActive
}

enum Effect {
case updatePrivateKeyRotationTimer
case startNetworkMonitor
case startTunnel
case stopTunnel
case reconnectTunnel(selectNewRelay: Bool)
case refreshDeviceState
case refreshTunnelStatus
}

private func update(model: Model, event: Event) -> Next<Model, Effect> {
switch event {
case .applicationBecameActive:
return .dispatchEffects([.refreshTunnelStatus, .refreshDeviceState])
}
// .noChange // TODO
}

var effectHandler: EffectRouter<Effect, Event>! = nil
var mobiusLoop: MobiusLoop<Model, Event, Effect>! = nil

// MARK: - Initialization

init(
Expand All @@ -97,6 +129,12 @@ final class TunnelManager: StorePaymentObserver {
self.operationQueue.underlyingQueue = internalQueue
self.accessTokenManager = accessTokenManager

effectHandler = EffectRouter<Effect, Event>()
.routeCase(Effect.refreshTunnelStatus).to { self.refreshTunnelStatus() }
.routeCase(Effect.refreshDeviceState).to { self.refreshDeviceState() }

mobiusLoop = Mobius.loop(update: self.update, effectHandler: effectHandler.asConnectable).start(from: .init())

NotificationCenter.default.addObserver(
self,
selector: #selector(applicationDidBecomeActive(_:)),
Expand Down Expand Up @@ -759,8 +797,9 @@ final class TunnelManager: StorePaymentObserver {
#if DEBUG
logger.debug("Refresh device state and tunnel status due to application becoming active.")
#endif
refreshTunnelStatus()
refreshDeviceState()
mobiusLoop.dispatchEvent(.applicationBecameActive)
// refreshTunnelStatus()
// refreshDeviceState()
}

private func didUpdateNetworkPath(_ path: Network.NWPath) {
Expand Down
Loading