fixed crashes

This commit is contained in:
Mariotaku Lee 2017-04-27 14:53:06 +08:00
parent 8283c03bdb
commit bab999a151
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
4 changed files with 17 additions and 5 deletions

View File

@ -1,5 +1,6 @@
package org.mariotaku.ktextension
import android.content.BroadcastReceiver
import android.content.Context
import android.content.pm.PackageManager
import android.support.v4.content.ContextCompat
@ -15,3 +16,13 @@ fun Context.checkAllSelfPermissionsGranted(vararg permissions: String): Boolean
fun Context.checkAnySelfPermissionsGranted(vararg permissions: String): Boolean {
return permissions.any { ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED }
}
fun Context.unregisterReceiverSafe(receiver: BroadcastReceiver?): Boolean {
if (receiver == null) return false
try {
unregisterReceiver(receiver)
return true
} catch (e: IllegalArgumentException) {
return false
}
}

View File

@ -44,6 +44,7 @@ import org.mariotaku.chameleon.Chameleon
import org.mariotaku.chameleon.ChameleonActivity
import org.mariotaku.kpreferences.KPreferences
import org.mariotaku.kpreferences.get
import org.mariotaku.ktextension.unregisterReceiverSafe
import org.mariotaku.restfu.http.RestHttpClient
import org.mariotaku.twidere.BuildConfig
import org.mariotaku.twidere.TwidereConstants.SHARED_PREFERENCES_NAME
@ -272,7 +273,7 @@ open class BaseActivity : ChameleonActivity(), IBaseActivity<BaseActivity>, IThe
override fun onPause() {
unregisterReceiver(nightTimeChangedReceiver)
unregisterReceiverSafe(nightTimeChangedReceiver)
val adapter = NfcAdapter.getDefaultAdapter(this)
if (adapter != null && adapter.isEnabled) {

View File

@ -76,15 +76,15 @@ private fun MicroBlog.showUserAlternative(id: String?, screenName: String?): Use
}
val paging = Paging().count(1)
val users = searchUsers(searchScreenName, paging)
val match = users.first { it.id == id || searchScreenName.equals(it.screenName, ignoreCase = true) }
val match = users.firstOrNull { it.id == id || searchScreenName.equals(it.screenName, ignoreCase = true) }
if (match != null) return match
if (id != null) {
val timeline = getUserTimeline(id, paging, null)
val status = timeline.first { it.user?.id == id }
val status = timeline.firstOrNull { it.user?.id == id }
if (status != null) return status.user
} else {
val timeline = getUserTimelineByScreenName(searchScreenName, paging, null)
val status = timeline.first { searchScreenName.equals(it.user?.screenName, ignoreCase = true) }
val status = timeline.firstOrNull { searchScreenName.equals(it.user?.screenName, ignoreCase = true) }
if (status != null) return status.user
}
throw MicroBlogException("Can't find user")

View File

@ -422,7 +422,7 @@ class GetMessagesTask(
conversation.conversation_avatar = v.avatarImageHttps
conversation.request_cursor = response.cursor
conversation.conversation_extras_type = ParcelableMessageConversation.ExtrasType.TWITTER_OFFICIAL
val myLastReadEventId = v.participants.first { it.userId == accountKey.id }?.lastReadEventId
val myLastReadEventId = v.participants.firstOrNull { it.userId == accountKey.id }?.lastReadEventId
// Find recent message timestamp
val myLastReadTimestamp = messagesMap.findLastReadTimestamp(k, myLastReadEventId)