@ -14,6 +14,13 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
/ * *
* Provides a way to expose a MutableStateFlow as an immutable StateFlow .
* /
fun < T > StateFlow < T > . set ( v : T ) {
( this as MutableStateFlow < T > ) . value = v
}
class IpnModel (
notifier : Notifier ,
val apiClient : LocalApiClient ,
@ -21,31 +28,16 @@ class IpnModel(
) {
private var notifierSessions : MutableList < String > = mutableListOf ( )
private val _state : MutableStateFlow < Ipn . State > = MutableStateFlow ( Ipn . State . NoState )
private val _netmap : MutableStateFlow < Netmap . NetworkMap ? > = MutableStateFlow ( null )
protected val _prefs : MutableStateFlow < Ipn . Prefs ? > = MutableStateFlow ( null )
private val _engineStatus : MutableStateFlow < Ipn . EngineStatus ? > = MutableStateFlow ( null )
private val _tailFSShares : MutableStateFlow < Map < String , String > ? > = MutableStateFlow ( null )
private val _browseToURL : MutableStateFlow < String ? > = MutableStateFlow ( null )
private val _loginFinished : MutableStateFlow < String ? > = MutableStateFlow ( null )
private val _version : MutableStateFlow < String ? > = MutableStateFlow ( null )
private val _loggedInUser : MutableStateFlow < IpnLocal . LoginProfile ? > = MutableStateFlow ( null )
private val _loginProfiles : MutableStateFlow < List < IpnLocal . LoginProfile > ? > =
MutableStateFlow ( null )
val state : StateFlow < Ipn . State > = _state
val netmap : StateFlow < Netmap . NetworkMap ? > = _netmap
val prefs : StateFlow < Ipn . Prefs ? > = _prefs
val engineStatus : StateFlow < Ipn . EngineStatus ? > = _engineStatus
val tailFSShares : StateFlow < Map < String , String > ? > = _tailFSShares
val browseToURL : StateFlow < String ? > = _browseToURL
val loginFinished : StateFlow < String ? > = _loginFinished
val version : StateFlow < String ? > = _version
val loggedInUser : StateFlow < IpnLocal . LoginProfile ? > = _loggedInUser
val loginProfiles : StateFlow < List < IpnLocal . LoginProfile > ? > = _loginProfiles
val state : StateFlow < Ipn . State > = MutableStateFlow ( Ipn . State . NoState )
val netmap : StateFlow < Netmap . NetworkMap ? > = MutableStateFlow ( null )
val prefs : StateFlow < Ipn . Prefs ? > = MutableStateFlow ( null )
val engineStatus : StateFlow < Ipn . EngineStatus ? > = MutableStateFlow ( null )
val tailFSShares : StateFlow < Map < String , String > ? > = MutableStateFlow ( null )
val browseToURL : StateFlow < String ? > = MutableStateFlow ( null )
val loginFinished : StateFlow < String ? > = MutableStateFlow ( null )
val version : StateFlow < String ? > = MutableStateFlow ( null )
val loggedInUser : StateFlow < IpnLocal . LoginProfile ? > = MutableStateFlow ( null )
val loginProfiles : StateFlow < List < IpnLocal . LoginProfile > ? > = MutableStateFlow ( null )
val isUsingExitNode : Boolean
get ( ) {
@ -58,41 +50,35 @@ class IpnModel(
LocalApiClient . isReady . await ( )
apiClient . getProfiles { result ->
result . success ?. let { users -> _loginProfiles . value = users }
result . success ?. let ( loginProfiles :: set )
?: run { Log . e ( " IpnManager " , " Error loading profiles: ${result.error} " ) }
}
apiClient . getCurrentProfile { result ->
result . success ?. let { user -> _loggedInUser . value = user }
?: run { Log . e ( " IpnManager " , " Error loading profiles : ${result.error} " ) }
result . success ?. let ( loggedInUser :: set )
?: run { Log . e ( " IpnManager " , " Error loading current profile: ${result.error} " ) }
}
}
private fun onNotifyChange ( notify : Ipn . Notify ) {
notify . State ?. let { s tate ->
notify . State ?. let { s ->
// Refresh the user profiles if we're transitioning out of the
// NeedsLogin state.
if ( _ state. value == Ipn . State . NeedsLogin ) {
if ( state. value == Ipn . State . NeedsLogin ) {
scope . launch { loadUserProfiles ( ) }
}
Log . d ( " IpnModel " , " State changed: $s tate " )
_state. value = Ipn . State . fromInt ( state )
Log . d ( " IpnModel " , " State changed: $s " )
state. set ( Ipn . State . fromInt ( s ) )
}
notify . NetMap ?. let { netmap -> _netmap . value = netmap }
notify . Prefs ?. let { prefs -> _prefs . value = prefs }
notify . Engine ?. let { engine -> _engineStatus . value = engine }
notify . TailFSShares ?. let { shares -> _tailFSShares . value = shares }
notify . BrowseToURL ?. let { url -> _browseToURL . value = url }
notify . LoginFinished ?. let { message -> _loginFinished . value = message . property }
notify . Version ?. let { version -> _version . value = version }
notify . NetMap ?. let ( netmap :: set )
notify . Prefs ?. let ( prefs :: set )
notify . Engine ?. let ( engineStatus :: set )
notify . TailFSShares ?. let ( tailFSShares :: set )
notify . BrowseToURL ?. let ( browseToURL :: set )
notify . LoginFinished ?. let { loginFinished . set ( it . property ) }
notify . Version ?. let ( version :: set )
}
init {