Update the provider activity

This commit is contained in:
Tommaso Berlose 2020-10-04 18:02:08 +02:00
parent 0a454f0b5f
commit d14dfee980
9 changed files with 263 additions and 154 deletions

View File

@ -40,6 +40,8 @@ object Preferences : KotprefModel() {
var weatherAppPackage by stringPref(key = "PREF_WEATHER_APP_PACKAGE", default = "")
var weatherProviderApi by stringPref(key = "PREF_WEATHER_PROVIDER_API_KEY", default = "")
var weatherProvider by intPref(default = Constants.WeatherProvider.OPEN_WEATHER.value)
var weatherProviderError by stringPref(default = "")
var weatherProviderLocationError by stringPref(default = "")
var eventAppName by stringPref(key = "PREF_EVENT_APP_NAME", default = "")
var eventAppPackage by stringPref(key = "PREF_EVENT_APP_PACKAGE", default = "")
var openEventDetails by booleanPref(default = true)

View File

@ -62,6 +62,56 @@ object WeatherHelper {
})
}
fun getProviderLinkName(context: Context): String {
return context.getString(when(Constants.WeatherProvider.fromInt(Preferences.weatherProvider)) {
Constants.WeatherProvider.OPEN_WEATHER -> R.string.action_open_provider_open_weather
Constants.WeatherProvider.WEATHER_BIT -> R.string.action_open_provider_weatherbit
Constants.WeatherProvider.FORECA -> R.string.action_open_provider_foreca
Constants.WeatherProvider.HERE -> R.string.action_open_provider_here
Constants.WeatherProvider.ACCUWEATHER -> R.string.action_open_provider_accuweather
Constants.WeatherProvider.WEATHER_GOV -> R.string.action_open_provider_weather_gov
Constants.WeatherProvider.YR -> R.string.action_open_provider_yr
Constants.WeatherProvider.SMHI -> R.string.action_open_provider_smhi
Constants.WeatherProvider.WEATHER_CA -> R.string.action_open_provider_weather_ca
Constants.WeatherProvider.BOM -> R.string.action_open_provider_bom
Constants.WeatherProvider.METEOFRANCE -> R.string.action_open_provider_meteofrance
else -> R.string.nothing
})
}
fun getProviderLink(): String {
return when(Constants.WeatherProvider.fromInt(Preferences.weatherProvider)) {
Constants.WeatherProvider.OPEN_WEATHER -> "https://home.openweathermap.org/users/sign_up"
Constants.WeatherProvider.WEATHER_BIT -> ""
Constants.WeatherProvider.FORECA -> ""
Constants.WeatherProvider.HERE -> ""
Constants.WeatherProvider.ACCUWEATHER -> ""
Constants.WeatherProvider.WEATHER_GOV -> ""
Constants.WeatherProvider.YR -> ""
Constants.WeatherProvider.SMHI -> ""
Constants.WeatherProvider.WEATHER_CA -> ""
Constants.WeatherProvider.BOM -> ""
Constants.WeatherProvider.METEOFRANCE -> ""
else -> ""
}
}
fun isKeyRequired(): Boolean = when (Constants.WeatherProvider.fromInt(Preferences.weatherProvider)) {
Constants.WeatherProvider.OPEN_WEATHER,
Constants.WeatherProvider.WEATHER_BIT,
Constants.WeatherProvider.FORECA,
Constants.WeatherProvider.HERE,
Constants.WeatherProvider.ACCUWEATHER,
Constants.WeatherProvider.YR,
Constants.WeatherProvider.SMHI,
Constants.WeatherProvider.WEATHER_CA,
Constants.WeatherProvider.BOM,
Constants.WeatherProvider.METEOFRANCE -> true
Constants.WeatherProvider.WEATHER_GOV -> false
else -> true
}
fun getWeatherIconResource(icon: String, style: Int = Preferences.weatherIconPack): Int {
return when (icon) {
"01d" -> {

View File

@ -1,37 +1,91 @@
package com.tommasoberlose.anotherwidget.ui.activities
import android.animation.ValueAnimator
import android.app.Activity
import android.app.AlertDialog
import android.os.Build
import android.os.Bundle
import android.text.Html
import android.view.View
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.animation.addListener
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.tommasoberlose.anotherwidget.R
import com.tommasoberlose.anotherwidget.components.BottomSheetMenu
import com.tommasoberlose.anotherwidget.global.Constants
import com.tommasoberlose.anotherwidget.global.Preferences
import com.tommasoberlose.anotherwidget.helpers.WeatherHelper
import com.tommasoberlose.anotherwidget.ui.viewmodels.MainViewModel
import com.tommasoberlose.anotherwidget.utils.collapse
import com.tommasoberlose.anotherwidget.utils.expand
import com.tommasoberlose.anotherwidget.utils.openURI
import kotlinx.android.synthetic.main.activity_weather_provider.*
import kotlinx.android.synthetic.main.the_widget_sans.*
class WeatherProviderActivity : AppCompatActivity() {
private lateinit var viewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_weather_provider)
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
api_key.editText?.setText(Preferences.weatherProviderApi)
}
override fun onResume() {
super.onResume()
setListener()
subscribeUi(viewModel)
}
private fun subscribeUi(viewModel: MainViewModel) {
viewModel.weatherProvider.observe(this, Observer {
weather_provider.editText?.setText(WeatherHelper.getProviderName(this, Constants.WeatherProvider.fromInt(Preferences.weatherProvider)!!).split("\n").first())
info_container.isVisible = WeatherHelper.isKeyRequired()
api_key_container.isVisible = WeatherHelper.isKeyRequired()
action_open_provider.text = WeatherHelper.getProviderLinkName(this)
})
}
private fun setListener() {
action_back.setOnClickListener {
onBackPressed()
}
action_save.setOnClickListener {
Preferences.weatherProviderApi = api_key.editText?.text.toString()
action_open_provider.setOnClickListener {
openURI(WeatherHelper.getProviderLink())
}
weather_provider_inner.setOnClickListener {
if (Preferences.showWeather) {
val dialog = BottomSheetMenu<Int>(this, header = getString(R.string.settings_weather_provider_api)).setSelectedValue(Preferences.weatherProvider)
(0 until 11).forEach {
val item = Constants.WeatherProvider.fromInt(it)
dialog.addItem(WeatherHelper.getProviderName(this, item!!), it)
}
dialog.addOnSelectItemListener { value ->
Preferences.weatherProvider = value
}.show()
}
}
api_key.editText?.addTextChangedListener {
Preferences.weatherProviderApi = it?.toString() ?: ""
}
}
override fun onBackPressed() {
setResult(Activity.RESULT_OK)
finish()
}
action_open_provider.setOnClickListener {
openURI("https://home.openweathermap.org/users/sign_up")
}
api_key.editText?.setText(Preferences.weatherProviderApi)
}
}

View File

@ -36,6 +36,7 @@ import com.tommasoberlose.anotherwidget.global.Preferences
import com.tommasoberlose.anotherwidget.helpers.BitmapHelper
import com.tommasoberlose.anotherwidget.helpers.ColorHelper
import com.tommasoberlose.anotherwidget.helpers.ColorHelper.isColorDark
import com.tommasoberlose.anotherwidget.helpers.WeatherHelper
import com.tommasoberlose.anotherwidget.ui.activities.MainActivity
import com.tommasoberlose.anotherwidget.ui.adapters.ViewPagerAdapter
import com.tommasoberlose.anotherwidget.ui.viewmodels.MainViewModel
@ -349,8 +350,15 @@ class MainFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeList
tabs?.getTabAt(2)?.orCreateBadge?.apply {
backgroundColor = ContextCompat.getColor(requireContext(), R.color.errorColorText)
badgeGravity = BadgeDrawable.TOP_END
}?.isVisible = Preferences.showWeather && (Preferences.weatherProviderApi == "" || (Preferences.customLocationAdd == "" && activity?.checkGrantedPermission(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Manifest.permission.ACCESS_BACKGROUND_LOCATION else Manifest.permission.ACCESS_FINE_LOCATION) != true))
}?.isVisible = if (Preferences.showWeather) {
(WeatherHelper.isKeyRequired() && Preferences.weatherProviderApi == "")
|| (Preferences.customLocationAdd == "" && activity?.checkGrantedPermission(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Manifest.permission.ACCESS_BACKGROUND_LOCATION else Manifest.permission.ACCESS_FINE_LOCATION
) != true)
|| (Preferences.weatherProviderError != "")
} else {
false
}
// Music error indicator
tabs?.getTabAt(4)?.orCreateBadge?.apply {

View File

@ -105,14 +105,16 @@ class WeatherTabFragment : Fragment() {
viewModel.weatherProvider.observe(viewLifecycleOwner, Observer {
maintainScrollPosition {
label_weather_provider.text = WeatherHelper.getProviderName(requireContext(), Constants.WeatherProvider.fromInt(it)!!)
checkWeatherProviderConfig()
}
})
viewModel.weatherProviderApi.observe(viewLifecycleOwner, Observer {
maintainScrollPosition {
viewModel.weatherProviderError.observe(viewLifecycleOwner, Observer {
checkWeatherProviderConfig()
})
viewModel.weatherProviderLocationError.observe(viewLifecycleOwner, Observer {
checkWeatherProviderConfig()
}
checkLocationPermission()
})
viewModel.customLocationAdd.observe(viewLifecycleOwner, Observer {
@ -184,11 +186,11 @@ class WeatherTabFragment : Fragment() {
}
private fun checkWeatherProviderConfig() {
label_weather_provider_api_key?.text =
if (Preferences.weatherProviderApi == "") getString(R.string.settings_weather_provider_api_key_subtitle_not_set) else getString(
R.string.settings_weather_provider_api_key_subtitle_all_set
)
label_weather_provider_api_key?.setTextColor(ContextCompat.getColor(requireContext(), if (Preferences.weatherProviderApi == "" && Preferences.showWeather) R.color.errorColorText else R.color.colorSecondaryText))
weather_provider_error.isVisible = Preferences.weatherProviderError != ""
weather_provider_error?.text = Preferences.weatherProviderError
weather_provider_location_error.isVisible = Preferences.weatherProviderLocationError != ""
weather_provider_location_error?.text = Preferences.weatherProviderLocationError
}
private fun setupListener() {
@ -205,21 +207,6 @@ class WeatherTabFragment : Fragment() {
}
action_weather_provider.setOnClickListener {
if (Preferences.showWeather) {
val dialog = BottomSheetMenu<Int>(requireContext(), header = getString(R.string.settings_weather_provider_api)).setSelectedValue(Preferences.weatherProvider)
(0 until 11).forEach {
val item = Constants.WeatherProvider.fromInt(it)
dialog.addItem(WeatherHelper.getProviderName(requireContext(), item!!), it)
}
dialog.addOnSelectItemListener { value ->
Preferences.weatherProvider = value
checkWeatherProviderConfig()
}.show()
}
}
action_weather_provider_api_key.setOnClickListener {
if (Preferences.showWeather) {
startActivityForResult(
Intent(requireContext(), WeatherProviderActivity::class.java),

View File

@ -68,6 +68,8 @@ class MainViewModel : ViewModel() {
val showWeatherWarning = Preferences.asLiveData(Preferences::showWeatherWarning)
val weatherIconPack = Preferences.asLiveData(Preferences::weatherIconPack)
val weatherProvider = Preferences.asLiveData(Preferences::weatherProvider)
val weatherProviderError = Preferences.asLiveData(Preferences::weatherProviderError)
val weatherProviderLocationError = Preferences.asLiveData(Preferences::weatherProviderLocationError)
// Glance
val showGlance = Preferences.asLiveData(Preferences::showGlance)

View File

@ -33,7 +33,7 @@
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:text="@string/settings_weather_provider_api_key_title"
android:text="@string/settings_weather_provider_api"
android:gravity="center"
style="@style/AnotherWidget.Main.Title"/>
<TextView
@ -41,6 +41,7 @@
android:layout_height="wrap_content"
android:text="@string/action_save"
android:gravity="center"
android:visibility="gone"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:padding="16dp"
@ -63,6 +64,45 @@
android:orientation="horizontal"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/weather_provider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:textStyle="bold"
android:nextFocusUp="@id/api_key"
android:nextFocusLeft="@id/api_key"
android:gravity="center_vertical|start"
android:textColor="@color/colorPrimaryText"
app:boxBackgroundColor="@color/colorPrimary"
app:boxStrokeWidth="0dp"
app:hintTextColor="@color/colorPrimary"
android:hint="@string/settings_weather_provider_api"
android:fontFamily="@font/google_sans"
android:textAlignment="viewStart"
android:textDirection="locale"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="viewStart"
android:focusable="false"
android:clickable="false"
android:id="@+id/weather_provider_inner"
android:textDirection="locale"
android:fontFamily="@font/google_sans"
android:gravity="center_vertical|start" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:paddingTop="16dp"
android:id="@+id/api_key_container"
android:focusableInTouchMode="true"
android:gravity="center_vertical">
@ -79,7 +119,7 @@
app:boxBackgroundColor="@color/colorPrimary"
app:boxStrokeWidth="0dp"
app:hintTextColor="@color/colorAccent"
android:hint="@string/api_key_hint"
android:hint="@string/settings_weather_provider_api_key_title"
android:fontFamily="@font/google_sans"
android:textAlignment="viewStart"
android:textDirection="locale"
@ -89,73 +129,39 @@
android:layout_height="wrap_content"
android:textAlignment="viewStart"
android:textDirection="locale"
android:id="@+id/api_key_inner"
android:fontFamily="@font/google_sans"
android:gravity="center_vertical|start" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/info_container">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="32dp"
android:layout_marginBottom="24dp"
android:background="@color/disabledButtonBackground" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/AnotherWidget.Settings.Title"
android:gravity="start"
android:textAlignment="viewStart"
android:text="@string/api_key_title_1"/>
android:text="This weather provider needs\nan API key to work correctly."/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/AnotherWidget.Main.Subtitle"
android:textSize="16sp"
android:gravity="start"
android:textAlignment="viewStart"
android:text="@string/api_key_subtitle_1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/AnotherWidget.Settings.Title"
android:layout_marginTop="12dp"
android:gravity="start"
android:textAlignment="viewStart"
android:text="@string/api_key_title_2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/AnotherWidget.Main.Subtitle"
android:textSize="16sp"
android:gravity="start"
android:textAlignment="viewStart"
android:text="@string/api_key_subtitle_2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/AnotherWidget.Settings.Title"
android:layout_marginTop="12dp"
android:gravity="start"
android:textAlignment="viewStart"
android:text="@string/api_key_title_3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/AnotherWidget.Main.Subtitle"
android:textSize="16sp"
android:gravity="start"
android:textAlignment="viewStart"
android:text="@string/api_key_subtitle_3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/AnotherWidget.Main.Subtitle"
android:textSize="16sp"
android:gravity="start"
android:textAlignment="viewStart"
android:text="@string/api_key_info_all_set"/>
android:text="Please, open the provider website, create a personal account and copy here the default key."/>
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -163,6 +169,8 @@
android:letterSpacing="0"
android:clickable="true"
android:focusable="true"
android:layout_marginTop="8dp"
android:textSize="15sp"
android:gravity="start|center_vertical"
android:textAlignment="viewStart"
android:layout_marginStart="-8dp"
@ -171,5 +179,6 @@
android:id="@+id/action_open_provider"
android:text="@string/action_open_provider"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>

View File

@ -162,52 +162,22 @@
style="@style/AnotherWidget.Settings.Subtitle"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_vertical"
android:id="@+id/action_weather_provider_api_key"
android:orientation="horizontal">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="12dp"
android:src="@drawable/round_vpn_key"
app:tint="@color/colorPrimaryText"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/AnotherWidget.Settings.Title"
android:text="@string/settings_weather_provider_api_key_title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/label_weather_provider_api_key"
style="@style/AnotherWidget.Settings.Subtitle"/>
</LinearLayout>
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="12dp"
android:duplicateParentState="true"
android:textSize="14sp"
android:paddingStart="64dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp"
android:visibility="gone"
android:id="@+id/api_key_alert_icon"
android:src="@drawable/round_error"
app:tint="@color/errorColorText"/>
</LinearLayout>
android:id="@+id/weather_provider_error"
android:textColor="@color/errorColorText"
android:letterSpacing="0"
android:fontFamily="@font/google_sans"
android:textStyle="bold"
android:textAppearance="@style/TextAppearance.MaterialComponents.Button"
app:textAllCaps="false" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -262,6 +232,22 @@
android:text="@string/action_grant_permission"/>
</LinearLayout>
</LinearLayout>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:textSize="14sp"
android:paddingStart="64dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp"
android:visibility="gone"
android:id="@+id/weather_provider_location_error"
android:textColor="@color/errorColorText"
android:letterSpacing="0"
android:fontFamily="@font/google_sans"
android:textStyle="bold"
android:textAppearance="@style/TextAppearance.MaterialComponents.Button"
app:textAllCaps="false" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

View File

@ -130,6 +130,17 @@
<string name="settings_weather_provider_weather_ca">Weather.gc.ca (Canada)</string>
<string name="settings_weather_provider_bom">BOM (Australia)\nPowered by Australia\'s national weather</string>
<string name="settings_weather_provider_meteofrance">Meteofrance.com (France)</string>
<string name="action_open_provider_open_weather">Open openweathermap.com</string>
<string name="action_open_provider_weatherbit">Open weatherbit.io</string>
<string name="action_open_provider_foreca">Open foreca.com</string>
<string name="action_open_provider_here">Open here.com</string>
<string name="action_open_provider_accuweather">Open Accuweather.com</string>
<string name="action_open_provider_weather_gov">Open Weather.gov</string>
<string name="action_open_provider_yr">Open yr.no</string>
<string name="action_open_provider_smhi">Open smhi.se</string>
<string name="action_open_provider_weather_ca">Open weather.gc.ca</string>
<string name="action_open_provider_bom">Open bom.gov.au</string>
<string name="action_open_provider_meteofrance">Open meteofrance.com</string>
<!-- Clock -->
<string name="settings_clock_title">Clock</string>