diff --git a/chat-android/src/main/java/com/ably/chat/Room.kt b/chat-android/src/main/java/com/ably/chat/Room.kt index fa9a832..22bd019 100644 --- a/chat-android/src/main/java/com/ably/chat/Room.kt +++ b/chat-android/src/main/java/com/ably/chat/Room.kt @@ -60,7 +60,7 @@ interface Room { * * @returns The status observable. */ - val status: IRoomStatus + val status: RoomStatus /** * Returns the room options. @@ -92,7 +92,7 @@ internal class DefaultRoom( override val options: RoomOptions, realtimeClient: RealtimeClient, chatApi: ChatApi, - override val status: IRoomStatus = RoomStatus(RoomLifecycle.Initialized, null), + override val status: RoomStatus = DefaultRoomStatus(RoomLifecycle.Initialized, null), val logger: LogHandler?, ) : Room { diff --git a/chat-android/src/main/java/com/ably/chat/RoomStatus.kt b/chat-android/src/main/java/com/ably/chat/RoomStatus.kt index 5a33cd3..166794d 100644 --- a/chat-android/src/main/java/com/ably/chat/RoomStatus.kt +++ b/chat-android/src/main/java/com/ably/chat/RoomStatus.kt @@ -1,27 +1,28 @@ package com.ably.chat import io.ably.lib.types.ErrorInfo +import io.ably.lib.util.EventEmitter /** * Represents the status of a Room. */ -interface IRoomStatus { +interface RoomStatus { /** * The current status of the room. */ - val current: RoomLifecycle + var current: RoomLifecycle /** * The current error, if any, that caused the room to enter the current status. */ - val error: ErrorInfo? + var error: ErrorInfo? /** * Registers a listener that will be called whenever the room status changes. * @param listener The function to call when the status changes. * @returns An object that can be used to unregister the listener. */ - fun on(listener: Listener): Subscription + fun onChange(listener: Listener): Subscription /** * An interface for listening to changes for the room status @@ -40,19 +41,69 @@ interface IRoomStatus { fun offAll(); } -class RoomStatus(override val current: RoomLifecycle, override val error: ErrorInfo?) : IRoomStatus { +/** + * A new room status that can be set. + */ +interface NewRoomStatus { + /** + * The new status of the room. + */ + val status: RoomLifecycle; - private var listeners = mutableListOf() + /** + * An error that provides a reason why the room has + * entered the new status, if applicable. + */ + val error: ErrorInfo? +} - override fun on(listener: IRoomStatus.Listener): Subscription { - listeners.add(listener) +interface InternalRoomStatus: RoomStatus { + /** + * Registers a listener that will be called once when the room status changes. + * @param listener The function to call when the status changes. + */ + fun onChangeOnce(listener: RoomStatus.Listener) + + /** + * Sets the status of the room. + * + * @param params The new status of the room. + */ + fun setStatus(params: NewRoomStatus) +} + +open class ChatEventEmitter: EventEmitter() { + override fun apply(listener: Listener, event: Event, vararg args: Any?) { + TODO("Not yet implemented") + } +} + +class DefaultRoomStatus(override var current: RoomLifecycle, override var error: ErrorInfo?) : InternalRoomStatus, + ChatEventEmitter() { + + private val internalEmitter = ChatEventEmitter() + + override fun onChange(listener: RoomStatus.Listener): Subscription { + this.on(listener); return Subscription { - listeners.remove(listener) + this.off(listener) } } override fun offAll() { - listeners.clear(); + this.offAll() + } + + override fun onChangeOnce(listener: RoomStatus.Listener) { + internalEmitter.once(listener); + } + + override fun setStatus(params: NewRoomStatus) { + val change = RoomStatusChange(params.status, current, params.error); + this.current = change.current + this.error = change.error + this.internalEmitter.emit(change.current, change) + this.emit(change.current, change) } }