diff --git a/core/src/main/java/com/example/util/simpletimetracker/core/adapter/statistics/StatisticsAdapterDelegate.kt b/core/src/main/java/com/example/util/simpletimetracker/core/adapter/statistics/StatisticsAdapterDelegate.kt index 9fa3de75..0cfc97ba 100644 --- a/core/src/main/java/com/example/util/simpletimetracker/core/adapter/statistics/StatisticsAdapterDelegate.kt +++ b/core/src/main/java/com/example/util/simpletimetracker/core/adapter/statistics/StatisticsAdapterDelegate.kt @@ -1,19 +1,11 @@ package com.example.util.simpletimetracker.core.adapter.statistics -import android.graphics.Color -import androidx.annotation.ColorInt import androidx.core.view.ViewCompat import com.example.util.simpletimetracker.core.R import com.example.util.simpletimetracker.core.adapter.createRecyclerAdapterDelegate import com.example.util.simpletimetracker.core.extension.setOnClick -import com.example.util.simpletimetracker.core.extension.visible import com.example.util.simpletimetracker.core.view.TransitionNames -import kotlinx.android.synthetic.main.item_statistics_layout.view.dividerStatisticsPercent -import kotlinx.android.synthetic.main.item_statistics_layout.view.ivStatisticsItemIcon -import kotlinx.android.synthetic.main.item_statistics_layout.view.layoutStatisticsItem -import kotlinx.android.synthetic.main.item_statistics_layout.view.tvStatisticsItemDuration -import kotlinx.android.synthetic.main.item_statistics_layout.view.tvStatisticsItemName -import kotlinx.android.synthetic.main.item_statistics_layout.view.tvStatisticsItemPercent +import kotlinx.android.synthetic.main.item_statistics_layout.view.* fun createStatisticsAdapterDelegate( addTransitionNames: Boolean = false, @@ -22,20 +14,20 @@ fun createStatisticsAdapterDelegate( R.layout.item_statistics_layout ) { itemView, item, _ -> - with(itemView) { + with(itemView.viewStatisticsItem) { item as StatisticsViewData val transitionName = TransitionNames.STATISTICS_DETAIL + item.id - layoutStatisticsItem.setCardBackgroundColor(item.color) - tvStatisticsItemName.text = item.name - tvStatisticsItemDuration.text = item.duration - tvStatisticsItemPercent.text = item.percent - normalizeLightness(item.color).let(dividerStatisticsPercent::setBackgroundColor) - if (item is StatisticsViewData.Activity && item.iconId != null) { - ivStatisticsItemIcon.visible = true - ivStatisticsItemIcon.itemIcon = item.iconId + itemColor = item.color + itemName = item.name + itemDuration = item.duration + itemPercent = item.percent + + if (item is StatisticsViewData.Activity) { + itemIconVisible = true + itemIcon = item.icon } else { - ivStatisticsItemIcon.visible = false + itemIconVisible = false } setOnClick { onItemClick(item, mapOf(this to transitionName)) } @@ -44,20 +36,3 @@ fun createStatisticsAdapterDelegate( } } } - -/** - * Lightens dark colors and darkens light colors. - */ -@ColorInt -private fun normalizeLightness(@ColorInt color: Int): Int { - val colorNormalization = 0.05f - return FloatArray(3).apply { - Color.colorToHSV(color, this) - // change value - if (this[2] > 0.5f) { - this[2] -= colorNormalization - } else { - this[2] += colorNormalization - } - }.let(Color::HSVToColor) -} diff --git a/core/src/main/java/com/example/util/simpletimetracker/core/adapter/statistics/StatisticsViewData.kt b/core/src/main/java/com/example/util/simpletimetracker/core/adapter/statistics/StatisticsViewData.kt index 237cccc0..76848376 100644 --- a/core/src/main/java/com/example/util/simpletimetracker/core/adapter/statistics/StatisticsViewData.kt +++ b/core/src/main/java/com/example/util/simpletimetracker/core/adapter/statistics/StatisticsViewData.kt @@ -21,7 +21,7 @@ sealed class StatisticsViewData : ViewHolderType { override val duration: String, override val percent: String, @ColorInt override val color: Int, - val iconId: RecordTypeIcon? + val icon: RecordTypeIcon ) : StatisticsViewData() data class Category( diff --git a/core/src/main/java/com/example/util/simpletimetracker/core/view/StatisticsView.kt b/core/src/main/java/com/example/util/simpletimetracker/core/view/StatisticsView.kt new file mode 100644 index 00000000..b847aa86 --- /dev/null +++ b/core/src/main/java/com/example/util/simpletimetracker/core/view/StatisticsView.kt @@ -0,0 +1,136 @@ +package com.example.util.simpletimetracker.core.view + +import android.content.Context +import android.graphics.Color +import android.util.AttributeSet +import android.view.View +import androidx.annotation.ColorInt +import androidx.cardview.widget.CardView +import androidx.core.content.ContextCompat +import com.example.util.simpletimetracker.core.R +import com.example.util.simpletimetracker.core.extension.visible +import com.example.util.simpletimetracker.core.viewData.RecordTypeIcon +import kotlinx.android.synthetic.main.statistics_view_layout.view.* + +class StatisticsView @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : CardView( + context, + attrs, + defStyleAttr +) { + + var itemName: String = "" + set(value) { + field = value + tvStatisticsItemName.text = itemName + } + + var itemColor: Int = Color.BLACK + set(value) { + setCardBackgroundColor(value) + setDividerColor() + field = value + } + + var itemIcon: RecordTypeIcon = RecordTypeIcon.Image(0) + set(value) { + ivStatisticsItemIcon.itemIcon = value + field = value + } + + var itemIconVisible: Boolean = false + set(value) { + ivStatisticsItemIcon.visible = value + field = value + } + + var itemDuration: String = "" + set(value) { + tvStatisticsItemDuration.text = value + field = value + } + + var itemPercent: String = "" + set(value) { + tvStatisticsItemPercent.text = value + setDividerColor() + field = value + } + + init { + View.inflate(context, R.layout.statistics_view_layout, this) + initProps() + initAttrs(context, attrs, defStyleAttr) + } + + private fun initProps() { + ContextCompat.getColor(context, R.color.black).let(::setCardBackgroundColor) + radius = resources.getDimensionPixelOffset(R.dimen.record_type_card_corner_radius).toFloat() + // TODO doesn't work here for some reason, need to set in the layout + cardElevation = resources.getDimensionPixelOffset(R.dimen.record_type_card_elevation).toFloat() + preventCornerOverlap = false + useCompatPadding = true + } + + private fun initAttrs( + context: Context, + attrs: AttributeSet?, + defStyleAttr: Int + ) { + context.obtainStyledAttributes(attrs, R.styleable.StatisticsView, defStyleAttr, 0) + .run { + if (hasValue(R.styleable.StatisticsView_itemName)) itemName = + getString(R.styleable.StatisticsView_itemName).orEmpty() + + if (hasValue(R.styleable.StatisticsView_itemColor)) itemColor = + getColor(R.styleable.StatisticsView_itemColor, Color.BLACK) + + if (hasValue(R.styleable.StatisticsView_itemIcon)) itemIcon = + getResourceId(R.styleable.StatisticsView_itemIcon, R.drawable.unknown) + .let(RecordTypeIcon::Image) + + if (hasValue(R.styleable.StatisticsView_itemEmoji)) itemIcon = + getString(R.styleable.StatisticsView_itemEmoji).orEmpty() + .let(RecordTypeIcon::Emoji) + + if (hasValue(R.styleable.StatisticsView_itemIconVisible)) itemIconVisible = + getBoolean(R.styleable.StatisticsView_itemIconVisible, false) + + if (hasValue(R.styleable.StatisticsView_itemDuration)) itemDuration = + getString(R.styleable.StatisticsView_itemDuration).orEmpty() + + if (hasValue(R.styleable.StatisticsView_itemPercent)) itemPercent = + getString(R.styleable.StatisticsView_itemPercent).orEmpty() + + recycle() + } + } + + private fun setDividerColor() { + if (itemPercent.isNotEmpty()) { + normalizeLightness(itemColor) + } else { + itemColor + }.let(dividerStatisticsPercent::setBackgroundColor) + } + + /** + * Lightens dark colors and darkens light colors. + */ + @ColorInt + private fun normalizeLightness(@ColorInt color: Int): Int { + val colorNormalization = 0.05f + return FloatArray(3).apply { + Color.colorToHSV(color, this) + // change value + if (this[2] > 0.5f) { + this[2] -= colorNormalization + } else { + this[2] += colorNormalization + } + }.let(Color::HSVToColor) + } +} \ No newline at end of file diff --git a/core/src/main/res/layout/item_statistics_layout.xml b/core/src/main/res/layout/item_statistics_layout.xml index bdb3c862..cc147e2e 100644 --- a/core/src/main/res/layout/item_statistics_layout.xml +++ b/core/src/main/res/layout/item_statistics_layout.xml @@ -1,97 +1,15 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file + app:cardElevation="@dimen/record_type_card_elevation" + tools:itemColor="@color/red_800" + tools:itemDuration="5h 23m 3s" + tools:itemIcon="@drawable/ic_sports_motorsports_24px" + tools:itemName="Statistics" + tools:itemPercent="13%" /> \ No newline at end of file diff --git a/core/src/main/res/layout/statistics_view_layout.xml b/core/src/main/res/layout/statistics_view_layout.xml new file mode 100644 index 00000000..39ae5f41 --- /dev/null +++ b/core/src/main/res/layout/statistics_view_layout.xml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core/src/main/res/values/attrs.xml b/core/src/main/res/values/attrs.xml index 9f954c2a..f4be33ad 100644 --- a/core/src/main/res/values/attrs.xml +++ b/core/src/main/res/values/attrs.xml @@ -53,6 +53,16 @@ + + + + + + + + + + @@ -73,7 +83,7 @@ - + diff --git a/feature_statistics/src/main/java/com/example/util/simpletimetracker/feature_statistics/mapper/StatisticsViewDataMapper.kt b/feature_statistics/src/main/java/com/example/util/simpletimetracker/feature_statistics/mapper/StatisticsViewDataMapper.kt index e2a97dd1..b8873f92 100644 --- a/feature_statistics/src/main/java/com/example/util/simpletimetracker/feature_statistics/mapper/StatisticsViewDataMapper.kt +++ b/feature_statistics/src/main/java/com/example/util/simpletimetracker/feature_statistics/mapper/StatisticsViewDataMapper.kt @@ -195,7 +195,7 @@ class StatisticsViewDataMapper @Inject constructor( duration = statistics.duration .let(timeMapper::formatInterval), percent = durationPercent, - iconId = RecordTypeIcon.Image(R.drawable.unknown), + icon = RecordTypeIcon.Image(R.drawable.unknown), color = colorMapper.toUntrackedColor(isDarkTheme) ) } @@ -209,7 +209,7 @@ class StatisticsViewDataMapper @Inject constructor( "" }, percent = durationPercent, - iconId = recordType.icon + icon = recordType.icon .let(iconMapper::mapIcon), color = recordType.color .let { colorMapper.mapToColorResId(it, isDarkTheme) } diff --git a/feature_statistics/src/main/java/com/example/util/simpletimetracker/feature_statistics/viewModel/StatisticsViewModel.kt b/feature_statistics/src/main/java/com/example/util/simpletimetracker/feature_statistics/viewModel/StatisticsViewModel.kt index 55b6ed7e..0f1f6795 100644 --- a/feature_statistics/src/main/java/com/example/util/simpletimetracker/feature_statistics/viewModel/StatisticsViewModel.kt +++ b/feature_statistics/src/main/java/com/example/util/simpletimetracker/feature_statistics/viewModel/StatisticsViewModel.kt @@ -72,7 +72,7 @@ class StatisticsViewModel @Inject constructor( ), preview = StatisticsDetailParams.Preview( name = item.name, - iconId = (item as? StatisticsViewData.Activity)?.iconId?.toParams(), + iconId = (item as? StatisticsViewData.Activity)?.icon?.toParams(), color = item.color ) ), diff --git a/feature_statistics_detail/src/main/java/com/example/util/simpletimetracker/feature_statistics_detail/mapper/StatisticsDetailViewDataMapper.kt b/feature_statistics_detail/src/main/java/com/example/util/simpletimetracker/feature_statistics_detail/mapper/StatisticsDetailViewDataMapper.kt index 018537b9..3c5f9133 100644 --- a/feature_statistics_detail/src/main/java/com/example/util/simpletimetracker/feature_statistics_detail/mapper/StatisticsDetailViewDataMapper.kt +++ b/feature_statistics_detail/src/main/java/com/example/util/simpletimetracker/feature_statistics_detail/mapper/StatisticsDetailViewDataMapper.kt @@ -13,6 +13,7 @@ import com.example.util.simpletimetracker.domain.extension.rotateLeft import com.example.util.simpletimetracker.domain.mapper.StatisticsMapper import com.example.util.simpletimetracker.domain.model.Category import com.example.util.simpletimetracker.domain.model.DayOfWeek +import com.example.util.simpletimetracker.domain.model.IconType import com.example.util.simpletimetracker.domain.model.RangeLength import com.example.util.simpletimetracker.domain.model.Record import com.example.util.simpletimetracker.domain.model.RecordTag @@ -472,8 +473,9 @@ class StatisticsDetailViewDataMapper @Inject constructor( duration = duration .let(timeMapper::formatInterval), percent = durationPercent, - iconId = recordType?.icon - ?.let(iconMapper::mapIcon), + icon = recordType?.icon + ?.let(iconMapper::mapIcon) + ?: RecordTypeIcon.Image(R.drawable.unknown), color = recordType?.color ?.let { colorMapper.mapToColorResId(it, isDarkTheme) } ?.let(resourceRepo::getColor) diff --git a/feature_statistics_detail/src/main/java/com/example/util/simpletimetracker/feature_statistics_detail/view/StatisticsDetailFragment.kt b/feature_statistics_detail/src/main/java/com/example/util/simpletimetracker/feature_statistics_detail/view/StatisticsDetailFragment.kt index a37148b5..5792fdd7 100644 --- a/feature_statistics_detail/src/main/java/com/example/util/simpletimetracker/feature_statistics_detail/view/StatisticsDetailFragment.kt +++ b/feature_statistics_detail/src/main/java/com/example/util/simpletimetracker/feature_statistics_detail/view/StatisticsDetailFragment.kt @@ -78,7 +78,7 @@ class StatisticsDetailFragment : BaseFragment(R.layout.statistics_detail_fragmen .inflateTransition(android.R.transition.move) } - ViewCompat.setTransitionName(layoutStatisticsDetailItem, params.transitionName) + ViewCompat.setTransitionName(viewStatisticsDetailItem, params.transitionName) rvStatisticsDetailPreviewItems.apply { layoutManager = FlexboxLayoutManager(requireContext()).apply { @@ -154,15 +154,15 @@ class StatisticsDetailFragment : BaseFragment(R.layout.statistics_detail_fragmen ?: return val rest = viewData.drop(1) - tvStatisticsDetailItemName.text = first.name - layoutStatisticsDetailItem.setCardBackgroundColor(first.color) + viewStatisticsDetailItem.itemName = first.name + viewStatisticsDetailItem.itemColor = first.color chartStatisticsDetail.setBarColor(first.color) chartStatisticsDetailSplit.setBarColor(first.color) if (first.iconId != null) { - ivStatisticsDetailItemIcon.visible = true - ivStatisticsDetailItemIcon.itemIcon = first.iconId + viewStatisticsDetailItem.itemIconVisible = true + viewStatisticsDetailItem.itemIcon = first.iconId } else { - ivStatisticsDetailItemIcon.visible = false + viewStatisticsDetailItem.itemIconVisible = false } previewAdapter.replace(rest) diff --git a/feature_statistics_detail/src/main/res/layout/statistics_detail_fragment.xml b/feature_statistics_detail/src/main/res/layout/statistics_detail_fragment.xml index 27696cac..9022d720 100644 --- a/feature_statistics_detail/src/main/res/layout/statistics_detail_fragment.xml +++ b/feature_statistics_detail/src/main/res/layout/statistics_detail_fragment.xml @@ -18,55 +18,19 @@ android:descendantFocusability="blocksDescendants" android:paddingBottom="16dp"> - - - - - - - - - - - + tools:itemColor="@color/red_800" /> + app:layout_constraintTop_toTopOf="@id/viewStatisticsDetailItem">