'Where to send message to smartwatch? ViewModel, UseCase or Repository?
Where should I place/split the following code?
The ViewModel shouldn't be responsible for business logic, but I also don't know how to handle the UseCases, have two 'special' UseCases for updating the Ip and the object, which then depend on a general UseCase (SendDataToWatch) to actually send the data? In that case, where should I get the Nodes list and decide to which node to send the message, in ViewModel?
Or should I pass the context to the Repository, but then, which one? (ConnectionDataRep, CmdObjectsRep or one created for this purpose).
Also, regarding the ThreadExecutor, since it is expensive to create, should I pass it from the ViewModel, or create it in each UseCase/Repository?
private Collection<String> getNodes() {
HashSet<String> results = new HashSet<>();
Wearable.getCapabilityClient()
Task<CapabilityInfo> nodeListTask = Wearable.getCapabilityClient(context).getCapability(WATCH_CAPABILITY, CapabilityClient.FILTER_REACHABLE);
try {
// Block on a task and get the result synchronously (because this is on a background
// thread).
Set<Node> nodes = Tasks.await(nodeListTask).getNodes();
for (Node node : nodes) {
results.add(node.getId());
}
} catch (ExecutionException exception) {
Log.e(TAG, "Task failed: " + exception);
} catch (InterruptedException exception) {
Log.e(TAG, "Interrupt occurred: " + exception);
}
return results;
}
private void updateAddrOnWatch(String node, String message) {
sendMessageToEndpoint(node, KeysConstants.UPDATE_CONN_PATH, strToBytes(message));
}
private void updateCmdObjects(String node, String jsonObjectsArr) {
sendMessageToEndpoint(node, KeysConstants.UPDATE_OBJECTS_PATH, strToBytes(jsonObjectsArr));
}
private void sendMessageToEndpoint(String node, String endpoint, byte[] message) {
Task<Integer> sendMessageTask =
Wearable.getMessageClient(context)
.sendMessage(node, endpoint, message);
try {
// Block on a task and get the result synchronously (because this is on a background
// thread).
Integer result = Tasks.await(sendMessageTask);
Log.d(TAG, "Message sent: " + result);
} catch (ExecutionException exception) {
Log.e(TAG, "Task failed: " + exception);
} catch (InterruptedException exception) {
Log.e(TAG, "Interrupt occurred: " + exception);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
