Files
amnezia-client/client/android/protocolApi/src/main/kotlin/Status.kt
T

39 lines
902 B
Kotlin
Raw Normal View History

2023-11-23 15:45:55 +03:00
package org.amnezia.vpn.protocol
import android.os.Bundle
private const val STATE_KEY = "state"
2023-11-23 15:45:55 +03:00
@Suppress("DataClassPrivateConstructor")
data class Status private constructor(
val state: ProtocolState
2023-11-23 15:45:55 +03:00
) {
private constructor(builder: Builder) : this(builder.state)
2023-11-23 15:45:55 +03:00
class Builder {
lateinit var state: ProtocolState
2023-11-23 15:45:55 +03:00
private set
fun setState(state: ProtocolState) = apply { this.state = state }
2023-11-23 15:45:55 +03:00
fun build(): Status = Status(this)
}
companion object {
inline fun build(block: Builder.() -> Unit): Status = Builder().apply(block).build()
}
}
fun Bundle.putStatus(status: Status) {
putInt(STATE_KEY, status.state.ordinal)
2023-11-23 15:45:55 +03:00
}
2024-03-04 18:08:55 +03:00
fun Bundle.putStatus(state: ProtocolState) {
putInt(STATE_KEY, state.ordinal)
}
2023-11-23 15:45:55 +03:00
fun Bundle.getStatus(): Status =
Status.build {
setState(ProtocolState.entries[getInt(STATE_KEY)])
2023-11-23 15:45:55 +03:00
}