crash fixes

This commit is contained in:
Mariotaku Lee 2017-04-03 01:11:37 +08:00
parent c914564712
commit 7c6e75d8ef
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
4 changed files with 24 additions and 14 deletions

View File

@ -9,13 +9,14 @@ import android.os.Handler
*/ */
fun AccountManager.addOnAccountsUpdatedListenerSafe(listener: OnAccountsUpdateListener, fun AccountManager.addOnAccountsUpdatedListenerSafe(listener: OnAccountsUpdateListener,
handler: Handler? = null, handler: Handler? = null, updateImmediately: Boolean = false): Boolean {
updateImmediately: Boolean = false): Boolean {
try { try {
this.addOnAccountsUpdatedListener(listener, handler, updateImmediately) this.addOnAccountsUpdatedListener(listener, handler, updateImmediately)
return true return true
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
return false return false
} catch (e: IllegalArgumentException) {
return false
} }
} }
@ -25,5 +26,7 @@ fun AccountManager.removeOnAccountsUpdatedListenerSafe(listener: OnAccountsUpdat
return true return true
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
return false return false
} catch (e: IllegalArgumentException) {
return false
} }
} }

View File

@ -16,9 +16,17 @@ class AccountDetailsLoader(
context: Context, context: Context,
val filter: (AccountDetails.() -> Boolean)? = null val filter: (AccountDetails.() -> Boolean)? = null
) : FixedAsyncTaskLoader<List<AccountDetails>>(context) { ) : FixedAsyncTaskLoader<List<AccountDetails>>(context) {
private val am: AccountManager = AccountManager.get(context) private val am: AccountManager = AccountManager.get(context)
private val accountUpdateListener = OnAccountsUpdateListener {
onContentChanged() private var accountUpdateListener: OnAccountsUpdateListener? = null
set(value) {
field?.let {
am.removeOnAccountsUpdatedListenerSafe(it)
}
if (value != null) {
am.addOnAccountsUpdatedListenerSafe(value, updateImmediately = true)
}
} }
override fun loadInBackground(): List<AccountDetails> { override fun loadInBackground(): List<AccountDetails> {
@ -30,11 +38,13 @@ class AccountDetailsLoader(
override fun onReset() { override fun onReset() {
super.onReset() super.onReset()
onStopLoading() onStopLoading()
am.removeOnAccountsUpdatedListenerSafe(accountUpdateListener) accountUpdateListener = null
} }
override fun onStartLoading() { override fun onStartLoading() {
am.addOnAccountsUpdatedListenerSafe(accountUpdateListener, updateImmediately = true) accountUpdateListener = OnAccountsUpdateListener {
onContentChanged()
}
if (takeContentChanged()) { if (takeContentChanged()) {
forceLoad() forceLoad()
} }

View File

@ -63,13 +63,9 @@ class AccountViewHolder(
name.text = details.user.name name.text = details.user.name
screenName.text = String.format("@%s", details.user.screen_name) screenName.text = String.format("@%s", details.user.screen_name)
setAccountColor(details.color) setAccountColor(details.color)
if (adapter.profileImageEnabled) {
profileImage.visibility = View.VISIBLE profileImage.visibility = View.VISIBLE
adapter.requestManager.loadProfileImage(adapter.context, details, adapter.profileImageStyle, adapter.requestManager.loadProfileImage(adapter.context, details, adapter.profileImageStyle,
profileImage.cornerRadius, profileImage.cornerRadiusRatio).into(profileImage) profileImage.cornerRadius, profileImage.cornerRadiusRatio).into(profileImage)
} else {
profileImage.visibility = View.GONE
}
accountType.setImageResource(AccountUtils.getAccountTypeIcon(details.type)) accountType.setImageResource(AccountUtils.getAccountTypeIcon(details.type))
toggle.isChecked = details.activated toggle.isChecked = details.activated
toggle.setOnCheckedChangeListener(adapter.checkedChangeListener) toggle.setOnCheckedChangeListener(adapter.checkedChangeListener)

View File

@ -1205,6 +1205,7 @@
<string name="title_statuses">Tweets</string> <string name="title_statuses">Tweets</string>
<string name="title_statuses_and_replies">Tweets &amp; replies</string> <string name="title_statuses_and_replies">Tweets &amp; replies</string>
<string name="title_statuses_scheduling">Tweets scheduling</string> <string name="title_statuses_scheduling">Tweets scheduling</string>
<string name="title_gif_sharing">GIF sharing</string>
<string name="title_streaming">Streaming</string> <string name="title_streaming">Streaming</string>
<string name="title_subscription_name">Name</string> <string name="title_subscription_name">Name</string>
<string name="title_subscription_url">URL</string> <string name="title_subscription_url">URL</string>