Skip to content

Commit

Permalink
[feat]: refactored recordings screen to compose
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Nov 4, 2024
1 parent 5eae9b4 commit 7c79613
Show file tree
Hide file tree
Showing 27 changed files with 778 additions and 411 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.f0x1d.logfox.arch.presentation.ui.base.SimpleFragmentLifecycleOwner
import com.f0x1d.logfox.arch.presentation.ui.snackbar
import dev.chrisbanes.insetter.applyInsetter

abstract class BaseFragment<T : ViewBinding>: Fragment(), SimpleFragmentLifecycleOwner {
abstract class BaseFragment<T : ViewBinding> : Fragment(), SimpleFragmentLifecycleOwner {

private var mutableBinding: T? = null
protected val binding: T get() = mutableBinding!!
Expand Down
2 changes: 2 additions & 0 deletions core/database/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ android {
dependencies {
implementation(projects.core.arch)

compileOnly(libs.androidx.compose.runtime)

implementation(libs.androidx.room)
implementation(libs.androidx.room.runtime)
ksp(libs.androidx.room.compiler)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.f0x1d.logfox.database.entity

import androidx.compose.runtime.Immutable
import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Delete
Expand All @@ -12,6 +13,7 @@ import com.f0x1d.logfox.model.Identifiable
import kotlinx.coroutines.flow.Flow
import java.io.File

@Immutable
@Entity
data class LogRecording(
@ColumnInfo(name = "title") val title: String,
Expand Down
2 changes: 1 addition & 1 deletion core/navigation/src/main/res/navigation/recordings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<fragment
android:id="@+id/recordingsFragment"
android:name="com.f0x1d.logfox.feature.recordings.list.presentation.ui.fragment.RecordingsFragment"
android:name="com.f0x1d.logfox.feature.recordings.list.presentation.ui.RecordingsFragment"
android:label="RecordingsFragment" />
<action
android:id="@+id/action_recordingsFragment_to_recordingBottomSheet"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.f0x1d.logfox.ui.compose.component.button

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ProvideTextStyle
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.f0x1d.logfox.strings.Strings
import com.f0x1d.logfox.ui.Icons
import com.f0x1d.logfox.ui.compose.preview.DayNightPreview
import com.f0x1d.logfox.ui.compose.theme.LogFoxTheme

@Composable
fun VerticalButton(
icon: @Composable () -> Unit,
text: @Composable () -> Unit,
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
shape: Shape = CardDefaults.shape,
) {
Card(
modifier = modifier,
onClick = onClick,
enabled = enabled,
shape = shape,
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(20.dp),
verticalArrangement = Arrangement.spacedBy(6.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
icon()

ProvideTextStyle(
value = MaterialTheme.typography.bodyLarge.copy(
fontWeight = FontWeight.Medium,
),
) {
text()
}
}
}
}

@DayNightPreview
@Composable
private fun Preview() = LogFoxTheme {
VerticalButton(
icon = {
Icon(
painter = painterResource(Icons.ic_menu_overflow),
contentDescription = null,
)
},
text = {
Text(text = stringResource(Strings.root))
},
onClick = { },
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.f0x1d.logfox.ui.compose.component.placeholder

import androidx.annotation.DrawableRes
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ProvideTextStyle
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.f0x1d.logfox.strings.Strings
import com.f0x1d.logfox.ui.Icons
import com.f0x1d.logfox.ui.compose.preview.DayNightPreview
import com.f0x1d.logfox.ui.compose.theme.LogFoxTheme

@Composable
fun ListPlaceholder(
@DrawableRes iconResId: Int,
text: @Composable () -> Unit,
modifier: Modifier = Modifier,
) {
Column(
modifier = modifier.padding(horizontal = 10.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
Box(
modifier = Modifier
.size(120.dp)
.clip(CircleShape)
.background(MaterialTheme.colorScheme.surfaceContainerHighest)
.padding(26.dp),
) {
Icon(
modifier = Modifier.fillMaxSize(),
painter = painterResource(iconResId),
contentDescription = null,
)
}

ProvideTextStyle(MaterialTheme.typography.bodyLarge) {
text()
}
}
}

@DayNightPreview
@Composable
private fun Preview() = LogFoxTheme {
ListPlaceholder(
iconResId = Icons.ic_recording,
text = {
Text(text = stringResource(Strings.no_crashes))
},
)
}
37 changes: 37 additions & 0 deletions core/ui/src/main/kotlin/com/f0x1d/logfox/ui/view/EditTextExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.f0x1d.logfox.ui.view

import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText

class ExtendedTextWatcher(
val editText: EditText,
var enabled: Boolean = true,
private val doAfterTextChanged: (e: Editable?) -> Unit,
) : TextWatcher {

init {
editText.addTextChangedListener(this)
}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) = Unit

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) = Unit

override fun afterTextChanged(e: Editable?) {
if (enabled) {
doAfterTextChanged(e)
}
}

fun setText(text: String?) {
enabled = false
editText.setText(text)
enabled = true
}
}

fun EditText.applyExtendedTextWatcher(doAfterTextChanged: (e: Editable?) -> Unit): ExtendedTextWatcher = ExtendedTextWatcher(
editText = this,
doAfterTextChanged = doAfterTextChanged,
)
9 changes: 9 additions & 0 deletions core/ui/src/main/res/drawable/ic_menu_overflow.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M12,16A2,2 0 0,1 14,18A2,2 0 0,1 12,20A2,2 0 0,1 10,18A2,2 0 0,1 12,16M12,10A2,2 0 0,1 14,12A2,2 0 0,1 12,14A2,2 0 0,1 10,12A2,2 0 0,1 12,10M12,4A2,2 0 0,1 14,6A2,2 0 0,1 12,8A2,2 0 0,1 10,6A2,2 0 0,1 12,4Z" />
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class LogsViewModel @Inject constructor(
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
dateTimeFormatter: DateTimeFormatter,
application: Application,
): BaseViewModel<LogsState, LogsAction>(
) : BaseViewModel<LogsState, LogsAction>(
initialStateProvider = { LogsState() },
application = application,
), DateTimeFormatter by dateTimeFormatter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class RecordingDetailsViewModel @Inject constructor(
application = application,
), DateTimeFormatter by dateTimeFormatter {
var currentTitle: String? = null
private set

private val titleUpdateMutex = Mutex()

Expand Down Expand Up @@ -83,6 +84,8 @@ class RecordingDetailsViewModel @Inject constructor(

fun updateTitle(title: String) = launchCatching {
titleUpdateMutex.withLock {
currentTitle = title

currentState.recording?.let {
recordingsRepository.updateTitle(it, title)
}
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 androidx.activity.result.contract.ActivityResultContracts
import androidx.core.os.bundleOf
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.f0x1d.logfox.arch.asUri
Expand All @@ -15,11 +14,12 @@ import com.f0x1d.logfox.arch.shareFileIntent
import com.f0x1d.logfox.feature.recordings.details.databinding.SheetRecordingDetailsBinding
import com.f0x1d.logfox.feature.recordings.details.presentation.RecordingDetailsViewModel
import com.f0x1d.logfox.navigation.Directions
import com.f0x1d.logfox.ui.view.applyExtendedTextWatcher
import dagger.hilt.android.AndroidEntryPoint
import java.util.Date

@AndroidEntryPoint
class RecordingDetailsBottomSheetFragment: BaseBottomSheetFragment<SheetRecordingDetailsBinding>() {
class RecordingDetailsBottomSheetFragment : BaseBottomSheetFragment<SheetRecordingDetailsBinding>() {

private val viewModel by viewModels<RecordingDetailsViewModel>()

Expand Down Expand Up @@ -67,10 +67,12 @@ class RecordingDetailsBottomSheetFragment: BaseBottomSheetFragment<SheetRecordin
}
}

title.doAfterTextChanged { viewModel.updateTitle(it?.toString().orEmpty()) }
val textWatcher = title.applyExtendedTextWatcher {
viewModel.updateTitle(it?.toString().orEmpty())
}

viewModel.state.collectWithLifecycle { state ->
title.setText(viewModel.currentTitle.orEmpty())
textWatcher.setText(viewModel.currentTitle.orEmpty())

val logRecording = state.recording ?: return@collectWithLifecycle

Expand Down
2 changes: 1 addition & 1 deletion feature/recordings/list/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("logfox.android.feature")
id("logfox.android.feature.compose")
}

android.namespace = "com.f0x1d.logfox.feature.recordings.list"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.f0x1d.logfox.feature.recordings.list.presentation

import androidx.compose.runtime.Immutable
import com.f0x1d.logfox.database.entity.LogRecording
import com.f0x1d.logfox.feature.recordings.api.data.RecordingState

@Immutable
data class RecordingsState(
val recordings: List<LogRecording> = emptyList(),
val recordingState: RecordingState = RecordingState.IDLE,
Expand Down

This file was deleted.

Loading

0 comments on commit 7c79613

Please sign in to comment.