Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #35 from boswelja/refactoring
Browse files Browse the repository at this point in the history
breaking: Reorganise library
  • Loading branch information
boswelja authored Jun 16, 2021
2 parents 36ef03d + 012e7b1 commit d5a56d0
Show file tree
Hide file tree
Showing 49 changed files with 1,139 additions and 939 deletions.
11 changes: 0 additions & 11 deletions core/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
# Keep objects
-keep class com.boswelja.watchconnection.core.Messages

# Keep classes
-keep class com.boswelja.watchconnection.core.WatchPlatformManager
-keep class com.boswelja.watchconnection.core.Watch
-keep class com.boswelja.watchconnection.core.MessageReceiver

# Keep interfaces
-keep class com.boswelja.watchconnection.core.MessageListener
-keep class com.boswelja.watchconnection.core.WatchPlatform
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.boswelja.watchconnection.core

abstract class BaseClient<T : Platform>(
vararg platforms: T
) {
/** A map of platform IDs to their handlers */
protected val platforms = HashMap<String, T>()

init {
// Throw exception if no platforms were provided.
if (platforms.isEmpty())
throw IllegalArgumentException("Tried creating a client with no platforms")

// Map platform IDs to their handlers for easier access later
platforms.forEach {
this.platforms[it.platformIdentifier] = it
}
}
}
13 changes: 13 additions & 0 deletions core/src/main/kotlin/com/boswelja/watchconnection/core/Platform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.boswelja.watchconnection.core

/**
* Provides a common interface between platforms
*/
interface Platform {

/**
* Returns a unique string to identify this platform. This will be used to map watches to the
* correct platform as needed.
*/
val platformIdentifier: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ open class Watch(

/**
* Gets a reproducible [UUID] from the provided info.
* @param platform See [WatchPlatform.platformIdentifier].
* @param platform See [Platform.platformIdentifier].
* @param platformId See [Watch.platformId].
*/
fun createUUID(platform: String, platformId: String): UUID =
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.boswelja.watchconnection.core.discovery

import com.boswelja.watchconnection.core.BaseClient
import com.boswelja.watchconnection.core.Platform
import com.boswelja.watchconnection.core.Watch
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine

/**
* DiscoveryClient takes a number of [DiscoveryPlatform]s, and provides a common interface between
* them.
* @param platforms The [DiscoveryPlatform]s this MessageClient should support.
*/
class DiscoveryClient(
vararg platforms: DiscoveryPlatform
) : BaseClient<DiscoveryPlatform>(*platforms) {

/**
* Get a [Flow] of all [Watch]es found by all [Platform]s.
*/
@ExperimentalCoroutinesApi
fun allWatches(): Flow<List<Watch>> =
combine(platforms.values.map { it.allWatches() }) { flows ->
val list = mutableListOf<Watch>()
flows.forEach { watches ->
list += watches
}
list
}

/**
* Get a [Flow] of all [Watch]es determined to have the companion app installed from all
* [Platform]s.
*/
@ExperimentalCoroutinesApi
fun watchesWithApp(): Flow<List<Watch>> =
combine(platforms.values.map { it.watchesWithApp() }) { flows ->
val list = mutableListOf<Watch>()
flows.forEach { watches ->
list += watches
}
list
}

/**
* Get a flow of capabilities found for a given [Watch].
* @param watch See [Watch].
* @return A [Flow] of capability strings declared by the watch.
*/
fun getCapabilitiesFor(watch: Watch): Flow<List<String>>? {
return platforms[watch.platform]?.getCapabilitiesFor(watch.platformId)
}

/**
* Gets a [Flow] of [Status] for a given [Watch].
* @param watch The [Watch] to get a [Status] for.
* @return The [Flow] of [Status]. May be null if the given watches platform doesn't exist in
* this instance.
*/
fun getStatusFor(watch: Watch): Flow<Status>? {
return platforms[watch.platform]?.getStatusFor(watch.platformId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.boswelja.watchconnection.core.discovery

import com.boswelja.watchconnection.core.Platform
import com.boswelja.watchconnection.core.Watch
import kotlinx.coroutines.flow.Flow

/**
* [Platform] support for discovering watches, watch capabilities, and watch status.
*/
interface DiscoveryPlatform : Platform {

/**
* A flow of all available watches for this platform.
*/
fun allWatches(): Flow<List<Watch>>

/**
* A flow of all available watches with the companion app installed for this platform.
*/
fun watchesWithApp(): Flow<List<Watch>>

/**
* Get a flow of capabilities found for a [Watch] with a given ID.
* @param watchId See [Watch.platformId].
* @return A [Flow] of capability strings declared by the watch.
*/
fun getCapabilitiesFor(watchId: String): Flow<List<String>>

/**
* Gets a [Flow] of [Status] for the watch with the given ID.
* @param watchId See [Watch.platformId].
* @return The [Flow] of [Status].
*/
fun getStatusFor(watchId: String): Flow<Status>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.boswelja.watchconnection.core
package com.boswelja.watchconnection.core.discovery

enum class Status {
CONNECTING,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.boswelja.watchconnection.core
package com.boswelja.watchconnection.core.message

import java.util.UUID

/**
* A data class containing information about a received message.
* @param sourceWatchId The [Watch.id] of the watch that sent the message.
* @param sourceWatchId The [com.boswelja.watchconnection.core.Watch.id] of the watch that sent the
* message.
* @param message The message itself.
* @param data Any data that may have been included with the message, or null if there is none.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.boswelja.watchconnection.core.message

import com.boswelja.watchconnection.core.BaseClient
import com.boswelja.watchconnection.core.Platform
import com.boswelja.watchconnection.core.Watch
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.merge

/**
* MessageClient takes a number of [MessagePlatform]s, and provides a common interface between them.
* @param platforms The [MessagePlatform]s this MessageClient should support.
*/
class MessageClient(
vararg platforms: MessagePlatform
) : BaseClient<MessagePlatform>(*platforms) {

/**
* A [Flow] of [Message]s received by all [Platform]s. See [MessagePlatform.incomingMessages].
*/
@ExperimentalCoroutinesApi
fun incomingMessages(): Flow<Message> =
platforms.values.map { it.incomingMessages() }.merge()

/**
* Send a message to a [Watch]. See [MessagePlatform.sendMessage].
* @param to The [Watch] to send the message to.
* @param message The message to send.
* @param data The data to send with the message, if any.
* @return true if sending the message was successful, false otherwise.
*/
suspend fun sendMessage(
to: Watch,
message: String,
data: ByteArray? = null,
priority: Message.Priority = Message.Priority.LOW
) = platforms[to.platform]?.sendMessage(to.platformId, message, data, priority) == true
}
Loading

0 comments on commit d5a56d0

Please sign in to comment.