Update the providers

This commit is contained in:
Tommaso Berlose
2020-10-06 18:11:53 +02:00
parent bd4869540b
commit 1644fb7682
20 changed files with 587 additions and 146 deletions

View File

@ -1,33 +1,43 @@
package com.tommasoberlose.anotherwidget.ui.activities
import android.animation.ValueAnimator
import android.app.Activity
import android.app.AlertDialog
import android.os.Build
import android.content.Intent
import android.content.pm.ResolveInfo
import android.os.Bundle
import android.text.Html
import android.view.View
import android.widget.RelativeLayout
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.animation.addListener
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.tommasoberlose.anotherwidget.R
import com.tommasoberlose.anotherwidget.components.BottomSheetMenu
import com.tommasoberlose.anotherwidget.databinding.ActivityChooseApplicationBinding
import com.tommasoberlose.anotherwidget.databinding.ActivityWeatherProviderBinding
import com.tommasoberlose.anotherwidget.global.Constants
import com.tommasoberlose.anotherwidget.global.Preferences
import com.tommasoberlose.anotherwidget.helpers.WeatherHelper
import com.tommasoberlose.anotherwidget.ui.viewmodels.ChooseApplicationViewModel
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_choose_application.*
import kotlinx.android.synthetic.main.activity_weather_provider.*
import kotlinx.android.synthetic.main.the_widget_sans.*
import kotlinx.android.synthetic.main.activity_weather_provider.action_back
import kotlinx.android.synthetic.main.activity_weather_provider.list_view
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.idik.lib.slimadapter.SlimAdapter
class WeatherProviderActivity : AppCompatActivity() {
private lateinit var adapter: SlimAdapter
private lateinit var viewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
@ -35,39 +45,62 @@ class WeatherProviderActivity : AppCompatActivity() {
setContentView(R.layout.activity_weather_provider)
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
api_key.editText?.setText(Preferences.weatherProviderApi)
}
api_key.editText?.setText(Preferences.weatherProviderApiOpen)
override fun onResume() {
super.onResume()
list_view.setHasFixedSize(true)
val mLayoutManager = LinearLayoutManager(this)
list_view.layoutManager = mLayoutManager
setListener()
adapter = SlimAdapter.create()
adapter
.register<Int>(R.layout.list_item) { item, injector ->
val provider = Constants.WeatherProvider.fromInt(item)!!
injector
.text(R.id.text, WeatherHelper.getProviderName(this, provider))
.clicked(R.id.text) {
Preferences.weatherProvider = item
}
}
.attachTo(list_view)
setupListener()
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())
adapter.updateData(Constants.WeatherProvider.values().map { it.value })
api_key_container.isVisible = WeatherHelper.isKeyRequired()
WeatherHelper.getProviderInfoTitle(this).let {
info_title.text = it
info_title.isVisible = it != ""
}
WeatherHelper.getProviderInfoSubtitle(this).let {
info_subtitle.text = it
info_subtitle.isVisible = it != ""
}
action_open_provider.text = WeatherHelper.getProviderLinkName(this)
})
viewModel.weatherProviderApi
// viewModel.weatherProvider.observe(this, Observer {
// weather_provider.editText?.setText(WeatherHelper.getProviderName(this, Constants.WeatherProvider.fromInt(Preferences.weatherProvider)!!).split("\n").first())
//
// api_key_container.isVisible = WeatherHelper.isKeyRequired()
//
// WeatherHelper.getProviderInfoTitle(this).let {
// info_title.text = it
// info_title.isVisible = it != ""
// }
//
// WeatherHelper.getProviderInfoSubtitle(this).let {
// info_subtitle.text = it
// info_subtitle.isVisible = it != ""
// }
//
// action_open_provider.text = WeatherHelper.getProviderLinkName(this)
//
// api_key.editText?.setText(when (Constants.WeatherProvider.fromInt(it)) {
// Constants.WeatherProvider.OPEN_WEATHER -> Preferences.weatherProviderApiOpen
// Constants.WeatherProvider.WEATHER_BIT -> Preferences.weatherProviderApiWeatherBit
// Constants.WeatherProvider.WEATHER_API -> Preferences.weatherProviderApiWeatherApi
// Constants.WeatherProvider.HERE -> Preferences.weatherProviderApiHere
// Constants.WeatherProvider.ACCUWEATHER -> Preferences.weatherProviderApiAccuweather
// Constants.WeatherProvider.WEATHER_GOV,
// Constants.WeatherProvider.YR,
// null -> ""
// })
// })
}
private fun setListener() {
private fun setupListener() {
action_back.setOnClickListener {
onBackPressed()
}
@ -79,7 +112,7 @@ class WeatherProviderActivity : AppCompatActivity() {
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 {
(0 until 7).forEach {
val item = Constants.WeatherProvider.fromInt(it)
dialog.addItem(WeatherHelper.getProviderName(this, item!!), it)
}
@ -92,7 +125,14 @@ class WeatherProviderActivity : AppCompatActivity() {
api_key.editText?.addTextChangedListener {
val key = it?.toString() ?: ""
Preferences.weatherProviderApi = key
when (Constants.WeatherProvider.fromInt(Preferences.weatherProvider)) {
Constants.WeatherProvider.OPEN_WEATHER -> Preferences.weatherProviderApiOpen = key
Constants.WeatherProvider.WEATHER_BIT -> Preferences.weatherProviderApiWeatherBit = key
Constants.WeatherProvider.WEATHER_API -> Preferences.weatherProviderApiWeatherApi = key
Constants.WeatherProvider.HERE -> Preferences.weatherProviderApiHere = key
Constants.WeatherProvider.ACCUWEATHER -> Preferences.weatherProviderApiAccuweather = key
else -> {}
}
}
}

View File

@ -2,8 +2,6 @@ package com.tommasoberlose.anotherwidget.ui.fragments
import android.Manifest
import android.animation.ValueAnimator
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo
import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
@ -11,7 +9,6 @@ import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.DisplayMetrics
import android.util.Log
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
@ -355,7 +352,7 @@ class MainFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeList
backgroundColor = ContextCompat.getColor(requireContext(), R.color.errorColorText)
badgeGravity = BadgeDrawable.TOP_END
}?.isVisible = if (Preferences.showWeather) {
(WeatherHelper.isKeyRequired() && Preferences.weatherProviderApi == "")
(WeatherHelper.isKeyRequired() && Preferences.weatherProviderApiOpen == "")
|| (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)

View File

@ -170,9 +170,11 @@ class WeatherTabFragment : Fragment() {
if (activity?.checkGrantedPermission(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Manifest.permission.ACCESS_BACKGROUND_LOCATION else Manifest.permission.ACCESS_FINE_LOCATION) == true) {
location_permission_alert?.isVisible = false
background_location_warning.isVisible = Preferences.customLocationAdd == ""
WeatherReceiver.setUpdates(requireContext())
} else if (Preferences.showWeather && Preferences.customLocationAdd == "") {
location_permission_alert?.isVisible = true
background_location_warning.isVisible = false
location_permission_alert?.setOnClickListener {
MaterialBottomSheetDialog(requireContext(), message = getString(R.string.background_location_warning))
.setPositiveButton(getString(android.R.string.ok)) {
@ -283,7 +285,7 @@ class WeatherTabFragment : Fragment() {
MainWidget.updateWidget(requireContext())
}
RequestCode.WEATHER_PROVIDER_REQUEST_CODE.code -> {
WeatherReceiver.setOneTimeUpdate(requireContext())
checkLocationPermission()
}
}
}

View File

@ -64,7 +64,7 @@ class MainViewModel : ViewModel() {
val weatherRefreshPeriod = Preferences.asLiveData(Preferences::weatherRefreshPeriod)
val weatherAppName = Preferences.asLiveData(Preferences::weatherAppName)
val weatherProviderApi = Preferences.asLiveData(Preferences::weatherProviderApi)
val weatherProviderApi = Preferences.asLiveData(Preferences::weatherProviderApiOpen)
val customLocationAdd = Preferences.asLiveData(Preferences::customLocationAdd)