Fix not attached fragment (#337)

* Add condition for error dialog
* Add safe call of forEach in LuckyNumberWidgetProvider
This commit is contained in:
Rafał Borcz 2019-05-09 22:06:11 +02:00 committed by Mikołaj Pich
parent a191f03cdf
commit 103ab95c80
3 changed files with 26 additions and 13 deletions

View File

@ -2,6 +2,7 @@ package io.github.wulkanowy.ui.base
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.fragment.app.Fragment
@ -25,7 +26,7 @@ abstract class BaseActivity : AppCompatActivity(), BaseView, HasSupportFragmentI
@Inject
lateinit var themeManager: ThemeManager
protected lateinit var messageContainer: View
protected var messageContainer: View? = null
public override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
@ -36,13 +37,18 @@ abstract class BaseActivity : AppCompatActivity(), BaseView, HasSupportFragmentI
}
override fun showError(text: String, error: Throwable) {
Snackbar.make(messageContainer, text, LENGTH_LONG).setAction(R.string.all_details) {
if (messageContainer != null) {
Snackbar.make(messageContainer!!, text, LENGTH_LONG)
.setAction(R.string.all_details) {
ErrorDialog.newInstance(error).show(supportFragmentManager, error.toString())
}.show()
}
.show()
} else showMessage(text)
}
override fun showMessage(text: String) {
Snackbar.make(messageContainer, text, LENGTH_LONG).show()
if (messageContainer != null) Snackbar.make(messageContainer!!, text, LENGTH_LONG).show()
else Toast.makeText(this, text, Toast.LENGTH_LONG).show()
}
override fun onDestroy() {

View File

@ -2,6 +2,7 @@ package io.github.wulkanowy.ui.base
import android.view.View
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.snackbar.Snackbar.LENGTH_LONG
import dagger.android.support.DaggerFragment
import io.github.wulkanowy.R
@ -10,16 +11,22 @@ abstract class BaseFragment : DaggerFragment(), BaseView {
protected var messageContainer: View? = null
override fun showError(text: String, error: Throwable) {
if (messageContainer == null) (activity as? BaseActivity)?.showError(text, error)
else messageContainer?.also {
Snackbar.make(it, text, Snackbar.LENGTH_LONG).setAction(R.string.all_details) {
ErrorDialog.newInstance(error).show(childFragmentManager, error.toString())
}.show()
if (messageContainer != null) {
Snackbar.make(messageContainer!!, text, LENGTH_LONG)
.setAction(R.string.all_details) {
if (isAdded) ErrorDialog.newInstance(error).show(childFragmentManager, error.toString())
}
.show()
} else {
(activity as? BaseActivity)?.showError(text, error)
}
}
override fun showMessage(text: String) {
if (messageContainer == null) (activity as? BaseActivity)?.showMessage(text)
else messageContainer?.also { Snackbar.make(it, text, Snackbar.LENGTH_LONG).show() }
if (messageContainer != null) {
Snackbar.make(messageContainer!!, text, LENGTH_LONG).show()
} else {
(activity as? BaseActivity)?.showMessage(text)
}
}
}

View File

@ -68,7 +68,7 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() {
}
private fun onUpdate(context: Context, intent: Intent) {
intent.getIntArrayExtra(EXTRA_APPWIDGET_IDS).forEach { appWidgetId ->
intent.getIntArrayExtra(EXTRA_APPWIDGET_IDS)?.forEach { appWidgetId ->
RemoteViews(context.packageName, R.layout.widget_luckynumber).apply {
setTextViewText(R.id.luckyNumberWidgetNumber,
getLuckyNumber(sharedPref.getLong(getStudentWidgetKey(appWidgetId), 0), appWidgetId)?.luckyNumber?.toString() ?: "#"