Skip to content

Commit

Permalink
[refactor]: changed logic for opening files in LogFox
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Oct 18, 2023
1 parent 46fd9f5 commit 815bfc5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 100 deletions.
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

<activity
android:name=".ui.activity.MainActivity"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustResize"
android:exported="true">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ package com.f0x1d.logfox.extensions.views.widgets

import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.FragmentActivity
import androidx.navigation.findNavController
import com.f0x1d.logfox.R

fun Toolbar.setupBackButton(onClickListener: View.OnClickListener) {
navigationIcon ?: setNavigationIcon(R.drawable.ic_arrow_back)
setNavigationIcon(R.drawable.ic_arrow_back)
setNavigationOnClickListener(onClickListener)
setNavigationContentDescription(androidx.appcompat.R.string.abc_action_bar_up_description)
}

fun Toolbar.setupBackButtonForNavController() {
setupBackButton { findNavController().popBackStack() }
fun Toolbar.setupBackButtonForNavController() = setupBackButton {
findNavController().popBackStack()
}

fun Toolbar.setupBackButtonForBackPressedDispatcher(activity: FragmentActivity) = setupBackButton {
activity.onBackPressedDispatcher.onBackPressed()
}

fun Toolbar.setupCloseButton() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.hilt.navigation.fragment.hiltNavGraphViewModels
import androidx.navigation.fragment.navArgs
import com.f0x1d.logfox.R
import com.f0x1d.logfox.databinding.SheetSearchBinding
import com.f0x1d.logfox.ui.dialog.base.BaseBottomSheet
Expand All @@ -16,16 +15,16 @@ class SearchBottomSheet: BaseBottomSheet<SheetSearchBinding>() {

private val logsViewModel by hiltNavGraphViewModels<LogsViewModel>(R.id.logsFragment)

private val navArgs by navArgs<SearchBottomSheetArgs>()

override fun inflateBinding(inflater: LayoutInflater, container: ViewGroup?) = SheetSearchBinding.inflate(inflater, container, false)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.queryText.setText(navArgs.query)
val query = logsViewModel.query.value

binding.queryText.setText(query)

binding.clearSearchButton.visibility = if (navArgs.query == null) View.GONE else View.VISIBLE
binding.clearSearchButton.visibility = if (query == null) View.GONE else View.VISIBLE
binding.clearSearchButton.setOnClickListener {
search(null)
}
Expand Down
88 changes: 35 additions & 53 deletions app/src/main/java/com/f0x1d/logfox/ui/fragment/LogsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import com.f0x1d.logfox.adapter.LogsAdapter
import com.f0x1d.logfox.databinding.FragmentLogsBinding
import com.f0x1d.logfox.extensions.copyText
import com.f0x1d.logfox.extensions.isHorizontalOrientation
import com.f0x1d.logfox.extensions.readFileName
import com.f0x1d.logfox.extensions.sendKillApp
import com.f0x1d.logfox.extensions.sendStopService
import com.f0x1d.logfox.extensions.startLoggingService
import com.f0x1d.logfox.extensions.views.widgets.invalidateNavigationButton
import com.f0x1d.logfox.extensions.views.widgets.setClickListenerOn
import com.f0x1d.logfox.extensions.views.widgets.setupBackButtonForBackPressedDispatcher
import com.f0x1d.logfox.extensions.views.widgets.setupCloseButton
import com.f0x1d.logfox.ui.fragment.base.BaseViewModelFragment
import com.f0x1d.logfox.utils.fillWithStrings
Expand All @@ -44,11 +44,6 @@ class LogsFragment: BaseViewModelFragment<LogsViewModel, FragmentLogsBinding>(),
}
private var changingState = false

private val stopViewingFileBackPressedCallback = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
viewModel.stopViewingFile()
}
}
private val clearSelectionBackPressedCallback = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
viewModel.selectedItems.update {
Expand Down Expand Up @@ -83,7 +78,7 @@ class LogsFragment: BaseViewModelFragment<LogsViewModel, FragmentLogsBinding>(),
viewModel.switchState()
}
setClickListenerOn(R.id.search_item) {
findNavController().navigate(LogsFragmentDirections.actionLogsFragmentToSearchBottomSheet(viewModel.query.value))
findNavController().navigate(LogsFragmentDirections.actionLogsFragmentToSearchBottomSheet())
}
setClickListenerOn(R.id.filters_item) {
findNavController().navigate(LogsFragmentDirections.actionLogsFragmentToFiltersFragment())
Expand Down Expand Up @@ -112,14 +107,6 @@ class LogsFragment: BaseViewModelFragment<LogsViewModel, FragmentLogsBinding>(),
requireContext().sendKillApp()
}
}
binding.toolbar.setNavigationOnClickListener {
viewModel.apply {
if (selectedItems.value.isNotEmpty()) selectedItems.update {
emptyList()
} else if (viewModel.viewingFile.value)
viewModel.stopViewingFile()
}
}

