Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color segments depending on what type they are #4055

Merged
merged 12 commits into from
Jun 22, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.libretube.enums

enum class SbSkipOptions {
OFF,
VISIBLE,
MANUAL,
AUTOMATIC
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ object PlayerHelper {
}
}
seekTo(segmentEnd)
} else {
} else if (sponsorBlockConfig[segment.category] == SbSkipOptions.MANUAL) {
return segmentEnd
}
}
Expand Down
169 changes: 169 additions & 0 deletions app/src/main/java/com/github/libretube/ui/dialogs/ColorPickerDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package com.github.libretube.ui.dialogs

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.SeekBar
import android.widget.Toast
import com.github.libretube.R

class ColorPickerDialog(
context: Context,
private val initialColor: Int,
private val onColorSelectedListener: OnColorSelectedListener
) : Dialog(context), SeekBar.OnSeekBarChangeListener, View.OnClickListener {
general-a marked this conversation as resolved.
Show resolved Hide resolved

private lateinit var colorPreview: View
private lateinit var redSeekBar: SeekBar
private lateinit var greenSeekBar: SeekBar
private lateinit var blueSeekBar: SeekBar
private lateinit var alphaSeekBar: SeekBar
private lateinit var colorHexInput: EditText
private lateinit var okay: Button
private lateinit var cancel: Button
Bnyro marked this conversation as resolved.
Show resolved Hide resolved

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.dialog_color_picker)

// Initialize UI elements
colorPreview = findViewById(R.id.colorPreview)
redSeekBar = findViewById(R.id.redSeekBar)
greenSeekBar = findViewById(R.id.greenSeekBar)
blueSeekBar = findViewById(R.id.blueSeekBar)
alphaSeekBar = findViewById(R.id.alphaSeekBar)
colorHexInput = findViewById(R.id.colorHexInput)
okay = findViewById(R.id.okay)
cancel = findViewById(R.id.cancel)

Bnyro marked this conversation as resolved.
Show resolved Hide resolved
// Set initial color
setColor(initialColor)

redSeekBar.setOnSeekBarChangeListener(this)
greenSeekBar.setOnSeekBarChangeListener(this)
blueSeekBar.setOnSeekBarChangeListener(this)
alphaSeekBar.setOnSeekBarChangeListener(this)
okay.setOnClickListener(this)
cancel.setOnClickListener(this)


colorHexInput.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
Bnyro marked this conversation as resolved.
Show resolved Hide resolved

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
Bnyro marked this conversation as resolved.
Show resolved Hide resolved

var valid = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var valid = true
var isValid = true

var oldHex = ""
override fun afterTextChanged(s: Editable?) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please add a new line before that one

// Update color when text input changes
val hexColor = s.toString()
if (hexColor.length == 9 && oldHex != hexColor) {
valid = try {
oldHex = hexColor
val color = Color.parseColor(hexColor)
setColor(color, true)
true
} catch (e: IllegalArgumentException) {
if (valid) {
showInvalidColorMessage()
}
false
}
}
}
})
}

override fun onStart() {
super.onStart()
val dialogWidth = ViewGroup.LayoutParams.MATCH_PARENT
val dialogHeight = ViewGroup.LayoutParams.WRAP_CONTENT
window?.setLayout(dialogWidth, dialogHeight)
}

Bnyro marked this conversation as resolved.
Show resolved Hide resolved
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
// Update color preview when SeekBar progress changes
val color = getColor()
setColorPreview(color)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val color = getColor()
setColorPreview(color)
setColorPreview(getColor())

}

override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit


override fun onStopTrackingTouch(seekBar: SeekBar?) {
val newColorString = colorToString(getColor())

if (newColorString != colorHexInput.text.toString()) {
colorHexInput.setText(newColorString)
}
}


private fun showInvalidColorMessage() {
val invalidColorMessage = R.string.invalid_color
Toast.makeText(context, invalidColorMessage, Toast.LENGTH_SHORT).show()
Bnyro marked this conversation as resolved.
Show resolved Hide resolved
}


override fun onClick(v: View?) {
if (v?.id == R.id.okay) {
// Notify the selected color
val color = getColor()
onColorSelectedListener.onColorSelected(color)
Bnyro marked this conversation as resolved.
Show resolved Hide resolved
dismiss()
} else if (v?.id == R.id.cancel) {
dismiss()
}
}

