Added toast extension function

This commit is contained in:
Arty Bishop committed 2020-05-13 10:57:35 +01:00
1 parent d804c20bd0
commit a5c08c2614
5 files changed
+28 -52

No files matched your search

@@ -30,7 +30,6 @@ import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import android.view.WindowManager import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
@@ -50,6 +49,7 @@ import com.rtbishop.look4sat.data.Result
import com.rtbishop.look4sat.databinding.ActivityMainBinding import com.rtbishop.look4sat.databinding.ActivityMainBinding
import com.rtbishop.look4sat.databinding.DialogTleUrlBinding import com.rtbishop.look4sat.databinding.DialogTleUrlBinding
import com.rtbishop.look4sat.databinding.DrawerHeaderBinding import com.rtbishop.look4sat.databinding.DrawerHeaderBinding
import com.rtbishop.look4sat.utility.GeneralUtils.toast
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
@@ -131,8 +131,10 @@ class MainActivity : AppCompatActivity() {
} }
is Result.Error -> { is Result.Error -> {
when (result.exception) { when (result.exception) {
is SecurityException -> toast(getString(R.string.err_no_permissions)) is SecurityException ->
is IllegalArgumentException -> toast(getString(R.string.update_loc_failure)) getString(R.string.err_no_permissions).toast(this)
is IllegalArgumentException ->
getString(R.string.update_loc_failure).toast(this)
} }
} }
} }
@@ -141,11 +143,11 @@ class MainActivity : AppCompatActivity() {
when (result) { when (result) {
is Result.Success -> { is Result.Success -> {
when (result.data) { when (result.data) {
0 -> toast(getString(R.string.update_tle_success)) 0 -> getString(R.string.update_tle_success).toast(this)
1 -> toast(getString(R.string.update_trans_success)) 1 -> getString(R.string.update_trans_success).toast(this)
} }
} }
is Result.Error -> toast(getString(R.string.update_failure)) is Result.Error -> getString(R.string.update_failure).toast(this)
} }
}) })
} }
@@ -202,10 +204,6 @@ class MainActivity : AppCompatActivity() {
} }
} }
private fun toast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
private fun View.lockButton() { private fun View.lockButton() {
lifecycleScope.launch { lifecycleScope.launch {
this@lockButton.isEnabled = false this@lockButton.isEnabled = false
@@ -217,7 +215,7 @@ class MainActivity : AppCompatActivity() {
private fun requestLocationUpdate() { private fun requestLocationUpdate() {
if (ContextCompat.checkSelfPermission(this, permLocation) != permGranted) { if (ContextCompat.checkSelfPermission(this, permLocation) != permGranted) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permLocation)) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, permLocation)) {
toast(getString(R.string.err_no_permissions)) getString(R.string.err_no_permissions).toast(this)
} else { } else {
ActivityCompat.requestPermissions(this, arrayOf(permLocation), permLocationCode) ActivityCompat.requestPermissions(this, arrayOf(permLocation), permLocationCode)
} }
@@ -21,7 +21,6 @@ package com.rtbishop.look4sat.ui.fragments
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
@@ -38,6 +37,7 @@ import com.rtbishop.look4sat.ui.MainActivity
import com.rtbishop.look4sat.ui.SharedViewModel import com.rtbishop.look4sat.ui.SharedViewModel
import com.rtbishop.look4sat.ui.views.MapView import com.rtbishop.look4sat.ui.views.MapView
import com.rtbishop.look4sat.utility.GeneralUtils import com.rtbishop.look4sat.utility.GeneralUtils
import com.rtbishop.look4sat.utility.GeneralUtils.toast
import com.rtbishop.look4sat.utility.PassPredictor import com.rtbishop.look4sat.utility.PassPredictor
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -69,9 +69,7 @@ class MapViewFragment : Fragment(R.layout.fragment_map_view) {
private fun setupObservers(binding: FragmentMapViewBinding) { private fun setupObservers(binding: FragmentMapViewBinding) {
viewModel.getGSP().observe(viewLifecycleOwner, androidx.lifecycle.Observer { result -> viewModel.getGSP().observe(viewLifecycleOwner, androidx.lifecycle.Observer { result ->
when (result) { when (result) {
is Result.Success -> { is Result.Success -> gsp = result.data
gsp = result.data
}
} }
}) })
@@ -99,11 +97,7 @@ class MapViewFragment : Fragment(R.layout.fragment_map_view) {
refreshView() refreshView()
} else { } else {
binding.fabMap.setOnClickListener { binding.fabMap.setOnClickListener {
Toast.makeText( getString(R.string.err_no_sat_selected).toast(mainActivity)
mainActivity,
getString(R.string.err_no_sat_selected),
Toast.LENGTH_SHORT
).show()
} }
} }
} }
@@ -22,13 +22,13 @@ package com.rtbishop.look4sat.ui.fragments
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.text.InputType import android.text.InputType
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.preference.EditTextPreference import androidx.preference.EditTextPreference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import com.rtbishop.look4sat.R import com.rtbishop.look4sat.R
import com.rtbishop.look4sat.ui.MainActivity import com.rtbishop.look4sat.ui.MainActivity
import com.rtbishop.look4sat.ui.SharedViewModel import com.rtbishop.look4sat.ui.SharedViewModel
import com.rtbishop.look4sat.utility.GeneralUtils.toast
class PreferencesFragment : PreferenceFragmentCompat(), class PreferencesFragment : PreferenceFragmentCompat(),
SharedPreferences.OnSharedPreferenceChangeListener { SharedPreferences.OnSharedPreferenceChangeListener {
@@ -54,21 +54,15 @@ class PreferencesFragment : PreferenceFragmentCompat(),
findPreference<EditTextPreference>(keyLat)?.apply { findPreference<EditTextPreference>(keyLat)?.apply {
summaryProvider = provider summaryProvider = provider
setOnBindEditTextListener { setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER or it.inputType = InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED InputType.TYPE_NUMBER_FLAG_SIGNED
} }
setOnPreferenceChangeListener { _, newValue -> setOnPreferenceChangeListener { _, newValue ->
val valueStr = newValue.toString() val valueStr = newValue.toString()
if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -90.0 || valueStr.toDouble() > 90.0) { if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -90.0 || valueStr.toDouble() > 90.0) {
Toast.makeText( getString(R.string.pref_lat_input_error).toast(mainActivity)
mainActivity,
getString(R.string.pref_lat_input_error),
Toast.LENGTH_SHORT
).show()
return@setOnPreferenceChangeListener false return@setOnPreferenceChangeListener false
} }
return@setOnPreferenceChangeListener true return@setOnPreferenceChangeListener true
@@ -77,61 +71,45 @@ class PreferencesFragment : PreferenceFragmentCompat(),
findPreference<EditTextPreference>(keyLon)?.apply { findPreference<EditTextPreference>(keyLon)?.apply {
summaryProvider = provider summaryProvider = provider
setOnBindEditTextListener { setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER or it.inputType = InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED InputType.TYPE_NUMBER_FLAG_SIGNED
} }
setOnPreferenceChangeListener { _, newValue -> setOnPreferenceChangeListener { _, newValue ->
val valueStr = newValue.toString() val valueStr = newValue.toString()
if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -180.0 || valueStr.toDouble() > 180.0) { if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -180.0 || valueStr.toDouble() > 180.0) {
Toast.makeText( getString(R.string.pref_lon_input_error).toast(mainActivity)
mainActivity,
getString(R.string.pref_lon_input_error),
Toast.LENGTH_SHORT
).show()
return@setOnPreferenceChangeListener false return@setOnPreferenceChangeListener false
} }
return@setOnPreferenceChangeListener true return@setOnPreferenceChangeListener true
} }
} }
findPreference<EditTextPreference>(keyAlt)?.apply { findPreference<EditTextPreference>(keyAlt)?.apply {
summaryProvider = provider summaryProvider = provider
setOnBindEditTextListener { setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER or it.inputType = InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED InputType.TYPE_NUMBER_FLAG_SIGNED
} }
setOnPreferenceChangeListener { _, newValue -> setOnPreferenceChangeListener { _, newValue ->
val valueStr = newValue.toString() val valueStr = newValue.toString()
if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -413.0 || valueStr.toDouble() > 8850.0) { if (valueStr.isEmpty() || valueStr == "-" || valueStr.toDouble() < -413.0 || valueStr.toDouble() > 8850.0) {
Toast.makeText( getString(R.string.pref_alt_input_error).toast(mainActivity)
mainActivity,
getString(R.string.pref_alt_input_error),
Toast.LENGTH_SHORT
).show()
return@setOnPreferenceChangeListener false return@setOnPreferenceChangeListener false
} }
return@setOnPreferenceChangeListener true return@setOnPreferenceChangeListener true
} }
} }
findPreference<EditTextPreference>(keyDelay)?.apply { findPreference<EditTextPreference>(keyDelay)?.apply {
summaryProvider = provider summaryProvider = provider
setOnBindEditTextListener { it.inputType = InputType.TYPE_CLASS_NUMBER } setOnBindEditTextListener { it.inputType = InputType.TYPE_CLASS_NUMBER }
setOnPreferenceChangeListener { _, newValue -> setOnPreferenceChangeListener { _, newValue ->
val valueStr = newValue.toString() val valueStr = newValue.toString()
if (valueStr.isEmpty() || valueStr.toLong() < 250 || valueStr.toLong() > 10000) { if (valueStr.isEmpty() || valueStr.toLong() < 250 || valueStr.toLong() > 10000) {
Toast.makeText( getString(R.string.pref_refresh_rate_input_error).toast(mainActivity)
mainActivity,
getString(R.string.pref_refresh_rate_input_error),
Toast.LENGTH_SHORT
).show()
return@setOnPreferenceChangeListener false return@setOnPreferenceChangeListener false
} }
return@setOnPreferenceChangeListener true return@setOnPreferenceChangeListener true
@@ -1,5 +1,7 @@
package com.rtbishop.look4sat.utility package com.rtbishop.look4sat.utility
import android.content.Context
import android.widget.Toast
import java.util.* import java.util.*
import kotlin.math.cos import kotlin.math.cos
import kotlin.math.sin import kotlin.math.sin
@@ -25,4 +27,8 @@ object GeneralUtils {
val radius = r * (piDiv2 - elevation) / piDiv2 val radius = r * (piDiv2 - elevation) / piDiv2
return (radius * sin(piDiv2 - azimuth)).toFloat() return (radius * sin(piDiv2 - azimuth)).toFloat()
} }
fun Any.toast(context: Context, duration: Int = Toast.LENGTH_SHORT): Toast {
return Toast.makeText(context, this.toString(), duration).apply { show() }
}
} }
+3 -3
View File
@@ -6,7 +6,7 @@
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<TextView <TextView
android:id="@+id/dialotTleTitle" android:id="@+id/dialogTleTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
@@ -24,8 +24,8 @@
android:importantForAutofill="no" android:importantForAutofill="no"
android:inputType="textWebEditText" android:inputType="textWebEditText"
app:layout_constraintEnd_toEndOf="@+id/dialogTlePos" app:layout_constraintEnd_toEndOf="@+id/dialogTlePos"
app:layout_constraintStart_toStartOf="@+id/dialotTleTitle" app:layout_constraintStart_toStartOf="@+id/dialogTleTitle"
app:layout_constraintTop_toBottomOf="@+id/dialotTleTitle" /> app:layout_constraintTop_toBottomOf="@+id/dialogTleTitle" />
<TextView <TextView
android:id="@+id/dialogTleNeg" android:id="@+id/dialogTleNeg"