binding.logsRecycler.layoutManager = LinearLayoutManager(requireContext())
binding.logsRecycler.itemAnimator = null
Expand All @@ -144,17 +131,12 @@ class LogsFragment: BaseViewModelFragment<LogsViewModel, FragmentLogsBinding>(),
scrollLogToBottom()
}

viewModel.viewingFile.asLiveData().observe(viewLifecycleOwner) {
stopViewingFileBackPressedCallback.isEnabled = it
setupToolbarForFile(it)
}

viewModel.selectedItems.asLiveData().observe(viewLifecycleOwner) {
val selecting = it.isNotEmpty()

clearSelectionBackPressedCallback.isEnabled = selecting
adapter.selectedItems = it

adapter.selectedItems = it
setupToolbarForSelection(selecting, it.size)
}

Expand All @@ -169,15 +151,18 @@ class LogsFragment: BaseViewModelFragment<LogsViewModel, FragmentLogsBinding>(),

viewModel.paused.asLiveData().observe(viewLifecycleOwner) { paused ->
changingState = true

binding.toolbar.menu.findItem(R.id.pause_item)
.setIcon(if (paused) R.drawable.ic_play else R.drawable.ic_pause)
.setTitle(if (paused) R.string.resume else R.string.pause)

if (paused) {
binding.scrollFab.show()
} else {
binding.scrollFab.hide()
scrollLogToBottom()
}

changingState = false
}

Expand All @@ -186,46 +171,18 @@ class LogsFragment: BaseViewModelFragment<LogsViewModel, FragmentLogsBinding>(),
}

requireActivity().onBackPressedDispatcher.apply {
addCallback(viewLifecycleOwner, stopViewingFileBackPressedCallback)
addCallback(viewLifecycleOwner, clearSelectionBackPressedCallback)
}
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
viewModel.appPreferences.apply {
if (key == "pref_logs_text_size") adapter.textSize = logsTextSize.toFloat()
else if (key == "pref_logs_expanded") adapter.logsExpanded = logsExpanded

if (key?.startsWith("pref_show_log") == true) adapter.logsFormat = showLogValues
}
}

override fun onDestroy() {
super.onDestroy()
viewModel.appPreferences.unregisterListener(this)
}

private fun setupToolbarForFile(viewing: Boolean) = binding.toolbar.apply {
menu.findItem(R.id.pause_item).isVisible = !viewing

title = when (viewing) {
true -> viewModel.fileUri?.readFileName(requireContext())

else -> getString(R.string.app_name)
}

if (viewing) setupCloseButton()
else invalidateNavigationButton()
}

private fun setupToolbarForSelection(selecting: Boolean, count: Int) = binding.toolbar.apply {
val setVisibility = { itemId: Int, visible: Boolean ->
menu.findItem(itemId).isVisible = visible
}
val visibleDuringSelection = { itemId: Int -> setVisibility(itemId, selecting) }
val invisibleDuringSelection = { itemId: Int -> setVisibility(itemId, !selecting) }

invisibleDuringSelection(R.id.pause_item)
setVisibility(R.id.pause_item, !selecting && !viewModel.viewingFile)
invisibleDuringSelection(R.id.search_item)
invisibleDuringSelection(R.id.filters_item)
visibleDuringSelection(R.id.selected_item)
Expand All @@ -235,13 +192,24 @@ class LogsFragment: BaseViewModelFragment<LogsViewModel, FragmentLogsBinding>(),

title = when {
selecting -> resources.getQuantityString(R.plurals.selected_count, count, count)
viewModel.viewingFile.value -> viewModel.fileUri?.readFileName(requireContext())
viewModel.viewingFile -> viewModel.viewingFileName

else -> getString(R.string.app_name)
}

if (selecting) setupCloseButton()
else if (!viewModel.viewingFile.value) invalidateNavigationButton()
if (selecting) {
setupCloseButton()

setNavigationOnClickListener {
viewModel.selectedItems.update {
emptyList()
}
}
} else if (viewModel.viewingFile)
// For new activities with opened files
setupBackButtonForBackPressedDispatcher(requireActivity())

else invalidateNavigationButton()
}

