Skip to content

Commit

Permalink
Add multiple accent colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Abrynos authored Mar 17, 2022
2 parents 2a61fef + d860a7e commit 4ac3b01
Show file tree
Hide file tree
Showing 35 changed files with 189 additions and 69 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {

defaultConfig {
applicationId "pl.edu.pjwstk.s999844.shoppinglist"
minSdkVersion 24
minSdkVersion 25
targetSdkVersion 32
versionCode 13
versionName "v1.12.0"
Expand All @@ -54,7 +54,7 @@ android {
}
signingConfigs {
release {
if(keystorePropertiesFile.exists()) {
if (keystorePropertiesFile.exists()) {
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

Expand Down
5 changes: 1 addition & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/appName"
android:logo="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ShoppingList">
<activity
Expand All @@ -47,8 +45,7 @@
android:parentActivityName=".MainActivity" />
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.ShoppingList.MainActivity">
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,25 @@

package pl.edu.pjwstk.s999844.shoppinglist

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import pl.edu.pjwstk.s999844.shoppinglist.settings.Settings

abstract class AbstractShoppingActivity : AppCompatActivity() {
protected val settings: Settings by lazy { Settings(this) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setTheme(settings.accentColor.styleResourceId)
}

override fun onStart() {
super.onStart()

ThemeManager.setDark(settings.darkThemeActive)
setDark(settings.darkThemeActive)
}

protected fun setDark(dark: Boolean) = AppCompatDelegate.setDefaultNightMode(if (dark) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO)
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ class MainActivity : AbstractShoppingActivity() {
mainListRecyclerView.adapter = ShoppingListAdapter(this::changeItemCallback)
}

override fun onStart() {
super.onStart()

supportActionBar?.setDisplayShowHomeEnabled(true)
supportActionBar?.setDisplayUseLogoEnabled(true)
}


override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.Spinner
import kotlinx.android.synthetic.main.activity_options.*
import pl.edu.pjwstk.s999844.shoppinglist.adapters.OrderSpinnerAdapter
import pl.edu.pjwstk.s999844.shoppinglist.adapters.DescriptiveSettingSpinnerAdapter
import pl.edu.pjwstk.s999844.shoppinglist.adapters.SpinnerItemSelectedListener
import pl.edu.pjwstk.s999844.shoppinglist.settings.Settings

class OptionsActivity : AbstractShoppingActivity() {
Expand All @@ -51,21 +51,30 @@ class OptionsActivity : AbstractShoppingActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_options)

listOrderDropdown.adapter = OrderSpinnerAdapter(this)
listOrderDropdown.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, position: Int, id: Long) {
settings.order = listOrderDropdown.getItemAtPosition(position) as Settings.Order
listOrderDropdown.adapter = DescriptiveSettingSpinnerAdapter(this, Settings.Order.values())
listOrderDropdown.onItemSelectedListener = SpinnerItemSelectedListener<Settings.Order>(listOrderDropdown) {
if (settings.order == it) {
return@SpinnerItemSelectedListener
}

override fun onNothingSelected(p0: AdapterView<*>?) {
throw IllegalStateException()
settings.order = it
}

accentColorDropdown.adapter = DescriptiveSettingSpinnerAdapter(this, Settings.AccentColor.values())
accentColorDropdown.onItemSelectedListener = SpinnerItemSelectedListener<Settings.AccentColor>(accentColorDropdown) {
if (settings.accentColor == it) {
return@SpinnerItemSelectedListener
}

settings.accentColor = it
recreate()
}
}

private fun getIndex(spinner: Spinner, order: Settings.Order): Int {
private fun <T> getIndex(spinner: Spinner, order: T): Int {
for (i in 0..spinner.count) {
val valueAtPosition: Settings.Order = spinner.getItemAtPosition(i) as Settings.Order
@Suppress("UNCHECKED_CAST")
val valueAtPosition: T = spinner.getItemAtPosition(i) as T
if (valueAtPosition == order) {
return i
}
Expand All @@ -79,6 +88,7 @@ class OptionsActivity : AbstractShoppingActivity() {

optionsThemeSwitch.isChecked = settings.darkThemeActive
listOrderDropdown.setSelection(getIndex(listOrderDropdown, settings.order))
accentColorDropdown.setSelection(getIndex(accentColorDropdown, settings.accentColor))

title = getString(R.string.optionsTitleBarText)
}
Expand All @@ -87,7 +97,7 @@ class OptionsActivity : AbstractShoppingActivity() {
fun onClickThemeSwitch(view: View) {
val isDark = optionsThemeSwitch.isChecked
settings.darkThemeActive = isDark
ThemeManager.setDark(isDark)
setDark(isDark)
}

@Suppress("UNUSED_PARAMETER")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import android.widget.TextView
import pl.edu.pjwstk.s999844.shoppinglist.R
import pl.edu.pjwstk.s999844.shoppinglist.settings.Settings

class OrderSpinnerAdapter(context: Context) : ArrayAdapter<Settings.Order>(context, R.layout.spinner_item, Settings.Order.values()) {
class DescriptiveSettingSpinnerAdapter<T>(context: Context, values: Array<T>) : ArrayAdapter<T>(context, R.layout.spinner_item, values) where T : Settings.DescriptiveSetting {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val text: TextView = (convertView ?: LayoutInflater.from(context).inflate(R.layout.spinner_item, parent, false)) as TextView

Expand All @@ -46,4 +46,3 @@ class OrderSpinnerAdapter(context: Context) : ArrayAdapter<Settings.Order>(conte

override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View = getView(position, convertView, parent)
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package pl.edu.pjwstk.s999844.shoppinglist
package pl.edu.pjwstk.s999844.shoppinglist.adapters

import androidx.appcompat.app.AppCompatDelegate
import android.view.View
import android.widget.AdapterView
import android.widget.Spinner

object ThemeManager {
fun setDark(dark: Boolean) = AppCompatDelegate.setDefaultNightMode(if (dark) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO)

class SpinnerItemSelectedListener<T>(private val spinner: Spinner, private val callback: ((t: T) -> Unit)) : AdapterView.OnItemSelectedListener {
override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, position: Int, id: Long) {
@Suppress("UNCHECKED_CAST")
callback(spinner.getItemAtPosition(position) as T)
}

override fun onNothingSelected(p0: AdapterView<*>?) {
throw IllegalStateException()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,27 @@ class Settings(context: Context) {
companion object {
private const val IS_DARK_THEME_NAME = "darkTheme"
private const val IS_DARK_THEME_DEFAULT = true

private const val ORDER_NAME = "listOrder"
private val ORDER_DEFAULT: Order = Order.Unordered

private const val ACCENT_NAME = "accentColor"
private val ACCENT_DEFAULT: AccentColor = AccentColor.Blue
}

private val sharedPreferences: SharedPreferences = context.getSharedPreferences("SETTINGS", MODE_PRIVATE)

private fun edit(): SharedPreferences.Editor = sharedPreferences.edit()

var accentColor: AccentColor
get() {
val value: Int = sharedPreferences.getInt(ACCENT_NAME, ACCENT_DEFAULT.value)
return AccentColor.values().firstOrNull {
it.value == value
} ?: ACCENT_DEFAULT
}
set(value) = edit().putInt(ACCENT_NAME, value.value).apply()

var darkThemeActive: Boolean
get() = sharedPreferences.getBoolean(IS_DARK_THEME_NAME, IS_DARK_THEME_DEFAULT)
set(value) = edit().putBoolean(IS_DARK_THEME_NAME, value).apply()
Expand All @@ -52,15 +65,33 @@ class Settings(context: Context) {
val value: Int = sharedPreferences.getInt(ORDER_NAME, ORDER_DEFAULT.value)
return Order.values().firstOrNull {
it.value == value
}?: ORDER_DEFAULT
} ?: ORDER_DEFAULT
}
set(value) = edit().putInt(ORDER_NAME, value.value).apply()

interface DescriptiveSetting {
val descriptionResourceId: Int
}

enum class AccentColor(val value: Int, val styleResourceId: Int, private val description: Int) : DescriptiveSetting {
Blue(0, R.style.Theme_ShoppingList_AccentBlue, R.string.optionsAccentBlue),
Red(1, R.style.Theme_ShoppingList_AccentRed, R.string.optionsAccentRed),
Orange(2, R.style.Theme_ShoppingList_AccentOrange, R.string.optionsAccentOrange),
Teal(3, R.style.Theme_ShoppingList_AccentTeal, R.string.optionsAccentTeal),
Purple(4, R.style.Theme_ShoppingList_AccentPurple, R.string.optionsAccentPurple),
Green(5, R.style.Theme_ShoppingList_AccentGreen, R.string.optionsAccentGreen);

enum class Order(val value: Int, val descriptionResourceId: Int) {
override val descriptionResourceId: Int
get() = description
}

enum class Order(val value: Int, private val description: Int) : DescriptiveSetting {
Unordered(0, R.string.optionsUnorderedOrdering),
Alphabetical(1, R.string.optionsAlphabeticalOrdering),
AmountAscending(2, R.string.optionsAmountAscendingOrdering),
AmountDescending(3, R.string.optionsAmountDecendingOrdering)
AmountDescending(3, R.string.optionsAmountDecendingOrdering);

override val descriptionResourceId: Int
get() = description
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_launcher_background.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="@color/primary"
android:fillColor="@color/blue"
android:pathData="M0,0h108v108h-108z" />
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_launcher_foreground.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</aapt:attr>
</path>
<path
android:fillColor="@color/onPrimary"
android:fillColor="@color/white"
android:fillType="nonZero"
android:pathData="M21.7533,32.9866c0,1.2413 -1.0054,2.2466 -2.2466,2.2466c-1.2413,0 -2.2354,-1.0054 -2.2354,-2.2466c0,-1.2413 0.9941,-2.2466 2.2354,-2.2466c1.2413,0 2.2466,1.0054 2.2466,2.2466zM16.4344,12.7668l1.0672,2.2466h16.6084c0.6178,0 1.1233,0.4999 1.1233,1.1233c0,0.1966 -0.0505,0.3819 -0.1404,0.5392l-4.0159,7.2904c-0.3875,0.6909 -1.1233,1.157 -1.9658,1.157h-8.3688l-1.0054,1.831c-0.0225,0.0393 -0.0337,0.0843 -0.0337,0.1348c0,0.1573 0.1236,0.2808 0.2808,0.2808h13.0025v2.2466h-13.4799c-1.2413,0 -2.2466,-1.0054 -2.2466,-2.2466c0,-0.3932 0.1011,-0.7582 0.2752,-1.084l1.5165,-2.7522l-4.0384,-8.5204h-2.2466v-2.2466zM32.9866,32.9866c0,1.2413 -1.0054,2.2466 -2.2466,2.2466c-1.2413,0 -2.2354,-1.0054 -2.2354,-2.2466c0,-1.2413 0.9941,-2.2466 2.2354,-2.2466c1.2413,0 2.2466,1.0054 2.2466,2.2466z"
android:strokeWidth="0"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_minus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
android:viewportHeight="512"
tools:ignore="VectorRaster">
<path
android:fillColor="@color/primary"
android:fillColor="?attr/primary"
android:pathData="M416,208H32c-17.67,0 -32,14.33 -32,32v32c0,17.67 14.33,32 32,32h384c17.67,0 32,-14.33 32,-32v-32c0,-17.67 -14.33,-32 -32,-32z" />
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_plus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
android:viewportHeight="512"
tools:ignore="VectorRaster">
<path
android:fillColor="@color/primary"
android:fillColor="?attr/primary"
android:pathData="M416,208H272V64c0,-17.67 -14.33,-32 -32,-32h-32c-17.67,0 -32,14.33 -32,32v144H32c-17.67,0 -32,14.33 -32,32v32c0,17.67 14.33,32 32,32h144v144c0,17.67 14.33,32 32,32h32c17.67,0 32,-14.33 32,-32V304h144c17.67,0 32,-14.33 32,-32v-32c0,-17.67 -14.33,-32 -32,-32z" />
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/onPrimary"
android:fillColor="?attr/secondary"
android:pathData="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94c0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6s3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z"
tools:ignore="VectorPath" />
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_shopping_cart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:viewportWidth="48"
android:viewportHeight="48">
<path
android:fillColor="@color/onPrimary"
android:fillColor="?attr/secondary"
android:fillType="nonZero"
android:pathData="M21.7533,32.9866c0,1.2413 -1.0054,2.2466 -2.2466,2.2466c-1.2413,0 -2.2354,-1.0054 -2.2354,-2.2466c0,-1.2413 0.9941,-2.2466 2.2354,-2.2466c1.2413,0 2.2466,1.0054 2.2466,2.2466zM16.4344,12.7668l1.0672,2.2466h16.6084c0.6178,0 1.1233,0.4999 1.1233,1.1233c0,0.1966 -0.0505,0.3819 -0.1404,0.5392l-4.0159,7.2904c-0.3875,0.6909 -1.1233,1.157 -1.9658,1.157h-8.3688l-1.0054,1.831c-0.0225,0.0393 -0.0337,0.0843 -0.0337,0.1348c0,0.1573 0.1236,0.2808 0.2808,0.2808h13.0025v2.2466h-13.4799c-1.2413,0 -2.2466,-1.0054 -2.2466,-2.2466c0,-0.3932 0.1011,-0.7582 0.2752,-1.084l1.5165,-2.7522l-4.0384,-8.5204h-2.2466v-2.2466zM32.9866,32.9866c0,1.2413 -1.0054,2.2466 -2.2466,2.2466c-1.2413,0 -2.2354,-1.0054 -2.2354,-2.2466c0,-1.2413 0.9941,-2.2466 2.2354,-2.2466c1.2413,0 2.2466,1.0054 2.2466,2.2466z"
android:strokeWidth="0"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_trash.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
android:viewportHeight="512"
tools:ignore="VectorRaster">
<path
android:fillColor="@color/primary"
android:fillColor="?attr/primary"
android:pathData="M432,32H312l-9.4,-18.7A24,24 0,0 0,281.1 0H166.8a23.72,23.72 0,0 0,-21.4 13.3L136,32H16A16,16 0,0 0,0 48v32a16,16 0,0 0,16 16h416a16,16 0,0 0,16 -16V48a16,16 0,0 0,-16 -16zM53.2,467a48,48 0,0 0,47.9 45h245.8a48,48 0,0 0,47.9 -45L416,128H32z" />
</vector>
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/mainActionButtonMargin"
android:layout_marginBottom="@dimen/mainActionButtonMargin"
android:contentDescription="@string/mainFloatingAddButtonDescription"
android:onClick="onClickFloatingButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/ic_plus"
android:contentDescription="@string/mainFloatingAddButtonDescription" />
app:srcCompat="@drawable/ic_plus" />

<TextView
android:id="@+id/mainEmptyTextView"
Expand Down
30 changes: 28 additions & 2 deletions app/src/main/res/layout/activity_options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,36 @@
tools:ignore="MissingConstraints" />

<LinearLayout
android:id="@+id/optionsAccentDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/formDefaultMargin"
app:layout_constraintTop_toBottomOf="@id/optionsThemeSwitch">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/optionsAccentText"
android:textSize="@dimen/optionTextSize" />

<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />

<Spinner
android:id="@+id/accentColorDropdown"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/formDefaultMargin"
app:layout_constraintTop_toBottomOf="@id/optionsAccentDropdown">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -53,8 +78,9 @@

<Spinner
android:id="@+id/listOrderDropdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>


Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/spinner_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/optionTextSize"
android:padding="5dip" />
android:padding="5dip"
android:textSize="@dimen/optionTextSize" />
Binary file removed app/src/main/res/mipmap-hdpi/ic_launcher.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-hdpi/ic_launcher_round.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-mdpi/ic_launcher.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-mdpi/ic_launcher_round.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-xhdpi/ic_launcher.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Binary file not shown.
Binary file not shown.
2 changes: 0 additions & 2 deletions app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,4 @@
<item name="android:textColor">@color/white</item>
</style>

<style name="Theme.ShoppingList.MainActivity" parent="Theme.ShoppingList.Base.MainActivity" />

</resources>
Loading

0 comments on commit 4ac3b01

Please sign in to comment.