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

Update Settings: Update sub-screens #5334

Merged
merged 18 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update StatusIndicator to take a Status enum instead of a boolean
We have one more state "Always On" which we need for a couple of sub screens.

It makes more sense now to add this as an emum and allow setting from XML

We can make a function that allows us to easily set on/off which we need in most cases that calls to the new setStatus function that takes a "Status"
  • Loading branch information
mikescamell committed Dec 12, 2024
commit 9740734532593e1deec417d154cf9ba1875c6dbd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import com.duckduckgo.common.ui.viewbinding.viewBinding
import com.duckduckgo.mobile.android.R
import com.duckduckgo.mobile.android.databinding.ViewStatusIndicatorBinding

class StatusIndicator @JvmOverloads constructor(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you rename this to StatusIndicatorView instead? Just to follow other conventions.

Expand All @@ -30,15 +31,56 @@ class StatusIndicator @JvmOverloads constructor(

private val binding: ViewStatusIndicatorBinding by viewBinding()

init {
context.obtainStyledAttributes(
attrs,
R.styleable.StatusIndicator,
0,
0,
).apply {

val status = Status.from(getInt(R.styleable.StatusIndicator_indicatorStatus, 0))
setStatus(status)

recycle()
}
}

fun setStatus(status: Status) {
when (status) {
Status.ALWAYS_ON -> {
binding.icon.isEnabled = true
binding.label.text = context.getString(R.string.alwaysOn)
}
Status.ON -> {
binding.icon.isEnabled = true
binding.label.text = context.getString(R.string.on)
}
Status.OFF -> {
binding.icon.isEnabled = false
binding.label.text = context.getString(R.string.off)
}
}
}

fun setStatus(isOn: Boolean) {
if (isOn) {
binding.icon.isEnabled = true
// TODO copy changes
binding.label.text = "On"
} else {
binding.icon.isEnabled = false
// TODO copy changes
binding.label.text = "Off"
setStatus(if (isOn) Status.ON else Status.OFF)
}

enum class Status {

ALWAYS_ON,
ON,
OFF;

companion object {

fun from(value: Int): Status = when (value) {
0 -> ALWAYS_ON
1 -> ON
2 -> OFF
else -> OFF
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import com.duckduckgo.common.ui.view.StatusIndicator
import com.duckduckgo.common.ui.view.StatusIndicator.Status
import com.duckduckgo.common.ui.view.gone
import com.duckduckgo.common.ui.view.show
import com.duckduckgo.common.ui.view.text.DaxTextView
Expand Down Expand Up @@ -66,8 +67,8 @@ class SettingsListItem @JvmOverloads constructor(

setPillVisible(getBoolean(R.styleable.SettingsListItem_showBetaPill, false))

val isOn = getBoolean(R.styleable.SettingsListItem_isOn, false)
statusIndicator.setStatus(isOn)
val indicatorStatus = Status.from(getInt(R.styleable.SettingsListItem_indicatorStatus, 0))
statusIndicator.setStatus(indicatorStatus)

recycle()
}
Expand All @@ -79,7 +80,7 @@ class SettingsListItem @JvmOverloads constructor(
}

fun setStatus(isOn: Boolean) {
statusIndicator.setStatus(isOn)
statusIndicator.setStatus(if (isOn) Status.ON else Status.OFF)
}

private fun setPillVisible(isVisible: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
<attr name="leadingIcon" />
<attr name="primaryText" />
<attr name="showBetaPill" />
<attr name="isOn" format="boolean" />
<attr name="indicatorStatus" />
</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2023 DuckDuckGo
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>
<attr name="indicatorStatus">
<enum name="alwaysOn" value="0" />
<enum name="on" value="1" />
<enum name="off" value="2" />
</attr>
<attr name="labelText" format="string" />

<declare-styleable name="StatusIndicator">
<attr name="indicatorStatus" />
<attr name="labelText" />
</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

<!-- smartling.entity_escaping = false -->
<!-- smartling.instruction_attributes = instruction -->
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Notify Me View -->
<string name="notifyme_button_label">Notify me</string>

<!--Status Indicator-->
<string name="alwaysOn" tools:ignore="MissingTranslation">Always On</string>
<string name="on" tools:ignore="MissingTranslation">On</string>
<string name="off" tools:ignore="MissingTranslation">Off</string>
</resources>