Skip to content

Commit

Permalink
Add KDocs.
Browse files Browse the repository at this point in the history
  • Loading branch information
slambang committed Nov 15, 2022
1 parent 17eae91 commit ef0de95
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import android.util.Log
import com.jarvis.app.BuildConfig
import com.jarvis.app.contentprovider.util.ReadOnlyContentProvider
import com.jarvis.app.di.JarvisContentProviderEntryPoint
import com.jarvis.app.domain.fields.JarvisContentProviderController
import com.jarvis.app.domain.fields.JarvisContentProviderViewModel
import java.lang.RuntimeException

class JarvisContentProvider : ReadOnlyContentProvider() {

private val controller: JarvisContentProviderController by lazy {
JarvisContentProviderEntryPoint.getController(context!!)
private val viewModel: JarvisContentProviderViewModel by lazy {
JarvisContentProviderEntryPoint.getViewModel(requireContext())
}

override fun onCreate(): Boolean = true
Expand All @@ -39,7 +39,7 @@ class JarvisContentProvider : ReadOnlyContentProvider() {
private fun onConfigLocationReceived(uri: Uri): Int {
log("- Config file URI received: $uri")

if (controller.isJarvisLocked) {
if (viewModel.isJarvisLocked) {
log("- Jarvis is locked. Skipping.")
return JARVIS_PULL_CONFIG_RESULT_LOCKED
}
Expand All @@ -56,7 +56,7 @@ class JarvisContentProvider : ReadOnlyContentProvider() {
?: throw RuntimeException("Failed to open config input stream.")

stream.use {
controller.onConfigPush(it)
viewModel.onConfigPush(it)
}

return JARVIS_PULL_CONFIG_RESULT_SUCCESS
Expand Down Expand Up @@ -87,9 +87,9 @@ class JarvisContentProvider : ReadOnlyContentProvider() {
uri.pathSegments[PATH_VALUE_INDEX] == PATH_VALUE

private fun readFieldValue(uri: Uri): String? {
if (!controller.isJarvisActive) return null
if (!viewModel.isJarvisActive) return null
val fieldName = uri.pathSegments[PATH_CONFIG_VALUE_INDEX]
return controller.getFieldValue(fieldName)
return viewModel.getFieldValue(fieldName)
}

private fun log(message: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jarvis.app.di

import android.content.Context
import com.jarvis.app.domain.fields.JarvisContentProviderController
import com.jarvis.app.domain.fields.JarvisContentProviderViewModel
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
Expand All @@ -14,10 +14,10 @@ import dagger.hilt.components.SingletonComponent
@InstallIn(SingletonComponent::class)
interface JarvisContentProviderEntryPoint {

fun jarvisContentProviderController(): JarvisContentProviderController
fun jarvisContentProviderController(): JarvisContentProviderViewModel

companion object {
fun getController(context: Context): JarvisContentProviderController =
fun getViewModel(context: Context): JarvisContentProviderViewModel =
EntryPointAccessors.fromApplication(
context.applicationContext,
JarvisContentProviderEntryPoint::class.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.jarvis.client.data.StringListField
import java.io.InputStream
import javax.inject.Inject

class JarvisContentProviderController @Inject constructor(
class JarvisContentProviderViewModel @Inject constructor(
private val settingsInteractor: SettingsInteractor,
private val getSingleField: GetSingleFieldUseCase,
private val refreshConfig: RefreshConfigUseCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,10 @@ private val differCallback = object : DiffUtil.ItemCallback<FieldItemViewModel<*
override fun areItemsTheSame(
oldItem: FieldItemViewModel<*>,
newItem: FieldItemViewModel<*>
): Boolean {
return oldItem.name == newItem.name
}
): Boolean = oldItem.name == newItem.name

override fun areContentsTheSame(
oldItem: FieldItemViewModel<*>,
newItem: FieldItemViewModel<*>
): Boolean {
return oldItem == newItem
}
): Boolean = oldItem == newItem
}
4 changes: 2 additions & 2 deletions jarvis-app/src/main/java/com/jarvis/app/view/util/Ext.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.jarvis.app.view.util

fun String.toSafeLong(default: Long = 0): Long =
fun String.toSafeLong(default: Long): Long =
try {
this.toLong()
} catch (error: Throwable) {
default
}

fun String.toSafeDouble(default: Double = 0.0): Double =
fun String.toSafeDouble(default: Double): Double =
try {
this.toDouble()
} catch (error: Throwable) {
Expand Down
2 changes: 0 additions & 2 deletions jarvis-client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,4 @@ android {
dependencies {
implementation "androidx.appcompat:appcompat:1.4.1"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2"

testImplementation "junit:junit:4.13.2"
}
37 changes: 26 additions & 11 deletions jarvis-client/src/main/java/com/jarvis/client/JarvisClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@ package com.jarvis.client
import android.content.Context
import com.jarvis.client.data.ClientJsonMapper
import com.jarvis.client.data.JarvisConfig
import com.jarvis.client.data.jarvisConfig

/**
* The Jarvis interface, obtained via [JarvisClient.newInstance].
* The Jarvis interface, created via [JarvisClient.newInstance].
*
* @see [com.jarvis.client.data.jarvisConfig] to create a Jarvis config.
* Pass your Jarvis config to [JarvisClient.pushConfigToJarvisApp].
* Read data from the Jarvis App via the `get*()` functions.
* 1. @see [jarvisConfig] to create a Jarvis config.
* 2. Pass your Jarvis config to [JarvisClient.pushConfigToJarvisApp].
* 3. Read data from the Jarvis App via the `get*()` functions.
*
* If the Jarvis App is installed then all `get*()` functions read data from there.
* If the Jarvis App is installed then all `get*()` functions return data from the config there.
* If the Jarvis App is not installed then the default values are returned.
*/
interface JarvisClient {

/**
* Enable/disable Jarvis's internal logging.
* Useful for debugging purposes.
*/
var loggingEnabled: Boolean

/**
* Pushes your app's Jarvis configuration to the Jarvis app.
* Pushes your app's Jarvis config to the Jarvis App.
* Note that the Jarvis App must be in an *unlocked* state to accept new configs.
*
* @return [JarvisPushConfigResult] indicating the push operation result.
*/
fun pushConfigToJarvisApp(config: JarvisConfig): JarvisPushConfigResult

/**
* Gets a named String value from the Jarvis App.
*
* @return The named String, or the default value if the Jarvis App is not installed
* @param name The name of the string
* @param defaultValue The default value
*/
Expand All @@ -37,14 +43,16 @@ interface JarvisClient {
/**
* Gets a named String value from the Jarvis App.
*
* @return The named String, or the default value if the Jarvis App is not installed
* @param name The name of the string
* @param lazyDefaultValue The lazily obtained default value
* @param lazyDefaultValue The lazily created default value
*/
fun getString(name: String, lazyDefaultValue: () -> String): String

/**
* Gets a named Boolean value from the Jarvis App.
*
* @return The named Boolean, or the default value if the Jarvis App is not installed
* @param name The name of the boolean
* @param defaultValue The default value
*/
Expand All @@ -53,14 +61,16 @@ interface JarvisClient {
/**
* Gets a named Boolean value from the Jarvis App.
*
* @return The named Boolean, or the default value if the Jarvis App is not installed
* @param name The name of the boolean
* @param lazyDefaultValue The lazily obtained default value
* @param lazyDefaultValue The lazily created default value
*/
fun getBoolean(name: String, lazyDefaultValue: () -> Boolean): Boolean

/**
* Gets a named Long value from the Jarvis App.
*
* @return The named Long, or the default value if the Jarvis App is not installed
* @param name The name of the long
* @param defaultValue The default value
*/
Expand All @@ -69,14 +79,16 @@ interface JarvisClient {
/**
* Gets a named Long value from the Jarvis App.
*
* @return The named Long, or the default value if the Jarvis App is not installed
* @param name The name of the long
* @param lazyDefaultValue The lazily obtained default value
* @param lazyDefaultValue The lazily created default value
*/
fun getLong(name: String, lazyDefaultValue: () -> Long): Long

/**
* Gets a named Double value from the Jarvis App.
*
* @return The named Double, or the default value if the Jarvis App is not installed
* @param name The name of the double
* @param defaultValue The default value
*/
Expand All @@ -85,14 +97,16 @@ interface JarvisClient {
/**
* Gets a named Double value from the Jarvis App.
*
* @return The named Double, or the default value if the Jarvis App is not installed
* @param name The name of the double
* @param lazyDefaultValue The lazily obtained default value
* @param lazyDefaultValue The lazily created default value
*/
fun getDouble(name: String, lazyDefaultValue: () -> Double): Double

/**
* Gets the index selection from a String list from the Jarvis App.
*
* @return The named String List selection index, or the default value if the Jarvis App is not installed
* @param name The name of the string list
* @param defaultValue The default selection index
*/
Expand All @@ -101,8 +115,9 @@ interface JarvisClient {
/**
* Gets the index selection from a String list from the Jarvis App.
*
* @return The named String List selection index, or the default value if the Jarvis App is not installed
* @param name The name of the String list
* @param lazyDefaultValue The lazily obtained default value
* @param lazyDefaultValue The lazily created default value
*/
fun getStringListSelection(name: String, lazyDefaultValue: () -> Int): Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ internal class JarvisClientImpl(
it.delete()
}

val destinationOutputStream = FileOutputStream(destinationFile)
val fileOutputStream = FileOutputStream(destinationFile)
val configInputStream = jsonMapper.mapToJsonString(config).byteInputStream()

configInputStream.use { inputStream ->
destinationOutputStream.use { outputStream ->
fileOutputStream.use { outputStream ->
var read: Int
val buff = ByteArray(2048)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package com.jarvis.client

/**
* Result of [JarvisClient.pushConfigToJarvisApp].
*/
enum class JarvisPushConfigResult {
/**
* The config push succeeded.
*/
SUCCESS,

/**
* The config push failed.
*/
FAILURE,

/**
* The Jarvis app is locked and cannot accept new config.
*/
LOCKED
}
Loading

0 comments on commit ef0de95

Please sign in to comment.