Skip to content

Commit

Permalink
android widget: add more and organized options for the bg color
Browse files Browse the repository at this point in the history
was kinda bored so I just made this

welp, yesterday was the christmas eve, so today I can wish again everyone an 🎄 happy and joyful Christmas 🎅 hoho
  • Loading branch information
Toni500github committed Dec 25, 2024
1 parent 371d59d commit b4e338a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import android.util.TypedValue
import android.view.View
import android.widget.CheckBox
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.RadioGroup
import android.widget.TextView
import androidx.core.graphics.toColorInt
import androidx.core.text.HtmlCompat
Expand All @@ -39,20 +41,16 @@ class customfetchConfigureActivity : Activity() {
private lateinit var argsHelp: TextView
private lateinit var showModulesList: CheckBox
private lateinit var disableWrapLinesCheck: CheckBox
private lateinit var useSystemBgColor: CheckBox
private lateinit var selectBgColor: RadioGroup
private lateinit var colorPickerView: ColorPickerView
private lateinit var colorPickerHex: EditText
private lateinit var colorPreview: View
private lateinit var brightnessSlideBar: BrightnessSlideBar
private lateinit var alphaSlideBar: AlphaSlideBar
private lateinit var customColorSelect: LinearLayout
private var onClickListener = View.OnClickListener {
val context = this@customfetchConfigureActivity

val typedValue = TypedValue()
context.theme.resolveAttribute(android.R.attr.colorBackground, typedValue, true)
if (useSystemBgColor.isChecked)
bgColor = typedValue.data

// When the button is clicked, store the string locally
saveConfigPrefs(
context,
Expand Down Expand Up @@ -94,12 +92,13 @@ class customfetchConfigureActivity : Activity() {
argsHelp = binding.argsHelp
showModulesList = binding.showModulesList
disableWrapLinesCheck = binding.disableWrapLines
useSystemBgColor = binding.useSystemBgColor
selectBgColor = binding.selectBgColor
colorPickerView = binding.bgColor
colorPreview = binding.colorPreview
colorPickerHex = binding.bgColorHex
brightnessSlideBar = binding.brightnessSlide
alphaSlideBar = binding.alphaSlideBar
customColorSelect = binding.customColorsSelect
binding.addButton.setOnClickListener(onClickListener)

// Find the widget id from the intent.
Expand All @@ -126,47 +125,67 @@ class customfetchConfigureActivity : Activity() {
else
argsHelp.text = customfetchRender.mainAndroid("customfetch --help")
}

disableWrapLinesCheck.setOnCheckedChangeListener { _, isChecked ->
disableLineWrap = isChecked
}

// disable scroll when interacting with the color picker
colorPickerView.setOnTouchListener { view, _ ->
view.parent.requestDisallowInterceptTouchEvent(true)
false // allow colorPickerView to handle the touch event
}
selectBgColor.setOnCheckedChangeListener { _, checkedId ->
when (checkedId) {
R.id.radio_system_bg_color -> {
customColorSelect.visibility = View.GONE
val typedValue = TypedValue()
this.theme.resolveAttribute(android.R.attr.colorBackground, typedValue, true)
bgColor = typedValue.data
}

// if modified edittext and it's valid, apply to the preview
// else if modified in the color picker, apply to the edittext
var hexColor = ""
colorPickerHex.addTextChangedListener (object : TextWatcher {
override fun afterTextChanged(s: Editable) {
val col = s.toString()
if (isValidHex(col)) {
colorPreview.setBackgroundColor(Color.parseColor(col))
colorPickerView.setInitialColor(col.toColorInt())
hexColor = col
bgColor = col.toColorInt()
R.id.radio_transparent_bg -> {
customColorSelect.visibility = View.GONE
bgColor = 0x00FFFFFF
}

R.id.radio_custom_colors -> {
customColorSelect.visibility = View.VISIBLE
// disable scroll when interacting with the color picker
colorPickerView.setOnTouchListener { view, _ ->
view.parent.requestDisallowInterceptTouchEvent(true)
false // allow colorPickerView to handle the touch event
}

// if modified edittext and it's valid, apply to the preview
// else if modified in the color picker, apply to the edittext
var hexColor = ""
colorPickerHex.addTextChangedListener (object : TextWatcher {
override fun afterTextChanged(s: Editable) {
val col = s.toString()
if (isValidHex(col)) {
colorPreview.setBackgroundColor(Color.parseColor(col))
colorPickerView.setInitialColor(col.toColorInt())
hexColor = col
bgColor = col.toColorInt()
}
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
})
var firstRun = true
colorPickerView.setColorListener(ColorEnvelopeListener { envelope, _ ->
if (firstRun)
hexColor = "#${envelope.hexCode}"

if (hexColor != "#${envelope.hexCode}") {
colorPickerHex.setText("#${envelope.hexCode}")
hexColor = "#${envelope.hexCode}"
}
colorPreview.setBackgroundColor(envelope.color)
bgColor = envelope.color
firstRun = false
})
colorPickerView.attachAlphaSlider(alphaSlideBar)
colorPickerView.attachBrightnessSlider(brightnessSlideBar)
}
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
})
var firstRun = true
colorPickerView.setColorListener(ColorEnvelopeListener { envelope, _ ->
if (firstRun)
hexColor = "#${envelope.hexCode}"

if (hexColor != "#${envelope.hexCode}") {
colorPickerHex.setText("#${envelope.hexCode}")
hexColor = "#${envelope.hexCode}"
}
colorPreview.setBackgroundColor(envelope.color)
bgColor = envelope.color
firstRun = false
})
colorPickerView.attachAlphaSlider(alphaSlideBar)
colorPickerView.attachBrightnessSlider(brightnessSlideBar)
}
}

private fun isValidHex(color: String): Boolean =
Expand Down
114 changes: 71 additions & 43 deletions android/app/src/main/res/layout/customfetch_configure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,57 +52,85 @@
android:labelFor="@id/bg_color_hex"
android:text="@string/background_color" />

<CheckBox
android:id="@+id/use_system_bg_color"
<RadioGroup
android:id="@+id/select_bg_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/use_system_theme_color"/>
android:layout_height="wrap_content" >

<EditText
android:id="@+id/bg_color_hex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hex Color Code (#AARRGGBB)"
android:inputType="text" />
<RadioButton
android:id="@+id/radio_system_bg_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/use_system_theme_color"
android:checked="true" />

<com.skydoves.colorpickerview.ColorPickerView
android:id="@+id/bg_color"
android:layout_width="match_parent"
android:layout_height="250dp"
app:initialColor="@color/cardview_dark_background" />
<RadioButton
android:id="@+id/radio_transparent_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/transparent_background" />

<!-- padding separator -->
<View
android:layout_width="wrap_content"
android:layout_height="20dp" />
<RadioButton
android:id="@+id/radio_custom_colors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/custom_color"/>

<com.skydoves.colorpickerview.sliders.AlphaSlideBar
android:id="@+id/alphaSlideBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:selector_AlphaSlideBar="@drawable/colorpickerview_wheel"
app:borderColor_AlphaSlideBar="@android:color/darker_gray"
app:borderSize_AlphaSlideBar="5"/>
</RadioGroup>

<!-- padding separator -->
<View
<LinearLayout
android:id="@+id/custom_colors_select"
android:layout_width="wrap_content"
android:layout_height="20dp" />

<com.skydoves.colorpickerview.sliders.BrightnessSlideBar
android:id="@+id/brightnessSlide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:borderColor_BrightnessSlider="@android:color/darker_gray"
app:borderSize_BrightnessSlider="5"
app:selector_BrightnessSlider="@drawable/colorpickerview_wheel" />

<View
android:id="@+id/colorPreview"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:background="#FFFFFF" />
android:orientation="vertical"
android:visibility="gone" >

<EditText
android:id="@+id/bg_color_hex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hex Color Code (#AARRGGBB)"
android:inputType="text" />

<com.skydoves.colorpickerview.ColorPickerView
android:id="@+id/bg_color"
android:layout_width="match_parent"
android:layout_height="250dp"
app:initialColor="@color/cardview_dark_background" />

<!-- padding separator -->
<View
android:layout_width="wrap_content"
android:layout_height="20dp" />

<com.skydoves.colorpickerview.sliders.AlphaSlideBar
android:id="@+id/alphaSlideBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:selector_AlphaSlideBar="@drawable/colorpickerview_wheel"
app:borderColor_AlphaSlideBar="@android:color/darker_gray"
app:borderSize_AlphaSlideBar="5"/>

<!-- padding separator -->
<View
android:layout_width="wrap_content"
android:layout_height="20dp" />

<com.skydoves.colorpickerview.sliders.BrightnessSlideBar
android:id="@+id/brightnessSlide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:borderColor_BrightnessSlider="@android:color/darker_gray"
app:borderSize_BrightnessSlider="5"
app:selector_BrightnessSlider="@drawable/colorpickerview_wheel" />

<View
android:id="@+id/colorPreview"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:background="#FFFFFF" />
</LinearLayout>

<Button
android:id="@+id/add_button"
Expand Down
4 changes: 3 additions & 1 deletion android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
<string name="modules_list">Display modules list</string>
<string name="disable_wrap_lines">Truncate text (may be unstable, use it only on phones)</string>
<string name="background_color">Background color</string>
<string name="use_system_theme_color">Use system theme color instead</string>
<string name="use_system_theme_color">System theme color</string>
<string name="transparent_background">Transparent background</string>
<string name="custom_color">Custom color</string>
</resources>

0 comments on commit b4e338a

Please sign in to comment.