This commit is contained in:
Tommaso Berlose 2020-05-22 19:18:29 +02:00
parent 94825808f4
commit e58fef66aa
11 changed files with 71 additions and 15 deletions

View File

@ -31,6 +31,7 @@ object Preferences : KotprefModel() {
var customLocationAdd by stringPref(key = "PREF_CUSTOM_LOCATION_ADD", default = "") var customLocationAdd by stringPref(key = "PREF_CUSTOM_LOCATION_ADD", default = "")
var dateFormat by stringPref(default = "") var dateFormat by stringPref(default = "")
var isDateCapitalize by booleanPref(default = true) var isDateCapitalize by booleanPref(default = true)
var isDateUppercase by booleanPref(default = false)
var weatherRefreshPeriod by intPref(key = "PREF_WEATHER_REFRESH_PERIOD", default = 1) var weatherRefreshPeriod by intPref(key = "PREF_WEATHER_REFRESH_PERIOD", default = 1)
var showUntil by intPref(key = "PREF_SHOW_UNTIL", default = 1) var showUntil by intPref(key = "PREF_SHOW_UNTIL", default = 1)
var calendarAppName by stringPref(key = "PREF_CALENDAR_APP_NAME", default = "") var calendarAppName by stringPref(key = "PREF_CALENDAR_APP_NAME", default = "")

View File

@ -2,7 +2,6 @@ package com.tommasoberlose.anotherwidget.helpers
import android.content.Context import android.content.Context
import android.text.format.DateUtils import android.text.format.DateUtils
import android.util.Log
import com.tommasoberlose.anotherwidget.global.Preferences import com.tommasoberlose.anotherwidget.global.Preferences
import com.tommasoberlose.anotherwidget.utils.getCapWordString import com.tommasoberlose.anotherwidget.utils.getCapWordString
import java.lang.Exception import java.lang.Exception
@ -17,7 +16,11 @@ object DateHelper {
} catch (e: Exception) { } catch (e: Exception) {
getDefaultDateText(context, date) getDefaultDateText(context, date)
} }
if (Preferences.isDateCapitalize) text.getCapWordString() else text when {
Preferences.isDateUppercase -> text.toUpperCase(Locale.getDefault())
Preferences.isDateCapitalize -> text.getCapWordString()
else -> text
}
} else { } else {
val flags: Int = val flags: Int =
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH
@ -25,17 +28,20 @@ object DateHelper {
SimpleDateFormat("EEEE", Locale.getDefault()).format(date.time), SimpleDateFormat("EEEE", Locale.getDefault()).format(date.time),
DateUtils.formatDateTime(context, date.timeInMillis, flags) DateUtils.formatDateTime(context, date.timeInMillis, flags)
) )
if (Preferences.isDateCapitalize) text.getCapWordString() else text when {
Preferences.isDateUppercase -> text.toUpperCase(Locale.getDefault())
Preferences.isDateCapitalize -> text.getCapWordString()
else -> text
}
} }
} }
fun getDefaultDateText(context: Context, date: Calendar): String { fun getDefaultDateText(context: Context, date: Calendar): String {
val flags: Int = val flags: Int =
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH
val text = "%s, %s".format( return "%s, %s".format(
SimpleDateFormat("EEEE", Locale.getDefault()).format(date.time), SimpleDateFormat("EEEE", Locale.getDefault()).format(date.time),
DateUtils.formatDateTime(context, date.timeInMillis, flags) DateUtils.formatDateTime(context, date.timeInMillis, flags)
) )
return if (Preferences.isDateCapitalize) text.getCapWordString() else text
} }
} }

View File

