This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from boswelja/refactoring
breaking: Reorganise library
- Loading branch information
Showing
49 changed files
with
1,139 additions
and
939 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
19 changes: 19 additions & 0 deletions
19
core/src/main/kotlin/com/boswelja/watchconnection/core/BaseClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
core/src/main/kotlin/com/boswelja/watchconnection/core/Platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
core/src/main/kotlin/com/boswelja/watchconnection/core/WatchPlatform.kt
This file was deleted.
Oops, something went wrong.
92 changes: 0 additions & 92 deletions
92
core/src/main/kotlin/com/boswelja/watchconnection/core/WatchPlatformManager.kt
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
core/src/main/kotlin/com/boswelja/watchconnection/core/discovery/DiscoveryClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
core/src/main/kotlin/com/boswelja/watchconnection/core/discovery/DiscoveryPlatform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
2 changes: 1 addition & 1 deletion
2
...m/boswelja/watchconnection/core/Status.kt → .../watchconnection/core/discovery/Status.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
.../boswelja/watchconnection/core/Message.kt → ...a/watchconnection/core/message/Message.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
core/src/main/kotlin/com/boswelja/watchconnection/core/message/MessageClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.