Skip to content

Commit

Permalink
feat: Add logging ability
Browse files Browse the repository at this point in the history
  • Loading branch information
haroldadmin committed Jun 22, 2019
1 parent c8ff415 commit 5ae314a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
"targetSdk" : 28,
"kotlin" : "1.3.40",
"agp" : "3.5.0-beta04",
"versionCode": 5,
"versionName": "0.1.0"
"versionCode": 6,
"versionName": "0.2.0"
]

ext.versions = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ package com.haroldadmin.sampleapp

import android.app.Application
import com.haroldadmin.sampleapp.utils.Provider
import com.haroldadmin.vector.Vector
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext

class EntityCounter : Application(), CoroutineScope {

private val job = Job()
override val coroutineContext: CoroutineContext = Dispatchers.Main
private val applicationScope = CoroutineScope(coroutineContext)

lateinit var provider: Provider

override fun onCreate() {
super.onCreate()
provider = Provider(this, applicationScope)
Vector.enableLogging()
provider = Provider(context = this, coroutineScope = this)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.haroldadmin.sampleapp.addEntity

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -44,7 +43,6 @@ class AddEntityFragment : VectorFragment() {
}

override fun renderState() = withState(viewModel) { state ->
Log.d("AddEntityFragment", "State: $state")
if (state.isSaved) {
Snackbar.make(binding.root, R.string.entitySavedMessage, Snackbar.LENGTH_SHORT).show()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.haroldadmin.sampleapp.entities

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -62,7 +61,6 @@ class EntitiesFragment : VectorFragment() {
}

override fun renderState() = withState(viewModel) { state ->
Log.d("EntitiesFragment", "State: $state")
entitiesAdapter.submitList(state.entities)
if (state.isLoading) {
binding.pbLoading.show()
Expand Down
17 changes: 17 additions & 0 deletions vector/src/main/java/com/haroldadmin/vector/Vector.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.haroldadmin.vector

import android.util.Log

object Vector {

private const val TAG = "Vector"
private var loggingEnabled = false

fun enableLogging() { loggingEnabled = true }

fun disableLogging() { loggingEnabled = false }

internal fun log(message: String) {
if (loggingEnabled) Log.d(TAG, message)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.haroldadmin.vector.viewModel

import com.haroldadmin.vector.Vector
import com.haroldadmin.vector.VectorState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -56,10 +57,13 @@ internal class StateStoreImpl<S : VectorState>(
consumeEach { action ->
when (action) {
is SetStateAction -> {
Vector.log("Processing set-state block")
val newState = action.reducer(state)
Vector.log("Sending new state to channel: $newState")
stateChannel.offer(newState)
}
is GetStateAction -> {
Vector.log("Processing get-state block")
getStateQueue.offer(action.block)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.haroldadmin.vector.Vector
import com.haroldadmin.vector.VectorState
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.launch
Expand All @@ -15,6 +16,7 @@ import kotlinx.coroutines.launch
*
* @param S The state class for this ViewModel implementing [VectorState]
* @param initialState The initial state for this ViewModel
* @param enableLogging Flag to enable/disable debug logs on state updates
*
* Initial State can be used in conjunction with fragment provided state to
* recover from process deaths.
Expand All @@ -35,7 +37,7 @@ import kotlinx.coroutines.launch
* }
* }
*/
abstract class VectorViewModel<S : VectorState>(private val initialState: S) : ViewModel() {
abstract class VectorViewModel<S : VectorState>(private val initialState: S, private val enableLogging: Boolean = false) : ViewModel() {

/**
* The state store associated with this view model.
Expand Down Expand Up @@ -86,12 +88,14 @@ abstract class VectorViewModel<S : VectorState>(private val initialState: S) : V

init {
viewModelScope.launch {
Vector.log("Connecting StateChannel to LiveData")
stateStore.stateChannel.consumeEach { state -> _state.value = state }
}
}

@CallSuper
override fun onCleared() {
Vector.log("Clearing ViewModel")
super.onCleared()
stateStore.cleanup()
}
Expand Down

0 comments on commit 5ae314a

Please sign in to comment.