Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Jan 5, 2025
1 parent ebf725c commit f588e3e
Show file tree
Hide file tree
Showing 72 changed files with 360 additions and 443 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import androidx.viewpager2.widget.ViewPager2
import com.example.util.simpletimetracker.core.utils.getLifecycleObserverAdapter
import com.example.util.simpletimetracker.domain.base.Coordinates
import java.util.Calendar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

inline fun <T, R> T.allowDiskWrite(block: T.() -> R): R {
Expand Down Expand Up @@ -87,14 +85,12 @@ fun Calendar.shiftTimeStamp(timestamp: Long, shift: Long): Long {
return timeInMillis
}

@OptIn(DelicateCoroutinesApi::class)
fun BroadcastReceiver.goAsync(
coroutineScope: CoroutineScope = GlobalScope,
finally: () -> Unit,
block: suspend () -> Unit,
) {
val result = goAsync()
coroutineScope.launch {
allowDiskRead { MainScope() }.launch {
try {
block()
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ColorMapper @Inject constructor(
}
}

// TODO use resourceRepo getThemedAttr, also others
fun toUntrackedColor(isDarkTheme: Boolean): Int {
return if (isDarkTheme) {
R.color.colorUntrackedDark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,19 @@ import com.example.util.simpletimetracker.domain.complexRule.model.ComplexRule
import com.example.util.simpletimetracker.domain.recordTag.model.RecordTag
import com.example.util.simpletimetracker.domain.recordType.model.RecordType
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.complexRule.ComplexRuleAddViewData
import com.example.util.simpletimetracker.feature_base_adapter.complexRule.ComplexRuleElementTitleViewData
import com.example.util.simpletimetracker.feature_base_adapter.complexRule.ComplexRuleViewData
import com.example.util.simpletimetracker.feature_base_adapter.listElement.ListElementViewData
import javax.inject.Inject

class ComplexRulesViewDataMapper @Inject constructor(
class ComplexRuleViewDataMapper @Inject constructor(
private val timeMapper: TimeMapper,
private val iconMapper: IconMapper,
private val colorMapper: ColorMapper,
private val resourceRepo: ResourceRepo,
private val recordTagViewDataMapper: RecordTagViewDataMapper,
) {

fun mapAddItem(
isDarkTheme: Boolean,
): ComplexRuleAddViewData {
return ComplexRuleAddViewData(
name = resourceRepo.getString(R.string.running_records_add_type),
color = colorMapper.toInactiveColor(isDarkTheme),
)
}

fun mapRule(
rule: ComplexRule,
isDarkTheme: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.example.util.simpletimetracker.core.view.buttonsRowView

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import com.example.util.simpletimetracker.core.R
import com.example.util.simpletimetracker.feature_base_adapter.BaseRecyclerAdapter
import com.example.util.simpletimetracker.core.databinding.ButtonsRowViewLayoutBinding
import com.example.util.simpletimetracker.feature_views.extension.layoutInflater
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
Expand All @@ -31,8 +31,7 @@ class ButtonsRowView @JvmOverloads constructor(
)
}

private val binding: ButtonsRowViewLayoutBinding = ButtonsRowViewLayoutBinding
.inflate(LayoutInflater.from(context), this)
private val binding = ButtonsRowViewLayoutBinding.inflate(layoutInflater, this)

private val selectedColor by lazy {
var defaultColor = ContextCompat.getColor(context, R.color.colorPrimary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.example.util.simpletimetracker.core.view.timeAdjustment

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.view.updatePadding
import com.example.util.simpletimetracker.core.databinding.TimeAdjustmentItemLayoutBinding
Expand All @@ -11,6 +10,7 @@ import com.example.util.simpletimetracker.feature_base_adapter.BaseRecyclerAdapt
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.createRecyclerBindingAdapterDelegate
import com.example.util.simpletimetracker.feature_views.extension.dpToPx
import com.example.util.simpletimetracker.feature_views.extension.layoutInflater
import com.example.util.simpletimetracker.feature_views.extension.setOnClickWith
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
Expand All @@ -35,8 +35,7 @@ class TimeAdjustmentView @JvmOverloads constructor(
)
}

private val binding: TimeAdjustmentViewLayoutBinding = TimeAdjustmentViewLayoutBinding
.inflate(LayoutInflater.from(context), this)
private val binding = TimeAdjustmentViewLayoutBinding.inflate(layoutInflater, this)

init {
initRecycler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.example.util.simpletimetracker.feature_views.extension.layoutInflater

interface RecyclerAdapterDelegate {
fun isForValidType(check: ViewHolderType): Boolean
Expand All @@ -30,7 +31,7 @@ inline fun <reified T : ViewHolderType, B : ViewBinding> createRecyclerBindingAd

override fun onCreateViewHolder(parent: ViewGroup): BaseRecyclerBindingViewHolder<B> =
BaseRecyclerBindingViewHolder(
binding = inflater(LayoutInflater.from(parent.context), parent, false),
binding = inflater(parent.layoutInflater, parent, false),
onBind = onBind,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.example.util.simpletimetracker.feature_base_adapter.button

import android.content.res.ColorStateList
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.core.view.isVisible
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.createRecyclerBindingAdapterDelegate
import com.example.util.simpletimetracker.feature_views.extension.setMargins
import com.example.util.simpletimetracker.feature_views.extension.setOnClickWith
import com.example.util.simpletimetracker.feature_base_adapter.button.ButtonViewData as ViewData
import com.example.util.simpletimetracker.feature_base_adapter.databinding.ItemButtonLayoutBinding as Binding

// TODO remove ripple from icon background if background is transparent.
// TODO SUG add backup tests
// TODO SUG add reorder
// TODO GOAL add backup tests, raise test file version
fun createButtonAdapterDelegate(
onClick: (ViewData) -> Unit,
) = createRecyclerBindingAdapterDelegate<ViewData, Binding>(
Binding::inflate,
) { binding, item, _ ->

with(binding) {
item as ViewData

root.setMargins(
start = item.marginHorizontalDp,
end = item.marginHorizontalDp,
)
tvButton.text = item.text
when (item.icon) {
is ViewData.Icon.Hidden -> {
cardButton.isVisible = false
}
is ViewData.Icon.Present -> {
cardButton.isVisible = true
ivButton.setImageResource(item.icon.icon)
ivButton.imageTintList = ColorStateList.valueOf(item.icon.iconColor)
cardButton.setCardBackgroundColor(item.icon.iconBackgroundColor)
}
}
root.setCardBackgroundColor(item.backgroundColor)
itemButton.isEnabled = item.isEnabled
itemButton.setOnClickWith(item, onClick)
}
}

data class ButtonViewData(
val id: Id,
val text: String,
val icon: Icon,
@ColorInt val backgroundColor: Int,
val isEnabled: Boolean,
val marginHorizontalDp: Int,
) : ViewHolderType {

override fun getUniqueId(): Long = id.hashCode().toLong()

override fun isValidType(other: ViewHolderType): Boolean =
other is ViewData

interface Id

sealed interface Icon {
data object Hidden : Icon
data class Present(
@DrawableRes val icon: Int,
@ColorInt val iconColor: Int,
@ColorInt val iconBackgroundColor: Int,
) : Icon
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.example.util.simpletimetracker.feature_base_adapter.multitaskRecord.
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.cardview.widget.CardView
import com.example.util.simpletimetracker.feature_base_adapter.BaseRecyclerAdapter
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
Expand All @@ -12,6 +11,7 @@ import com.example.util.simpletimetracker.feature_base_adapter.databinding.ItemM
import com.example.util.simpletimetracker.feature_base_adapter.databinding.MultitaskRecordViewLayoutBinding
import com.example.util.simpletimetracker.feature_views.extension.getThemedAttr
import com.example.util.simpletimetracker.feature_views.R
import com.example.util.simpletimetracker.feature_views.extension.layoutInflater
import com.example.util.simpletimetracker.feature_views.viewData.RecordTypeIcon

class MultitaskRecordView @JvmOverloads constructor(
Expand All @@ -27,8 +27,7 @@ class MultitaskRecordView @JvmOverloads constructor(
private val adapter: BaseRecyclerAdapter by lazy {
BaseRecyclerAdapter(createAdapterDelegate())
}
private val binding: MultitaskRecordViewLayoutBinding = MultitaskRecordViewLayoutBinding
.inflate(LayoutInflater.from(context), this)
private val binding = MultitaskRecordViewLayoutBinding.inflate(layoutInflater, this)

init {
initProps()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.example.util.simpletimetracker.feature_base_adapter.recordTypeSuggestion

import android.view.LayoutInflater
import android.view.ViewGroup
import com.example.util.simpletimetracker.feature_base_adapter.BaseRecyclerBindingViewHolder
import com.example.util.simpletimetracker.feature_base_adapter.RecyclerAdapterDelegate
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.recordType.createRecordTypeAdapterDelegate
import com.example.util.simpletimetracker.feature_views.TransitionNames
import com.example.util.simpletimetracker.feature_views.extension.layoutInflater
import com.example.util.simpletimetracker.feature_base_adapter.databinding.ItemRecordTypeLayoutBinding as BaseBinding
import com.example.util.simpletimetracker.feature_base_adapter.recordType.RecordTypeViewData as BaseViewData
import com.example.util.simpletimetracker.feature_base_adapter.recordTypeSuggestion.RecordTypeSuggestionViewData as ViewData
Expand All @@ -33,11 +33,10 @@ fun createRecordTypeSuggestionAdapterDelegate(
override fun onCreateViewHolder(parent: ViewGroup): BaseRecyclerBindingViewHolder<BaseBinding> {
val baseViewHolder = baseAdapter.onCreateViewHolder(parent)
as? BaseRecyclerBindingViewHolder<*>
// TODO SUG add extension for layoutInflater
// Just in case, so it wouldn't crash.
// Worst case - it would show unbound layout.
val fallbackBinding by lazy {
BaseBinding.inflate(LayoutInflater.from(parent.context), parent, false)
BaseBinding.inflate(parent.layoutInflater, parent, false)
}

return BaseRecyclerBindingViewHolder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,46 @@
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/itemActivitySuggestionsButton"
android:id="@+id/itemButton"
style="@style/EditScreenControlButton"
android:layout_width="match_parent"
android:layout_height="@dimen/button_height"
app:cardBackgroundColor="?appInactiveColor">
tools:cardBackgroundColor="?appActiveColor"
tools:layout_marginHorizontal="4dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.cardview.widget.CardView
android:id="@+id/cardActivitySuggestionsButton"
android:id="@+id/cardButton"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:foreground="?selectableItemBackground"
app:cardCornerRadius="8dp"
app:cardElevation="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/tvActivitySuggestionsButton"
app:layout_constraintEnd_toStartOf="@id/tvButton"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:backgroundTint="@color/purple_200">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ivActivitySuggestionsButton"
android:id="@+id/ivButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:scaleType="centerCrop"
android:tint="?appCardBackgroundColor"
tools:ignore="ContentDescription"
tools:src="@drawable/action_continue" />
tools:src="@drawable/app_unknown"
tools:tint="?appCardBackgroundColor" />

</androidx.cardview.widget.CardView>

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvActivitySuggestionsButton"
android:id="@+id/tvButton"
style="@style/EditScreenControlButtonText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
Expand All @@ -50,8 +51,8 @@
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/cardActivitySuggestionsButton"
tools:text="Button" />
app:layout_constraintStart_toEndOf="@id/cardButton"
tools:text="Button text" />

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down
Loading

0 comments on commit f588e3e

Please sign in to comment.