Skip to content

Commit

Permalink
Merge pull request #4 from bearlysophisticated/master
Browse files Browse the repository at this point in the history
Extend API with callback to signal events
  • Loading branch information
fennifith authored Dec 17, 2019
2 parents 5441f82 + f5e22a2 commit cc0ef01
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 71 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,28 @@ if (doki != null) doki.loadContent();
</p>
</details>

You can pass a `DokiContentCallback` instance to the `loadContent` method to receive events:

<details open>
<summary>Kotlin</summary>
<p>

```kotlin
val doki : DokiContentView? = findViewById(R.id.doki_content)
doki?.loadContent(callback = object : DokiContentCallback {
override fun onStartedToLoad() {}
override fun onLoaded() {}
override fun onFailedToLoad() {}
})
```

</p>
</details>

## Customization

If you are using the `DokiContentView`, you can customize it by setting custom attributes.
Please check [this file](https://github.com/DoubleDotLabs/doki/blob/master/app/src/main/res/layout/layout_doki_view_custom.xml)
Please check [this file](https://github.com/doubledotlabs/doki/blob/master/sample/src/main/res/layout/activity_doki_custom.xml)
which implements every option available.

For custom fonts and text styles, you can override the following styles:
Expand All @@ -135,6 +153,9 @@ For custom fonts and text styles, you can override the following styles:
<style name="Doki.Custom.Headline" parent="Doki.Headline"/>
<style name="Doki.Custom.Overline" parent="Doki.Overline"/>
<style name="Doki.Custom.Button" parent="Doki.Button"/>
<style name="Doki.Custom.Body" parent="Doki.Body"/>
<style name="Doki.Custom.Header" parent="Doki.Header"/>
<style name="Doki.Custom.Subtext" parent="Doki.Subtext"/>
```

And add the following attributes as you wish and with the values you want:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package dev.doubledot.doki.api.extensions
import android.os.Build
import android.os.Build.VERSION_CODES.*

val androidVersion: String
internal val androidVersion: String
get() {
var version = Build.VERSION.RELEASE ?: ""
if (!version.hasContent()) version = Build.VERSION.CODENAME ?: ""
return version
}

val androidVersionName: String
internal val androidVersionName: String
get() {
return when (Build.VERSION.SDK_INT) {
JELLY_BEAN, JELLY_BEAN_MR1, JELLY_BEAN_MR2 -> "JellyBean"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.doubledot.doki.api.extensions
import java.math.RoundingMode
import java.text.DecimalFormat

fun String.hasContent() = isNotEmpty() && isNotBlank()
internal fun String.hasContent() = isNotEmpty() && isNotBlank()

internal fun Number.round(decimalCount: Int): String {
val expression = StringBuilder().append("#.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path

interface DokiApiService {
internal interface DokiApiService {

@GET("{manufacturer}.json")
fun getManufacturer(@Path("manufacturer") manufacturer : String) : Observable<DokiManufacturer>
Expand Down
27 changes: 16 additions & 11 deletions api/src/main/java/dev/doubledot/doki/api/tasks/DokiApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@ import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException

@Suppress("MemberVisibilityCanBePrivate")
open class DokiApi {
class DokiApi {

val dokiApiService by lazy {
private val dokiApiService by lazy {
DokiApiService.create()
}

var disposable: Disposable? = null
var callback: DokiApiCallback? = null
var shouldFallback : Boolean = true
private var disposable: Disposable? = null

fun getManufacturer(manufacturer : String = DONT_KILL_MY_APP_DEFAULT_MANUFACTURER) {
fun getManufacturer(
manufacturer: String = DONT_KILL_MY_APP_DEFAULT_MANUFACTURER,
callback: DokiApiCallback,
shouldFallback: Boolean = true
) {
disposable = dokiApiService.getManufacturer(manufacturer)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ result : DokiManufacturer -> callback?.onSuccess(result) },
{ result: DokiManufacturer -> callback.onSuccess(result) },
{ error ->
if ((error as? HttpException)?.code() == 404 && shouldFallback) {
getManufacturer(DONT_KILL_MY_APP_FALLBACK_MANUFACTURER)
shouldFallback = false
} else callback?.onError(error)
getManufacturer(
DONT_KILL_MY_APP_FALLBACK_MANUFACTURER,
callback,
shouldFallback = false
)
} else callback.onError(error)
}
)

callback?.onStart()
callback.onStart()
}

fun cancel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ import dev.doubledot.doki.api.models.DokiManufacturer
interface DokiApiCallback {
fun onStart() {}
fun onSuccess(response: DokiManufacturer?)
fun onError(e: Throwable?) = e?.printStackTrace()

fun onError(e: Throwable?) {
e?.printStackTrace()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package dev.doubledot.doki.extensions

import android.webkit.WebView

fun WebView.loadHTML(htmlString: String?) = loadData(htmlString.orEmpty(), "text/html", null)
internal fun WebView.loadHTML(htmlString: String?) = loadData(htmlString.orEmpty(), "text/html", null)

2 changes: 1 addition & 1 deletion library/src/main/java/dev/doubledot/doki/models/Device.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.doubledot.doki.models
import android.os.Build
import dev.doubledot.doki.api.extensions.fullAndroidVersion

data class Device(
internal data class Device(
var manufacturer : String = Build.MANUFACTURER,
var model : String = Build.MODEL,
var androidVersion : String = fullAndroidVersion
Expand Down
18 changes: 7 additions & 11 deletions library/src/main/java/dev/doubledot/doki/ui/DokiActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dev.doubledot.doki.api.extensions.DONT_KILL_MY_APP_DEFAULT_MANUFACTURER
import dev.doubledot.doki.api.tasks.DokiApi
import dev.doubledot.doki.views.DokiContentView

public class DokiActivity : AppCompatActivity() {
class DokiActivity : AppCompatActivity() {

var api : DokiApi? = null
private val dokiView: DokiContentView by lazy { DokiContentView(context = this) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val dokiView = DokiContentView(context = this)
setContentView(dokiView)

api = dokiView.loadContent(manufacturerId = intent.extras?.run {
dokiView.loadContent(manufacturerId = intent.extras?.run {
this[MANUFACTURER_EXTRA] as? String
} ?: DONT_KILL_MY_APP_DEFAULT_MANUFACTURER)

Expand All @@ -27,22 +24,21 @@ public class DokiActivity : AppCompatActivity() {

override fun onDestroy() {
super.onDestroy()
api?.cancel()
dokiView.cancel()
}

companion object {

public const val MANUFACTURER_EXTRA = "dev.doubledot.doki.ui.DokiActivity.MANUFACTURER_EXTRA"
private const val MANUFACTURER_EXTRA = "dev.doubledot.doki.ui.DokiActivity.MANUFACTURER_EXTRA"

@JvmOverloads
public fun newIntent(context: Context, manufacturerId: String = DONT_KILL_MY_APP_DEFAULT_MANUFACTURER): Intent {
fun newIntent(context: Context, manufacturerId: String = DONT_KILL_MY_APP_DEFAULT_MANUFACTURER): Intent {
val intent = Intent(context, DokiActivity::class.java)
intent.putExtra(MANUFACTURER_EXTRA, manufacturerId)
return intent
}

@JvmOverloads
public fun start(context : Context, manufacturerId : String = DONT_KILL_MY_APP_DEFAULT_MANUFACTURER) {
fun start(context : Context, manufacturerId : String = DONT_KILL_MY_APP_DEFAULT_MANUFACTURER) {
context.startActivity(newIntent(context, manufacturerId))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.doubledot.doki.ui

interface DokiContentCallback {
fun onStartedToLoad() {}
fun onLoaded() {}
fun onFailedToLoad() {}
}
Loading

0 comments on commit cc0ef01

Please sign in to comment.