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,
handler: Handler? = null,
updateImmediately: Boolean = false): Boolean {
handler: Handler? = null, updateImmediately: Boolean = false): Boolean {
try {
this.addOnAccountsUpdatedListener(listener, handler, updateImmediately)
return true
} catch (e: IllegalStateException) {
return false
} catch (e: IllegalArgumentException) {
return false
}
}
@ -25,5 +26,7 @@ fun AccountManager.removeOnAccountsUpdatedListenerSafe(listener: OnAccountsUpdat
return true
} catch (e: IllegalStateException) {
return false
} catch (e: IllegalArgumentException) {
return false
}
}

View File

@ -16,10 +16,18 @@ class AccountDetailsLoader(
context: Context,
val filter: (AccountDetails.() -> Boolean)? = null
) : FixedAsyncTaskLoader<List<AccountDetails>>(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> {
return AccountUtils.getAllAccountDetails(am, true).filter {
@ -30,11 +38,13 @@ class AccountDetailsLoader(
override fun onReset() {
super.onReset()
onStopLoading()
am.removeOnAccountsUpdatedListenerSafe(accountUpdateListener)
accountUpdateListener = null
}
override fun onStartLoading() {
am.addOnAccountsUpdatedListenerSafe(accountUpdateListener, updateImmediately = true)
accountUpdateListener = OnAccountsUpdateListener {
onContentChanged()
}
if (takeContentChanged()) {
forceLoad()
}

View File

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

View File

@ -1205,6 +1205,7 @@
<string name="title_statuses">Tweets</string>
<string name="title_statuses_and_replies">Tweets &amp; replies</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_subscription_name">Name</string>
<string name="title_subscription_url">URL</string>