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