Skip to content

Commit

Permalink
Debug improvements (Co-Epi#93)
Browse files Browse the repository at this point in the history
* Initialize log level with debug

* Show a notification when copied the logs
  • Loading branch information
ivnsch authored Apr 19, 2020
1 parent 84add28 commit cecd5c4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/org/coepi/android/di/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ val viewModelModule = module {
viewModel { CENViewModel(get(), get(), get()) }
viewModel { LocationViewModel() }
viewModel { OnboardingViewModel(get(), get()) }
viewModel { LogsViewModel(cachingLog, get(), get()) }
viewModel { LogsViewModel(cachingLog, get(), get(), get()) }
viewModel { DebugViewModel(get()) }
viewModel { DebugBleViewModel(get()) }
viewModel { ContainerViewModel(get()) }
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/org/coepi/android/ui/debug/logs/LogsFragment.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.coepi.android.ui.debug.logs

import android.os.Bundle
import android.view.HapticFeedbackConstants
import android.view.HapticFeedbackConstants.LONG_PRESS
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -41,12 +39,20 @@ class LogsFragment: Fragment() {
item_min_loglevel, LogLevel.values().map { it.text }.toTypedArray())
logLevelAdapter.setDropDownViewResource(item_min_loglevel)
spinLevel.adapter = logLevelAdapter
spinLevel.setSelection(0)

viewModel.selectedLogLevel.observeWith(viewLifecycleOwner) {
spinLevel.setSelection(it.ordinal, false)
}

// Ignore automatic call to onItemSelected when fragment loads
var firstOnItemSelectedCall = true
spinLevel.onItemSelected {
val selected = spinLevel.selectedItem.toString()
val logLevel = LogLevel.valueOf("${selected[0]}") // FIXME using the first letter to get enum value is a hack
viewModel.onLogLevelSelected(logLevel)
if (!firstOnItemSelectedCall) {
viewModel.onLogLevelSelected(logLevel)
}
firstOnItemSelectedCall = false
}

viewModel.logs.observeWith(viewLifecycleOwner) {
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/java/org/coepi/android/ui/debug/logs/LogsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@ import org.coepi.android.system.Clipboard
import org.coepi.android.system.EnvInfos
import org.coepi.android.system.log.CachingLog
import org.coepi.android.system.log.LogLevel
import org.coepi.android.system.log.LogLevel.D
import org.coepi.android.system.log.LogLevel.V
import org.coepi.android.system.log.LogMessage
import org.coepi.android.ui.common.UINotificationData
import org.coepi.android.ui.common.UINotificationData.Success
import org.coepi.android.ui.common.UINotifier
import org.coepi.android.ui.notifications.NotificationsShower

class LogsViewModel(
logger: CachingLog,
private val clipboard: Clipboard,
private val envInfos: EnvInfos
private val envInfos: EnvInfos,
private val uiNotifier: UINotifier
) : ViewModel() {

private val selectedLogLevelSubject: BehaviorSubject<LogLevel> = createDefault(V)
private val selectedLogLevelSubject: BehaviorSubject<LogLevel> = createDefault(D)
val selectedLogLevel: LiveData<LogLevel> = selectedLogLevelSubject
.distinct()
.toLiveData()

private val logDoubleClickTrigger = PublishSubject.create<Unit>()

Expand All @@ -46,7 +55,10 @@ class LogsViewModel(
.map { (_, logs) ->
"${envInfos.clipboardString()}\n\n${logs.toClipboardText()}"
}
.subscribe { clipboard.putInClipboard(it) }
.subscribe {
clipboard.putInClipboard(it)
uiNotifier.notify(Success("Logs copied to clipboard"))
}
}

fun onLogLevelSelected(logLevel: LogLevel) {
Expand Down

0 comments on commit cecd5c4

Please sign in to comment.