-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(logger): added type & duration filters for logs (#312)
Co-authored-by: Aastha <[email protected]>
- Loading branch information
Showing
16 changed files
with
511 additions
and
108 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
pluto-plugins/plugins/logger/lib/src/main/java/com/pluto/plugins/logger/FilterViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.pluto.plugins.logger | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MediatorLiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import com.pluto.plugins.logger.internal.LogTimeStamp | ||
import com.pluto.plugins.logger.internal.LogType | ||
import com.pluto.plugins.logger.internal.Session | ||
import com.pluto.utilities.selector.SelectorOption | ||
|
||
internal class FilterViewModel(application: Application) : AndroidViewModel(application) { | ||
val selectedFiltersList: LiveData<List<LogType>> | ||
get() = _selectedFiltersList | ||
private val _selectedFiltersList = MutableLiveData<List<LogType>>() | ||
|
||
val selectedTimeStamp: LiveData<LogTimeStamp> | ||
get() = _selectedTimeStamp | ||
private val _selectedTimeStamp = MutableLiveData<LogTimeStamp>() | ||
|
||
val searchTextLogger: LiveData<String> | ||
get() = _searchTextLogger | ||
private val _searchTextLogger = MutableLiveData<String>() | ||
|
||
private val preferences = Preferences(application) | ||
private val logTypes = listOf( | ||
LogType("debug"), | ||
LogType("verbose"), | ||
LogType("error"), | ||
LogType("info"), | ||
LogType("event") | ||
) | ||
|
||
private val timeStamps = listOf( | ||
LogTimeStamp(1), | ||
LogTimeStamp(5), | ||
LogTimeStamp(10), | ||
LogTimeStamp(Integer.MIN_VALUE, true) | ||
) | ||
|
||
val isTriggerSearch: LiveData<Boolean> | ||
get() = _isTriggerSearch | ||
private val _isTriggerSearch = MediatorLiveData<Boolean>() | ||
val isFilterApplied: LiveData<Boolean> | ||
get() = _isFilterApplied | ||
private val _isFilterApplied = MediatorLiveData<Boolean>() | ||
|
||
val isFilterVisible: LiveData<Boolean> | ||
get() = _isFilterVisible | ||
private val _isFilterVisible = MediatorLiveData<Boolean>() | ||
|
||
init { | ||
|
||
_isTriggerSearch.addSource(_selectedFiltersList) { _isTriggerSearch.postValue(true) } | ||
_isTriggerSearch.addSource(_searchTextLogger) { _isTriggerSearch.postValue(true) } | ||
_isTriggerSearch.addSource(_selectedTimeStamp) { _isTriggerSearch.postValue(true) } | ||
_searchTextLogger.postValue(Session.loggerSearchText) | ||
_selectedFiltersList.postValue(preferences.selectedFilterLogType) | ||
_selectedTimeStamp.postValue(preferences.selectedFilterTime) | ||
_isFilterApplied.addSource(_selectedFiltersList) { | ||
if (it.isNotEmpty() || getSelectedTimeStamp().timeStamp != 0) { | ||
_isFilterApplied.postValue(true) | ||
} else { | ||
_isFilterApplied.postValue(false) | ||
} | ||
} | ||
_isFilterApplied.addSource(_selectedTimeStamp) { | ||
if (it.timeStamp != 0 || getSelectedFilters().isNotEmpty()) { | ||
_isFilterApplied.postValue(true) | ||
} else { | ||
_isFilterApplied.postValue(false) | ||
} | ||
} | ||
_isFilterVisible.postValue(false) | ||
} | ||
|
||
fun getLogTypes(): List<SelectorOption> { | ||
return logTypes | ||
} | ||
|
||
fun getTimeStamps(): List<SelectorOption> { | ||
return timeStamps | ||
} | ||
|
||
fun getSelectedFilters(): List<LogType> { | ||
return selectedFiltersList.value ?: emptyList() | ||
} | ||
|
||
fun getSelectedTimeStamp(): LogTimeStamp { | ||
return selectedTimeStamp.value ?: LogTimeStamp(0, false) | ||
} | ||
|
||
fun updateSearchText(searchText: String) { | ||
_searchTextLogger.postValue(searchText) | ||
Session.loggerSearchText = searchText | ||
} | ||
|
||
fun setSelectedFiltersLogType(logTypeList: ArrayList<LogType>) { | ||
_selectedFiltersList.postValue(logTypeList) | ||
preferences.selectedFilterLogType = logTypeList | ||
} | ||
fun setSelectedFilterTimeStamp(logTimeStamp: LogTimeStamp) { | ||
_selectedTimeStamp.postValue(logTimeStamp) | ||
preferences.selectedFilterTime = logTimeStamp | ||
} | ||
fun getSearchText(): String { | ||
return searchTextLogger.value ?: "" | ||
} | ||
|
||
fun toggleFilterViewVisibility() { | ||
_isFilterVisible.postValue(_isFilterVisible.value?.not()) | ||
} | ||
fun clearFilters() { | ||
preferences.clearFilters() | ||
_selectedFiltersList.postValue(emptyList()) | ||
_selectedTimeStamp.postValue(LogTimeStamp(0, false)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
pluto-plugins/plugins/logger/lib/src/main/java/com/pluto/plugins/logger/Preferences.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.pluto.plugins.logger | ||
|
||
import android.content.Context | ||
import com.pluto.plugins.logger.internal.LogTimeStamp | ||
import com.pluto.plugins.logger.internal.LogType | ||
import com.squareup.moshi.JsonAdapter | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.Types | ||
|
||
internal class Preferences(context: Context) { | ||
|
||
private val settingsPrefs by lazy { context.preferences("_pluto_log_filter_settings") } | ||
private val moshi: Moshi = Moshi.Builder().build() | ||
private val moshiAdapter: JsonAdapter<List<String>> = | ||
moshi.adapter(Types.newParameterizedType(List::class.java, String::class.java)) | ||
|
||
private var timeStampAdapter: JsonAdapter<LogTimeStamp> = | ||
moshi.adapter(LogTimeStamp::class.java) | ||
|
||
internal var selectedFilterLogType: List<LogType> | ||
get() = settingsPrefs.getString(SELECTED_FILTER_LOG_TYPE, null)?.let { | ||
moshiAdapter.fromJson(it)?.map { type -> LogType(type) } | ||
} ?: run { emptyList() } | ||
set(value) = settingsPrefs.edit() | ||
.putString(SELECTED_FILTER_LOG_TYPE, moshiAdapter.toJson(value.map { it.type })).apply() | ||
|
||
internal var selectedFilterTime: LogTimeStamp | ||
get() = settingsPrefs.getString(SELECTED_FILTER_TIMESTAMP, null)?.let { | ||
timeStampAdapter.fromJson(it) | ||
} ?: run { LogTimeStamp(0, false) } | ||
set(value) = settingsPrefs.edit() | ||
.putString(SELECTED_FILTER_TIMESTAMP, timeStampAdapter.toJson(value)).apply() | ||
|
||
companion object { | ||
private const val SELECTED_FILTER_LOG_TYPE = "selected_filter_logtype" | ||
private const val SELECTED_FILTER_TIMESTAMP = "selected_filter_timestamp" | ||
} | ||
|
||
fun clearFilters() { | ||
settingsPrefs.edit().clear().apply() | ||
} | ||
} | ||
|
||
private fun Context.preferences(name: String, mode: Int = Context.MODE_PRIVATE) = | ||
getSharedPreferences(name, mode) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.