@ -6,12 +6,14 @@ import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.View import android.view.View
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.chibatching.kotpref.blockingBulk
import com.chibatching.kotpref.bulk import com.chibatching.kotpref.bulk
import com.tommasoberlose.anotherwidget.R import com.tommasoberlose.anotherwidget.R
import com.tommasoberlose.anotherwidget.databinding.ActivityCustomDateBinding import com.tommasoberlose.anotherwidget.databinding.ActivityCustomDateBinding
@ -90,10 +92,14 @@ class CustomDateActivity : AppCompatActivity() {
"__" "__"
} }
if (Preferences.isDateCapitalize) { if (viewModel.isDateCapitalize.value == true) {
text = text.getCapWordString() text = text.getCapWordString()
} }
if (viewModel.isDateUppercase.value == true) {
text = text.toUpperCase(Locale.getDefault())
}
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
action_save.isVisible = text != ERROR_STRING action_save.isVisible = text != ERROR_STRING
loader.visibility = View.INVISIBLE loader.visibility = View.INVISIBLE
@ -105,8 +111,30 @@ class CustomDateActivity : AppCompatActivity() {
viewModel.isDateCapitalize.observe(this, Observer { viewModel.isDateCapitalize.observe(this, Observer {
viewModel.dateInput.value = viewModel.dateInput.value viewModel.dateInput.value = viewModel.dateInput.value
binding.isdCapitalizeEnabled = it updateCapitalizeUi()
}) })
viewModel.isDateUppercase.observe(this, Observer {
viewModel.dateInput.value = viewModel.dateInput.value
updateCapitalizeUi()
})
}
private fun updateCapitalizeUi() {
when {
viewModel.isDateUppercase.value == true -> {
action_capitalize.setImageDrawable(ContextCompat.getDrawable(this@CustomDateActivity, R.drawable.round_publish))
action_capitalize.alpha = 1f
}
viewModel.isDateCapitalize.value == true -> {
action_capitalize.setImageDrawable(ContextCompat.getDrawable(this@CustomDateActivity, R.drawable.ic_capitalize))
action_capitalize.alpha = 1f
}
else -> {
action_capitalize.setImageDrawable(ContextCompat.getDrawable(this@CustomDateActivity, R.drawable.round_publish))
action_capitalize.alpha = 0.3f
}
}
} }
private fun setupListener() { private fun setupListener() {
@ -115,12 +143,29 @@ class CustomDateActivity : AppCompatActivity() {
} }
action_save.setOnClickListener { action_save.setOnClickListener {
Preferences.dateFormat = viewModel.dateInput.value ?: "" Preferences.blockingBulk {
dateFormat = viewModel.dateInput.value ?: ""
isDateCapitalize = viewModel.isDateCapitalize.value ?: true
isDateUppercase = viewModel.isDateUppercase.value ?: false
}
finish() finish()
} }
action_capitalize.setOnClickListener { action_capitalize.setOnClickListener {
Preferences.isDateCapitalize = !Preferences.isDateCapitalize when {
viewModel.isDateUppercase.value == true -> {
viewModel.isDateCapitalize.value = false
viewModel.isDateUppercase.value = false
}
viewModel.isDateCapitalize.value == true -> {
viewModel.isDateCapitalize.value = false
viewModel.isDateUppercase.value = true
}
else -> {
viewModel.isDateCapitalize.value = true
viewModel.isDateUppercase.value = false
}
}
} }
action_capitalize.setOnLongClickListener { action_capitalize.setOnLongClickListener {

View File

@ -12,6 +12,7 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.chibatching.kotpref.blockingBulk
import com.tommasoberlose.anotherwidget.R import com.tommasoberlose.anotherwidget.R
import com.tommasoberlose.anotherwidget.components.BottomSheetColorPicker import com.tommasoberlose.anotherwidget.components.BottomSheetColorPicker
import com.tommasoberlose.anotherwidget.components.BottomSheetMenu import com.tommasoberlose.anotherwidget.components.BottomSheetMenu
@ -256,6 +257,12 @@ class GeneralTabFragment : Fragment() {
dialog.addOnSelectItemListener { value -> dialog.addOnSelectItemListener { value ->
if (value == "-") { if (value == "-") {
startActivity(Intent(requireContext(), CustomDateActivity::class.java)) startActivity(Intent(requireContext(), CustomDateActivity::class.java))
} else if (value == "") {
Preferences.blockingBulk {
isDateCapitalize = false
isDateUppercase = false
}
Preferences.dateFormat = value
} else { } else {
Preferences.dateFormat = value Preferences.dateFormat = value
} }

View File

@ -8,5 +8,6 @@ import com.tommasoberlose.anotherwidget.global.Preferences
class CustomDateViewModel(application: Application) : AndroidViewModel(application) { class CustomDateViewModel(application: Application) : AndroidViewModel(application) {
val dateInput: MutableLiveData<String> = MutableLiveData(if (Preferences.dateFormat == "") "EEEE, MMM dd" else Preferences.dateFormat) val dateInput: MutableLiveData<String> = MutableLiveData(if (Preferences.dateFormat == "") "EEEE, MMM dd" else Preferences.dateFormat)
val isDateCapitalize = Preferences.asLiveData(Preferences::isDateCapitalize) val isDateCapitalize = MutableLiveData<Boolean>(Preferences.isDateCapitalize)
val isDateUppercase = MutableLiveData<Boolean>(Preferences.isDateUppercase)
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 471 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 355 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 592 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 773 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -5,9 +5,6 @@
<variable <variable
name="viewModel" name="viewModel"
type="com.tommasoberlose.anotherwidget.ui.viewmodels.CustomDateViewModel" /> type="com.tommasoberlose.anotherwidget.ui.viewmodels.CustomDateViewModel" />
<variable
name="isdCapitalizeEnabled"
type="Boolean" />
</data> </data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
@ -145,14 +142,13 @@
android:layout_height="40dp" android:layout_height="40dp"
android:padding="5dp" android:padding="5dp"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:src="@drawable/round_publish" android:src="@drawable/ic_capitalize"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:clickable="true" android:clickable="true"
android:focusable="true" android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless" android:background="?attr/selectableItemBackgroundBorderless"
app:tint="@color/colorPrimaryText" app:tint="@color/colorPrimaryText"
android:layout_marginEnd="20dp" android:layout_marginEnd="20dp"
android:alpha="@{isdCapitalizeEnabled ? 1f : 0.3f, default=0.3}"
android:id="@+id/action_capitalize" /> android:id="@+id/action_capitalize" />
</RelativeLayout> </RelativeLayout>
</com.google.android.material.card.MaterialCardView> </com.google.android.material.card.MaterialCardView>