Skip to content

Commit

Permalink
Minor refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
XamDR committed Jul 21, 2022
1 parent 914bab9 commit 7970f23
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.color.MaterialColors
import net.azurewebsites.noties.R
import net.azurewebsites.noties.databinding.DialogFragmentColorBinding
import net.azurewebsites.noties.ui.helpers.ColorAdapter
import net.azurewebsites.noties.ui.helpers.getIntArray
import net.azurewebsites.noties.ui.helpers.toColorInt
import net.azurewebsites.noties.ui.helpers.setBackgroundColor

class EditorColorFragment : BottomSheetDialogFragment() {

Expand Down Expand Up @@ -48,19 +47,14 @@ class EditorColorFragment : BottomSheetDialogFragment() {
super.onViewCreated(view, savedInstanceState)
binding.list.adapter = colorAdapter
colorAdapter.selectedPosition = colors.indexOf(viewModel.entity.color)
binding.list.smoothScrollToPosition(colorAdapter.selectedPosition)
}

@SuppressLint("NotifyDataSetChanged")
private fun setEditorBackgroundColor(position: Int) {
val selectedColor = colors[position]
viewModel.updateNote(viewModel.entity.copy(color = selectedColor))
if (selectedColor != null) {
binding.root.setBackgroundColor(selectedColor.toColorInt())
}
else {
val defaultColor = MaterialColors.getColor(binding.root, R.attr.colorSurface)
binding.root.setBackgroundColor(defaultColor)
}
binding.root.setBackgroundColor(selectedColor)
colorAdapter.apply {
selectedPosition = colors.indexOf(viewModel.entity.color)
notifyDataSetChanged() // I don't like this, but it works
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import com.google.android.material.color.MaterialColors
import com.google.android.material.transition.MaterialContainerTransform
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import net.azurewebsites.noties.R
import net.azurewebsites.noties.core.Todo
import net.azurewebsites.noties.core.ImageEntity
import net.azurewebsites.noties.core.Note
import net.azurewebsites.noties.core.Todo
import net.azurewebsites.noties.databinding.FragmentEditorBinding
import net.azurewebsites.noties.ui.editor.todos.DragDropCallback
import net.azurewebsites.noties.ui.editor.todos.TodoItemAdapter
Expand Down Expand Up @@ -235,15 +234,7 @@ class EditorFragment : Fragment(), AttachImagesListener, LinkClickedListener,

override fun showBottomSheetColorDialog() {
val colorDialog = EditorColorFragment().apply {
setOnColorSelectedListener { color ->
if (color != null) {
binding.root.setBackgroundColor(color.toColorInt())
}
else {
val defaultColor = MaterialColors.getColor(binding.root, R.attr.colorSurface)
binding.root.setBackgroundColor(defaultColor)
}
}
setOnColorSelectedListener { color -> binding.root.setBackgroundColor(color) }
}
showDialog(colorDialog, COLOR_DIALOG_TAG)
}
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/kotlin/net/azurewebsites/noties/ui/helpers/UiUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.google.android.material.behavior.SwipeDismissBehavior
import com.google.android.material.color.MaterialColors
import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar
import net.azurewebsites.noties.R

fun FragmentActivity.findNavController(@IdRes id: Int) =
(this.supportFragmentManager.findFragmentById(id) as NavHostFragment).navController
Expand Down Expand Up @@ -209,4 +210,18 @@ fun TextView.strikethrough(shouldStrike: Boolean) {
fun Int.toColorInt(): Int {
val hex = Integer.toHexString(this)
return Color.parseColor("#$hex")
}

/**
* Sets the background color for this view or uses the colorSurface value
* if the color provided is null.
*/
fun View.setBackgroundColor(@ColorInt color: Int?) {
if (color == null) {
val defaultColor = MaterialColors.getColor(this, R.attr.colorSurface)
this.setBackgroundColor(defaultColor)
}
else {
this.setBackgroundColor(color)
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@
<item>@color/brown_600</item>
<item>@color/gray_700</item>
</integer-array>

<integer-array name="colors_editor">
<item>@color/blue_600_alpha_80</item>
<item>@color/red_600_alpha_80</item>
<item>@color/pink_600_alpha_80</item>
<item>@color/purple_600_alpha_80</item>
<item>@color/teal_600_alpha_80</item>
<item>@color/green_600_alpha_80</item>
<item>@color/yellow_A200_alpha_80</item>
<item>@color/brown_600_alpha_80</item>
<item>@color/gray_700_alpha_80</item>
</integer-array>
</resources>

0 comments on commit 7970f23

Please sign in to comment.