private fun scrollLogToBottom() {
Expand Down Expand Up @@ -271,4 +239,18 @@ class LogsFragment: BaseViewModelFragment<LogsViewModel, FragmentLogsBinding>(),
.setPositiveButton(R.string.close, null)
.show()
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
viewModel.appPreferences.apply {
if (key == "pref_logs_text_size") adapter.textSize = logsTextSize.toFloat()
else if (key == "pref_logs_expanded") adapter.logsExpanded = logsExpanded

if (key?.startsWith("pref_show_log") == true) adapter.logsFormat = showLogValues
}
}

override fun onDestroy() {
super.onDestroy()
viewModel.appPreferences.unregisterListener(this)
}
}
42 changes: 10 additions & 32 deletions app/src/main/java/com/f0x1d/logfox/viewmodel/LogsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.f0x1d.logfox.database.entity.UserFilter
import com.f0x1d.logfox.di.viewmodel.FileUri
import com.f0x1d.logfox.extensions.logline.filterAndSearch
import com.f0x1d.logfox.extensions.readFileContentsAsFlow
import com.f0x1d.logfox.extensions.readFileName
import com.f0x1d.logfox.extensions.updateList
import com.f0x1d.logfox.model.LogLine
import com.f0x1d.logfox.repository.logging.LoggingRepository
Expand Down Expand Up @@ -37,38 +38,24 @@ class LogsViewModel @Inject constructor(

val paused = MutableStateFlow(false)

val viewingFile = MutableStateFlow(fileUri != null)
val viewingFile = fileUri != null
val viewingFileName = fileUri?.readFileName(ctx)

val selectedItems = MutableStateFlow(emptyList<LogLine>())

@Suppress("UNCHECKED_CAST")
val logs = combine(
loggingRepository.logsFlow,
fileUri.readFileContentsAsFlow(ctx, appPreferences),
viewingFile,
fileUri?.readFileContentsAsFlow(ctx, appPreferences) ?: loggingRepository.logsFlow,
database.userFilterDao().getAllAsFlow(),
query,
paused
) { values ->
val logs = values[0] as List<LogLine>
val fileLogs = values[1] as List<LogLine>
val viewingFile = values[2] as Boolean
val filters = values[3] as List<UserFilter>
val query = values[4] as String?
val paused = values[5] as Boolean

val resultLogs = when {
viewingFile -> fileLogs
else -> logs
}

LogsData(resultLogs, viewingFile, filters, query, paused)
if (!viewingFile) paused else MutableStateFlow(true)
) { logs, filters, query, paused ->
LogsData(logs, filters, query, paused)
}.scan(null as LogsData?) { accumulator, data ->
when {
!data.paused || data.viewingFile != accumulator?.viewingFile -> data
!data.paused || viewingFile -> data

data.query != accumulator.query -> data.copy(logs = accumulator.logs)
data.filters != accumulator.filters -> data.copy(logs = accumulator.logs)
data.query != accumulator?.query -> data.copy(logs = accumulator?.logs ?: emptyList())
data.filters != accumulator?.filters -> data.copy(logs = accumulator?.logs ?: emptyList())

else -> data.copy(logs = accumulator.logs, passing = false)
}
Expand All @@ -86,14 +73,6 @@ class LogsViewModel @Inject constructor(

val resumeLoggingWithBottomTouch get() = appPreferences.resumeLoggingWithBottomTouch

fun stopViewingFile() = viewingFile.apply {
selectedItems.update { emptyList() }

if (value) update {
false
}
}

fun selectLine(logLine: LogLine, selected: Boolean) = selectedItems.updateList {
if (selected) add(
logLine
Expand All @@ -119,7 +98,6 @@ class LogsViewModel @Inject constructor(

data class LogsData(
val logs: List<LogLine>,
val viewingFile: Boolean,
val filters: List<UserFilter>,
val query: String?,
val paused: Boolean,
Expand Down
7 changes: 1 addition & 6 deletions app/src/main/res/navigation/logs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@
<dialog
android:id="@+id/searchBottomSheet"
android:name="com.f0x1d.logfox.ui.dialog.SearchBottomSheet"
android:label="SearchBottomSheet" >
<argument
android:name="query"
app:argType="string"
app:nullable="true" />
</dialog>
android:label="SearchBottomSheet" />
<fragment
android:id="@+id/filtersFragment"
android:name="com.f0x1d.logfox.ui.fragment.filters.FiltersFragment"
Expand Down

0 comments on commit 815bfc5

Please sign in to comment.