private fun getColor(): Int {
// Get the color from the SeekBar progress values
val red = redSeekBar.progress
val green = greenSeekBar.progress
val blue = blueSeekBar.progress
val alpha = alphaSeekBar.progress
return Color.argb(alpha, red, green, blue)
}
Bnyro marked this conversation as resolved.
Show resolved Hide resolved

private fun setColor(color: Int, textUpdate: Boolean = false) {
// Set the SeekBar progress values based on the color
val red = Color.red(color)
val green = Color.green(color)
val blue = Color.blue(color)
val alpha = Color.alpha(color)
redSeekBar.progress = red
greenSeekBar.progress = green
blueSeekBar.progress = blue
alphaSeekBar.progress = alpha
Bnyro marked this conversation as resolved.
Show resolved Hide resolved

// Set the hex color input value
if (!textUpdate) {
colorHexInput.setText(colorToString(color))
}
colorPreview.setBackgroundColor(color)
}

private fun setColorPreview(color: Int) {
// Set the color preview
colorPreview.setBackgroundColor(color)
}

private fun colorToString(color: Int): String {
return String.format("#%08X", color)
}

interface OnColorSelectedListener {
Bnyro marked this conversation as resolved.
Show resolved Hide resolved
fun onColorSelected(color: Int)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
return
}

}

private fun playVideo() {
Expand Down
80 changes: 80 additions & 0 deletions app/src/main/java/com/github/libretube/ui/views/ColorPreference.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.github.libretube.ui.views

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import com.github.libretube.R
import com.github.libretube.ui.dialogs.ColorPickerDialog

class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) {
private lateinit var circleView: View
private var currentColor: Int? = null

init {
layoutResource = R.layout.color_preference
}

override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)

holder.itemView.findViewById<TextView>(android.R.id.title)?.text = getTitle()
circleView = holder.itemView.findViewById(R.id.circle)
updateColorView()

circleView.setOnClickListener {
showColorPickerDialog()
}
}

private fun setColor(color: Int) {
currentColor = color
persistInt(color)
updateColorView()
}


override fun onGetDefaultValue(ta: TypedArray, index: Int): Any {
return Color.parseColor(ta.getString(index))
}

override fun onSetInitialValue(defaultValue: Any?) {
currentColor = if (defaultValue is Int) {
getPersistedInt(defaultValue)
} else {
getPersistedInt(Color.WHITE)
}
}

private fun updateColorView() {
(if (currentColor is Int) currentColor else Color.WHITE)?.let {
circleView.setBackgroundColor(it)
}
}

private fun showColorPickerDialog() {
(if (currentColor is Int) currentColor else Color.BLACK)?.let {
val dialog = ColorPickerDialog(
context,
it,
object : ColorPickerDialog.OnColorSelectedListener {
override fun onColorSelected(color: Int) {
setColor(color)
}
})
Bnyro marked this conversation as resolved.
Show resolved Hide resolved

dialog.show()
}
}


override fun getTitle(): CharSequence? {
return "${super.getTitle()}:"
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class MarkableTimeBar(
val horizontalOffset = (parent as View).marginLeft
length = canvas.width - horizontalOffset * 2
val marginY = canvas.height / 2 - progressBarHeight / 2
val themeColor = ThemeHelper.getThemeColor(context, R.attr.colorOnSecondary,)

segments.forEach {
canvas.drawRect(
Expand All @@ -55,10 +56,11 @@ class MarkableTimeBar(
canvas.height - marginY,
),
Paint().apply {
color = ThemeHelper.getThemeColor(
context,
R.attr.colorOnSecondary,
)
color = if (PreferenceHelper.getBoolean("sb_enable_custom_colors", false)) {
PreferenceHelper.getInt(it.category + "_color", themeColor)
} else {
themeColor
}
},
)
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/circle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="#000" />

</shape>
36 changes: 36 additions & 0 deletions app/src/main/res/layout/color_preference.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingTop="10dp"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingBottom="10dp">


<TextView
android:id="@android:id/title"
style="?android:attr/preferenceStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:ellipsize="end"
android:fadingEdge="horizontal"
android:singleLine="true"
android:text="Title"
android:textAlignment="textEnd"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp"
android:textStyle="italic" />

<View
android:id="@+id/circle"
android:layout_width="20sp"
android:layout_height="20sp"
android:layout_alignParentEnd="true"
android:background="@drawable/circle"
android:layout_marginStart="16dp"/>

</LinearLayout>
Loading