diff --git a/twidere/src/.google.commit-id b/twidere/src/.google.commit-id index 813be3899..29dc646fe 100644 --- a/twidere/src/.google.commit-id +++ b/twidere/src/.google.commit-id @@ -1 +1 @@ -27572907b9df93dc524c921ad30e2bfe1e2ec75c +64f8fa2e8644a79a84902f7a32cdf7b4b4d8e674 diff --git a/twidere/src/main/kotlin/org/mariotaku/ktextension/NumberExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/ktextension/NumberExtensions.kt index e0f50a898..cccc8d225 100644 --- a/twidere/src/main/kotlin/org/mariotaku/ktextension/NumberExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/ktextension/NumberExtensions.kt @@ -4,7 +4,7 @@ package org.mariotaku.ktextension * Created by mariotaku on 16/7/30. */ -fun String?.toLong(def: Long): Long { +fun String?.toLongOr(def: Long): Long { try { return this?.toLong() ?: def } catch (e: NumberFormatException) { @@ -12,7 +12,7 @@ fun String?.toLong(def: Long): Long { } } -fun String?.toInt(def: Int): Int { +fun String?.toIntOr(def: Int): Int { try { return this?.toInt() ?: def } catch (e: NumberFormatException) { @@ -20,7 +20,7 @@ fun String?.toInt(def: Int): Int { } } -fun String?.toDouble(def: Double): Double { +fun String?.toDoubleOr(def: Double): Double { try { return this?.toDouble() ?: def } catch (e: NumberFormatException) { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt index f1c973ab5..ce098d5fd 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt @@ -60,7 +60,6 @@ import com.bumptech.glide.Glide import com.twitter.Extractor import com.twitter.Validator import kotlinx.android.synthetic.main.activity_compose.* -import org.apache.commons.lang3.ArrayUtils import org.mariotaku.abstask.library.AbstractTask import org.mariotaku.abstask.library.TaskStarter import org.mariotaku.kpreferences.get @@ -254,7 +253,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener } setLabel(intent) val selectedAccountIds = accountsAdapter.selectedAccountKeys - if (ArrayUtils.isEmpty(selectedAccountIds)) { + if (selectedAccountIds.isNullOrEmpty()) { val idsInPrefs: Array = kPreferences[composeAccountsKey] ?: emptyArray() val intersection: Array = defaultAccountIds.intersect(listOf(*idsInPrefs)).toTypedArray() @@ -1194,11 +1193,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener private fun notifyAccountSelectionChanged() { displaySelectedAccountsIcon() val accounts = accountsAdapter.selectedAccounts - if (ArrayUtils.isEmpty(accounts)) { - editText.accountKey = Utils.getDefaultAccountKey(this) - } else { - editText.accountKey = accounts[0].key - } + editText.accountKey = accounts.firstOrNull()?.key ?: Utils.getDefaultAccountKey(this) statusTextCount.maxLength = accounts.textLimit setMenu() } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/LinkHandlerActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/LinkHandlerActivity.kt index 8feef5154..d29a9d4b6 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/LinkHandlerActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/LinkHandlerActivity.kt @@ -42,7 +42,7 @@ import android.view.Window import kotlinx.android.synthetic.main.activity_link_handler.* import org.mariotaku.kpreferences.get import org.mariotaku.ktextension.set -import org.mariotaku.ktextension.toDouble +import org.mariotaku.ktextension.toDoubleOr import org.mariotaku.twidere.Constants.* import org.mariotaku.twidere.R import org.mariotaku.twidere.activity.iface.IControlBarActivity @@ -559,8 +559,8 @@ class LinkHandlerActivity : BaseActivity(), SystemWindowsInsetsCallback, IContro LINK_ID_MAP -> { accountRequired = false if (!args.containsKey(EXTRA_LATITUDE) && !args.containsKey(EXTRA_LONGITUDE)) { - val lat = uri.getQueryParameter(QUERY_PARAM_LAT).toDouble(Double.NaN) - val lng = uri.getQueryParameter(QUERY_PARAM_LNG).toDouble(Double.NaN) + val lat = uri.getQueryParameter(QUERY_PARAM_LAT).toDoubleOr(Double.NaN) + val lng = uri.getQueryParameter(QUERY_PARAM_LNG).toDoubleOr(Double.NaN) if (lat.isNaN() || lng.isNaN()) return null args.putDouble(EXTRA_LATITUDE, lat) args.putDouble(EXTRA_LONGITUDE, lng) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt index 4acffb695..cfe3fa5aa 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt @@ -49,7 +49,6 @@ import android.view.View.OnClickListener import android.widget.EditText import android.widget.TextView import android.widget.Toast -import com.bluelinelabs.logansquare.LoganSquare import kotlinx.android.synthetic.main.activity_sign_in.* import nl.komponents.kovenant.combine.and import nl.komponents.kovenant.task diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/WebLinkHandlerActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/WebLinkHandlerActivity.kt index cebcde45c..f8ea7c588 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/WebLinkHandlerActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/WebLinkHandlerActivity.kt @@ -5,8 +5,7 @@ import android.content.Intent import android.net.Uri import android.os.Bundle import android.text.TextUtils -import org.apache.commons.lang3.ArrayUtils -import org.mariotaku.ktextension.toLong +import org.mariotaku.ktextension.toLongOr import org.mariotaku.twidere.R import org.mariotaku.twidere.TwidereConstants.* import org.mariotaku.twidere.app.TwidereApplication @@ -167,7 +166,7 @@ class WebLinkHandlerActivity : Activity() { return Pair(Intent(Intent.ACTION_VIEW, builder.build()), true) } else -> { - if (ArrayUtils.contains(TWITTER_RESERVED_PATHS, pathSegments[0])) { + if (pathSegments[0] in TWITTER_RESERVED_PATHS) { return Pair(null, true) } return handleUserSpecificPageIntent(uri, pathSegments, pathSegments[0]) @@ -223,7 +222,7 @@ class WebLinkHandlerActivity : Activity() { } } } else if (segsSize >= 3) { - if ("status" == pathSegments[1] && pathSegments[2].toLong(-1) != -1L) { + if ("status" == pathSegments[1] && pathSegments[2].toLongOr(-1L) != -1L) { val builder = Uri.Builder() builder.scheme(SCHEME_TWIDERE) builder.authority(AUTHORITY_STATUS) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/AccountSelectorAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/AccountSelectorAdapter.kt index 26715d704..3b9feee9c 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/AccountSelectorAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/AccountSelectorAdapter.kt @@ -27,9 +27,9 @@ import org.mariotaku.kpreferences.get import org.mariotaku.twidere.R import org.mariotaku.twidere.constant.profileImageStyleKey import org.mariotaku.twidere.fragment.AccountsDashboardFragment -import org.mariotaku.twidere.view.transformer.AccountsSelectorTransformer import org.mariotaku.twidere.model.AccountDetails import org.mariotaku.twidere.view.holder.AccountProfileImageViewHolder +import org.mariotaku.twidere.view.transformer.AccountsSelectorTransformer import java.util.* class AccountSelectorAdapter( diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableActivitiesAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableActivitiesAdapter.kt index cec744242..ecdfdb98f 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableActivitiesAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableActivitiesAdapter.kt @@ -28,11 +28,7 @@ import android.view.View import android.view.ViewGroup import android.widget.TextView import com.bumptech.glide.RequestManager -import org.apache.commons.lang3.ArrayUtils -import org.mariotaku.ktextension.contains -import org.mariotaku.ktextension.rangeOfSize -import org.mariotaku.ktextension.safeGetLong -import org.mariotaku.ktextension.safeMoveToPosition +import org.mariotaku.ktextension.* import org.mariotaku.library.objectcursor.ObjectCursor import org.mariotaku.microblog.library.twitter.model.Activity import org.mariotaku.twidere.R @@ -272,19 +268,19 @@ class ParcelableActivitiesAdapter( val activity = getActivity(position, false) when (activity.action) { Activity.Action.MENTION -> { - if (ArrayUtils.isEmpty(activity.target_object_statuses)) { + if (activity.target_object_statuses.isNullOrEmpty()) { return ITEM_VIEW_TYPE_STUB } return ITEM_VIEW_TYPE_STATUS } Activity.Action.REPLY -> { - if (ArrayUtils.isEmpty(activity.target_statuses)) { + if (activity.target_statuses.isNullOrEmpty()) { return ITEM_VIEW_TYPE_STUB } return ITEM_VIEW_TYPE_STATUS } Activity.Action.QUOTE -> { - if (ArrayUtils.isEmpty(activity.target_statuses)) { + if (activity.target_statuses.isNullOrEmpty()) { return ITEM_VIEW_TYPE_STUB } return ITEM_VIEW_TYPE_STATUS diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/app/TwidereApplication.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/app/TwidereApplication.kt index aa8bb6e6e..58c1d8613 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/app/TwidereApplication.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/app/TwidereApplication.kt @@ -34,7 +34,6 @@ import nl.komponents.kovenant.android.startKovenant import nl.komponents.kovenant.android.stopKovenant import nl.komponents.kovenant.task import okhttp3.Dns -import org.apache.commons.lang3.ArrayUtils import org.mariotaku.kpreferences.KPreferences import org.mariotaku.kpreferences.get import org.mariotaku.kpreferences.set @@ -244,7 +243,7 @@ class TwidereApplication : Application(), Constants, OnSharedPreferenceChangeLis val uid = intent.getIntExtra(Intent.EXTRA_UID, -1) val packages = packageManager.getPackagesForUid(uid) val manager = externalThemeManager - if (ArrayUtils.contains(packages, manager.emojiPackageName)) { + if (manager.emojiPackageName in packages) { manager.reloadEmojiPreferences() } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/constant/PreferenceKeys.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/constant/PreferenceKeys.kt index ac4b7f577..91d4cca88 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/constant/PreferenceKeys.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/constant/PreferenceKeys.kt @@ -7,7 +7,7 @@ import android.text.TextUtils import org.apache.commons.lang3.LocaleUtils import org.mariotaku.kpreferences.* import org.mariotaku.ktextension.bcp47Tag -import org.mariotaku.ktextension.toLong +import org.mariotaku.ktextension.toLongOr import org.mariotaku.twidere.BuildConfig import org.mariotaku.twidere.Constants.* import org.mariotaku.twidere.TwidereConstants.KEY_MEDIA_PRELOAD @@ -160,7 +160,7 @@ object linkHighlightOptionKey : KSimpleKey(KEY_LINK_HIGHLIGHT_OPTION, VALUE object refreshIntervalKey : KSimpleKey(KEY_REFRESH_INTERVAL, 15) { override fun read(preferences: SharedPreferences): Long { - return preferences.getString(key, null).toLong(def) + return preferences.getString(key, null).toLongOr(def) } override fun write(editor: SharedPreferences.Editor, value: Long): Boolean { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extensions/StaggeredGridLayoutManagerExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/StaggeredGridLayoutManagerExtensions.kt similarity index 97% rename from twidere/src/main/kotlin/org/mariotaku/twidere/extensions/StaggeredGridLayoutManagerExtensions.kt rename to twidere/src/main/kotlin/org/mariotaku/twidere/extension/StaggeredGridLayoutManagerExtensions.kt index 618182c66..cdabef7a7 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extensions/StaggeredGridLayoutManagerExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/StaggeredGridLayoutManagerExtensions.kt @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package org.mariotaku.twidere.extensions +package org.mariotaku.twidere.extension import android.support.v7.widget.RecyclerView import android.support.v7.widget.StaggeredGridLayoutManager diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/AccountExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/AccountExtensions.kt index f15c12d51..67456e730 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/AccountExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/AccountExtensions.kt @@ -8,7 +8,7 @@ import android.support.annotation.RequiresApi import android.text.TextUtils import org.mariotaku.ktextension.HexColorFormat import org.mariotaku.ktextension.toHexColor -import org.mariotaku.ktextension.toInt +import org.mariotaku.ktextension.toIntOr import org.mariotaku.twidere.TwidereConstants.* import org.mariotaku.twidere.annotation.AccountType import org.mariotaku.twidere.model.ParcelableUser @@ -71,7 +71,7 @@ fun Account.getColor(am: AccountManager): Int { } fun Account.getPosition(am: AccountManager): Int { - return AccountDataQueue.getUserData(am, this, ACCOUNT_USER_DATA_POSITION).toInt(-1) + return AccountDataQueue.getUserData(am, this, ACCOUNT_USER_DATA_POSITION).toIntOr(-1) } fun Account.getAccountExtras(am: AccountManager): AccountExtras? { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/DraftExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/DraftExtensions.kt index 8ed039fec..cb6f1c6d5 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/DraftExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/DraftExtensions.kt @@ -14,7 +14,7 @@ import org.apache.james.mime4j.stream.BodyDescriptor import org.apache.james.mime4j.stream.MimeConfig import org.apache.james.mime4j.stream.RawField import org.apache.james.mime4j.util.MimeUtil -import org.mariotaku.ktextension.toInt +import org.mariotaku.ktextension.toIntOr import org.mariotaku.ktextension.toString import org.mariotaku.twidere.R import org.mariotaku.twidere.model.* @@ -231,7 +231,7 @@ private class BodyPartHandler(private val context: Context, private val draft: D val mediaFile = File(context.filesDir, filename) media = ParcelableMediaUpdate().apply { bd.transferEncoding - this.type = contentType?.getParameter("media_type").toInt(ParcelableMedia.Type.UNKNOWN) + this.type = contentType?.getParameter("media_type").toIntOr(ParcelableMedia.Type.UNKNOWN) this.alt_text = contentType?.getParameter("alt_text") FileOutputStream(mediaFile).use { st.copyTo(it) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableCardEntityExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableCardEntityExtensions.kt new file mode 100644 index 000000000..dff8a43fd --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableCardEntityExtensions.kt @@ -0,0 +1,76 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.extension.model + +import org.mariotaku.ktextension.toIntOr +import org.mariotaku.ktextension.toLongOr +import org.mariotaku.microblog.library.twitter.model.CardEntity +import org.mariotaku.twidere.model.ParcelableCardEntity +import org.mariotaku.twidere.model.UserKey +import org.mariotaku.twidere.model.util.ParcelableCardEntityUtils +import java.text.ParseException +import java.util.* + + +fun CardEntity.toParcelable(accountKey: UserKey, accountType: String): ParcelableCardEntity? { + val obj = ParcelableCardEntity() + obj.name = name + obj.url = url + obj.users = users?.toParcelables(accountKey, accountType) + obj.account_key = accountKey + obj.values = bindingValues?.mapValues { entry -> + ParcelableCardEntity.ParcelableBindingValue(entry.value) + } + return obj +} + +fun ParcelableCardEntity.getAsBoolean(key: String, def: Boolean): Boolean { + val value = getValue(key) ?: return def + return value.value.toBoolean() +} + +fun ParcelableCardEntity.getAsString(key: String, def: String?): String? { + return getValue(key)?.value ?: return def +} + +fun ParcelableCardEntity.getString(key: String): String? { + val value = getValue(key) ?: return null + if (CardEntity.BindingValue.TYPE_STRING != value.type) return null + return value.value +} + +fun ParcelableCardEntity.getAsInteger(key: String, def: Int): Int { + val value = getValue(key) ?: return def + return value.value.toIntOr(def) +} + +fun ParcelableCardEntity.getAsLong(key: String, def: Long): Long { + val value = getValue(key) ?: return def + return value.value.toLongOr(def) +} + +fun ParcelableCardEntity.getAsDate(key: String, def: Date): Date { + val value = getValue(key) ?: return def + try { + return ParcelableCardEntityUtils.sISOFormat.parse(value.value) + } catch (e: ParseException) { + return def + } +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableStatusExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableStatusExtensions.kt index c84d39ed7..fefca8c27 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableStatusExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableStatusExtensions.kt @@ -1,7 +1,10 @@ package org.mariotaku.twidere.extension.model +import org.mariotaku.microblog.library.twitter.model.Status import org.mariotaku.twidere.model.ParcelableStatus import org.mariotaku.twidere.model.ParcelableUser +import org.mariotaku.twidere.model.UserKey +import org.mariotaku.twidere.model.util.ParcelableStatusUtils /** * Created by mariotaku on 2017/1/7. @@ -29,3 +32,8 @@ val ParcelableStatus.referenced_users: Array } return resultList.toTypedArray() } + + +fun Array.toParcelables(accountKey: UserKey, accountType: String, profileImageSize: String) = Array(size) { i -> + ParcelableStatusUtils.fromStatus(this[i], accountKey, accountType, false, profileImageSize) +} \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableUserExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableUserExtensions.kt index edc090ec8..ba051365f 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableUserExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableUserExtensions.kt @@ -19,8 +19,11 @@ package org.mariotaku.twidere.extension.model +import org.mariotaku.microblog.library.twitter.model.User import org.mariotaku.twidere.TwidereConstants.USER_TYPE_FANFOU_COM import org.mariotaku.twidere.model.ParcelableUser +import org.mariotaku.twidere.model.UserKey +import org.mariotaku.twidere.model.util.ParcelableUserUtils import org.mariotaku.twidere.util.InternalTwitterContentUtils fun ParcelableUser.getBestProfileBanner(width: Int): String? { @@ -33,4 +36,11 @@ fun ParcelableUser.getBestProfileBanner(width: Int): String? { } } -val ParcelableUser.urlPreferred: String? get() = url_expanded?.takeIf(String::isNotEmpty) ?: url \ No newline at end of file +val ParcelableUser.urlPreferred: String? get() = url_expanded?.takeIf(String::isNotEmpty) ?: url + + +fun Array.toParcelables(accountKey: UserKey, accountType: String, profileImageSize: String = "normal"): Array? { + return map { + ParcelableUserUtils.fromUser(it, accountKey, accountType, profileImageSize = profileImageSize) + }.toTypedArray() +} \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableUserListExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableUserListExtensions.kt new file mode 100644 index 000000000..d102a6a0a --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableUserListExtensions.kt @@ -0,0 +1,32 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.extension.model + +import org.mariotaku.microblog.library.twitter.model.UserList +import org.mariotaku.twidere.model.UserKey +import org.mariotaku.twidere.model.util.ParcelableUserListUtils + +/** + * Created by mariotaku on 2017/4/7. + */ + +fun Array.toParcelables(accountKey: UserKey, profileImageSize: String = "normal") = Array(size) { + ParcelableUserListUtils.from(this[it], accountKey, profileImageSize = profileImageSize) +} \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AbsStatusesFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AbsStatusesFragment.kt index 96739d1b4..06775943e 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AbsStatusesFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AbsStatusesFragment.kt @@ -35,8 +35,6 @@ import android.support.v7.widget.RecyclerView.OnScrollListener import android.view.* import com.bumptech.glide.Glide import com.squareup.otto.Subscribe -import edu.tsinghua.hotmobi.HotMobiLogger -import edu.tsinghua.hotmobi.model.MediaEvent import kotlinx.android.synthetic.main.fragment_content_recyclerview.* import org.mariotaku.kpreferences.get import org.mariotaku.ktextension.* @@ -48,9 +46,12 @@ import org.mariotaku.twidere.adapter.decorator.ExtendedDividerItemDecoration import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter import org.mariotaku.twidere.annotation.ReadPositionTag import org.mariotaku.twidere.annotation.Referral -import org.mariotaku.twidere.constant.* import org.mariotaku.twidere.constant.IntentConstants.* import org.mariotaku.twidere.constant.KeyboardShortcutConstants.* +import org.mariotaku.twidere.constant.displaySensitiveContentsKey +import org.mariotaku.twidere.constant.newDocumentApiKey +import org.mariotaku.twidere.constant.readFromBottomKey +import org.mariotaku.twidere.constant.rememberPositionKey import org.mariotaku.twidere.extension.model.getAccountType import org.mariotaku.twidere.graphic.like.LikeAnimationDrawable import org.mariotaku.twidere.loader.iface.IExtendedLoader diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt index a1ed55abb..fe7b0d458 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt @@ -34,8 +34,7 @@ import kotlinx.android.synthetic.main.fragment_content_listview.* import org.mariotaku.kpreferences.get import org.mariotaku.sqliteqb.library.Expression import org.mariotaku.twidere.R -import org.mariotaku.twidere.TwidereConstants -import org.mariotaku.twidere.TwidereConstants.* +import org.mariotaku.twidere.TwidereConstants.EXTRA_ACCOUNT_KEY import org.mariotaku.twidere.activity.QuickSearchBarActivity import org.mariotaku.twidere.adapter.TrendsAdapter import org.mariotaku.twidere.constant.IntentConstants.EXTRA_EXTRAS @@ -46,7 +45,6 @@ import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.model.event.TrendsRefreshedEvent import org.mariotaku.twidere.model.tab.extra.TrendsTabExtras import org.mariotaku.twidere.provider.TwidereDataStore.CachedTrends -import org.mariotaku.twidere.util.IntentUtils import org.mariotaku.twidere.util.IntentUtils.openTweetSearch import org.mariotaku.twidere.util.Utils diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt index a96fef2d0..88c78718a 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt @@ -127,7 +127,6 @@ import org.mariotaku.twidere.provider.TwidereDataStore.CachedRelationships import org.mariotaku.twidere.provider.TwidereDataStore.CachedUsers import org.mariotaku.twidere.text.TwidereURLSpan import org.mariotaku.twidere.util.* -import org.mariotaku.twidere.util.InternalTwitterContentUtils.getBestBannerUrl import org.mariotaku.twidere.util.KeyboardShortcutsHandler.KeyboardShortcutCallback import org.mariotaku.twidere.util.TwidereLinkify.OnLinkClickListener import org.mariotaku.twidere.util.UserColorNameManager.UserColorChangedListener diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserListMembersFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserListMembersFragment.kt index 6b2023d58..6dcb059cd 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserListMembersFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserListMembersFragment.kt @@ -30,7 +30,6 @@ import kotlinx.android.synthetic.main.fragment_content_recyclerview.* import org.mariotaku.kpreferences.get import org.mariotaku.twidere.R import org.mariotaku.twidere.constant.IntentConstants.* -import org.mariotaku.twidere.constant.SharedPreferenceConstants.KEY_NAME_FIRST import org.mariotaku.twidere.constant.nameFirstKey import org.mariotaku.twidere.loader.CursorSupportUsersLoader import org.mariotaku.twidere.loader.UserListMembersLoader diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserMediaTimelineFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserMediaTimelineFragment.kt index 2319b50ae..e3178f502 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserMediaTimelineFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserMediaTimelineFragment.kt @@ -9,13 +9,11 @@ import android.support.v7.widget.RecyclerView import android.support.v7.widget.StaggeredGridLayoutManager import android.text.TextUtils import com.bumptech.glide.Glide -import org.mariotaku.kpreferences.get import org.mariotaku.twidere.adapter.StaggeredGridParcelableStatusesAdapter import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter import org.mariotaku.twidere.constant.IntentConstants.* -import org.mariotaku.twidere.constant.userTimelineFilterKey -import org.mariotaku.twidere.extensions.reachingEnd -import org.mariotaku.twidere.extensions.reachingStart +import org.mariotaku.twidere.extension.reachingEnd +import org.mariotaku.twidere.extension.reachingStart import org.mariotaku.twidere.loader.MediaTimelineLoader import org.mariotaku.twidere.loader.iface.IExtendedLoader import org.mariotaku.twidere.model.ParcelableStatus diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/MutesUsersLoader.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/MutesUsersLoader.kt index db2219880..03e711bd7 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/MutesUsersLoader.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/MutesUsersLoader.kt @@ -28,7 +28,6 @@ import org.mariotaku.microblog.library.twitter.model.User import org.mariotaku.twidere.model.AccountDetails import org.mariotaku.twidere.model.ParcelableUser import org.mariotaku.twidere.model.UserKey -import org.mariotaku.twidere.model.util.AccountUtils import org.mariotaku.twidere.util.DataStoreUtils class MutesUsersLoader( diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserBlocksLoader.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserBlocksLoader.kt index cf6e44662..01d88bfb7 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserBlocksLoader.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserBlocksLoader.kt @@ -20,7 +20,6 @@ package org.mariotaku.twidere.loader import android.content.Context - import org.mariotaku.microblog.library.MicroBlog import org.mariotaku.microblog.library.MicroBlogException import org.mariotaku.microblog.library.twitter.model.Paging @@ -29,7 +28,6 @@ import org.mariotaku.twidere.annotation.AccountType import org.mariotaku.twidere.model.AccountDetails import org.mariotaku.twidere.model.ParcelableUser import org.mariotaku.twidere.model.UserKey -import org.mariotaku.twidere.model.util.AccountUtils import org.mariotaku.twidere.util.DataStoreUtils class UserBlocksLoader( diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/tab/conf/TrendsLocationExtraConfiguration.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/tab/conf/TrendsLocationExtraConfiguration.kt index ad9ac354d..3f00e024d 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/tab/conf/TrendsLocationExtraConfiguration.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/tab/conf/TrendsLocationExtraConfiguration.kt @@ -13,7 +13,6 @@ import org.mariotaku.twidere.activity.TrendsLocationSelectorActivity import org.mariotaku.twidere.annotation.AccountType import org.mariotaku.twidere.constant.IntentConstants.EXTRA_ACCOUNT_KEY import org.mariotaku.twidere.constant.IntentConstants.EXTRA_LOCATION -import org.mariotaku.twidere.fragment.CustomTabsFragment import org.mariotaku.twidere.fragment.CustomTabsFragment.TabEditorDialogFragment import org.mariotaku.twidere.model.AccountDetails import org.mariotaku.twidere.model.tab.TabConfiguration diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableActivityUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableActivityUtils.kt index 197830a25..c06615baf 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableActivityUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableActivityUtils.kt @@ -1,6 +1,7 @@ package org.mariotaku.twidere.model.util import org.mariotaku.microblog.library.twitter.model.Activity +import org.mariotaku.twidere.extension.model.toParcelables import org.mariotaku.twidere.model.ParcelableActivity import org.mariotaku.twidere.model.ParcelableUser import org.mariotaku.twidere.model.UserKey @@ -67,26 +68,26 @@ object ParcelableActivityUtils { result.min_sort_position = activity.minSortPosition result.max_position = activity.maxPosition result.min_position = activity.minPosition - result.sources = ParcelableUserUtils.fromUsers(activity.sources, accountKey, accountType, + result.sources = activity.sources?.toParcelables(accountKey, accountType, profileImageSize) - result.target_users = ParcelableUserUtils.fromUsers(activity.targetUsers, accountKey, + result.target_users = activity.targetUsers?.toParcelables(accountKey, accountType, profileImageSize) - result.target_user_lists = ParcelableUserListUtils.fromUserLists(activity.targetUserLists, - accountKey, profileImageSize) - result.target_statuses = ParcelableStatusUtils.fromStatuses(activity.targetStatuses, - accountKey, accountType, profileImageSize) - result.target_object_statuses = ParcelableStatusUtils.fromStatuses(activity.targetObjectStatuses, - accountKey, accountType, profileImageSize) - result.target_object_user_lists = ParcelableUserListUtils.fromUserLists(activity.targetObjectUserLists, - accountKey, profileImageSize) - result.target_object_users = ParcelableUserUtils.fromUsers(activity.targetObjectUsers, - accountKey, accountType, profileImageSize) - result.has_following_source = activity.sources.fold(false) { folded, item -> + result.target_user_lists = activity.targetUserLists?.toParcelables(accountKey, + profileImageSize) + result.target_statuses = activity.targetStatuses?.toParcelables(accountKey, + accountType, profileImageSize) + result.target_object_statuses = activity.targetObjectStatuses?.toParcelables(accountKey, + accountType, profileImageSize) + result.target_object_user_lists = activity.targetObjectUserLists?.toParcelables(accountKey, + profileImageSize) + result.target_object_users = activity.targetObjectUsers?.toParcelables(accountKey, accountType, + profileImageSize) + result.has_following_source = activity.sources?.fold(false) { folded, item -> if (item.isFollowing == true) { return@fold true } return@fold folded - } + } ?: false if (result.sources != null) { result.source_ids = arrayOfNulls(result.sources.size) for (i in result.sources.indices) { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.kt index 3970e7a0c..59604fdb9 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableCardEntityUtils.kt @@ -1,14 +1,8 @@ package org.mariotaku.twidere.model.util -import android.support.v4.util.ArrayMap -import org.apache.commons.lang3.math.NumberUtils -import org.mariotaku.microblog.library.twitter.model.CardEntity import org.mariotaku.microblog.library.twitter.util.ThreadLocalSimpleDateFormat -import org.mariotaku.twidere.model.ParcelableCardEntity -import org.mariotaku.twidere.model.UserKey import java.text.DateFormat -import java.text.ParseException import java.util.* /** @@ -24,58 +18,4 @@ object ParcelableCardEntityUtils { sISOFormat.timeZone = TimeZone.getTimeZone("UTC") } - fun fromCardEntity(card: CardEntity, accountKey: UserKey, accountType: String): ParcelableCardEntity? { - val obj = ParcelableCardEntity() - obj.name = card.name - obj.url = card.url - obj.users = ParcelableUserUtils.fromUsers(card.users, accountKey, accountType) - obj.account_key = accountKey - obj.values = from(card.bindingValues) - return obj - } - - fun from(bindingValues: Map?): Map? { - if (bindingValues == null) return null - val map = ArrayMap() - for ((key, value) in bindingValues) { - map.put(key, ParcelableCardEntity.ParcelableBindingValue(value)) - } - return map - } - - fun getAsBoolean(obj: ParcelableCardEntity, key: String, def: Boolean): Boolean { - val value = obj.getValue(key) ?: return def - return java.lang.Boolean.parseBoolean(value.value) - } - - fun getAsString(obj: ParcelableCardEntity, key: String, def: String?): String? { - return obj.getValue(key)?.value ?: return def - } - - fun getString(obj: ParcelableCardEntity, key: String): String? { - val value = obj.getValue(key) - if (value == null || CardEntity.BindingValue.TYPE_STRING != value.type) return null - return getAsString(obj, key, null) - } - - fun getAsInteger(obj: ParcelableCardEntity, key: String, def: Int): Int { - val value = obj.getValue(key) ?: return def - return NumberUtils.toInt(value.value, def) - } - - fun getAsLong(obj: ParcelableCardEntity, key: String, def: Long): Long { - val value = obj.getValue(key) ?: return def - return NumberUtils.toLong(value.value, def) - } - - fun getAsDate(obj: ParcelableCardEntity, key: String, def: Date): Date { - val value = obj.getValue(key) ?: return def - try { - return sISOFormat.parse(value.value) - } catch (e: ParseException) { - return def - } - - } - } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableMediaUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableMediaUtils.kt index 0df14dc1f..56a214f0f 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableMediaUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableMediaUtils.kt @@ -1,11 +1,11 @@ package org.mariotaku.twidere.model.util import android.text.TextUtils -import org.apache.commons.lang3.ArrayUtils -import org.apache.commons.lang3.math.NumberUtils import org.mariotaku.ktextension.addAllTo import org.mariotaku.ktextension.isNullOrEmpty +import org.mariotaku.ktextension.toIntOr import org.mariotaku.microblog.library.twitter.model.* +import org.mariotaku.twidere.extension.model.toParcelable import org.mariotaku.twidere.model.ParcelableMedia import org.mariotaku.twidere.model.ParcelableStatus import org.mariotaku.twidere.model.UserKey @@ -87,11 +87,9 @@ object ParcelableMediaUtils { private fun fromAttachments(status: Status): Array { val attachments = status.attachments ?: return emptyArray() - val temp = arrayOfNulls(attachments.size) val externalUrl = status.externalUrl - var i = 0 - for (attachment in attachments) { - val mimeType = attachment.mimetype ?: continue + return attachments.mapNotNull { attachment -> + val mimeType = attachment.mimetype ?: return@mapNotNull null val media = ParcelableMedia() if (mimeType.startsWith("image/")) { @@ -101,7 +99,7 @@ object ParcelableMediaUtils { } else { // https://github.com/TwidereProject/Twidere-Android/issues/729 // Skip unsupported attachment - continue + return@mapNotNull null } media.width = attachment.width media.height = attachment.height @@ -109,9 +107,8 @@ object ParcelableMediaUtils { media.page_url = if (TextUtils.isEmpty(externalUrl)) attachment.url else externalUrl media.media_url = attachment.url media.preview_url = attachment.largeThumbUrl - temp[i++] = media - } - return ArrayUtils.subarray(temp, 0, i) + return@mapNotNull null + }.toTypedArray() } private fun fromCard(card: CardEntity?, urlEntities: Array?, @@ -122,8 +119,7 @@ object ParcelableMediaUtils { if ("animated_gif" == name || "player" == name) { val media = ParcelableMedia() val playerStreamUrl = card.getBindingValue("player_stream_url") - media.card = ParcelableCardEntityUtils.fromCardEntity(card, accountKey, - accountType) + media.card = card.toParcelable(accountKey, accountType) val appUrlResolved = card.getBindingValue("app_url_resolved") as CardEntity.StringValue media.url = if (checkUrl(appUrlResolved)) appUrlResolved.value else card.url if ("animated_gif" == name) { @@ -148,8 +144,8 @@ object ParcelableMediaUtils { val playerWidth = card.getBindingValue("player_width") val playerHeight = card.getBindingValue("player_height") if (playerWidth is CardEntity.StringValue && playerHeight is CardEntity.StringValue) { - media.width = NumberUtils.toInt(playerWidth.value, -1) - media.height = NumberUtils.toInt(playerHeight.value, -1) + media.width = playerWidth.value.toIntOr(-1) + media.height = playerHeight.value.toIntOr(-1) } writeLinkInfo(media, urlEntities, mediaEntities, extendedMediaEntities) return arrayOf(media) @@ -158,8 +154,7 @@ object ParcelableMediaUtils { val media = ParcelableMedia() media.url = card.url - media.card = ParcelableCardEntityUtils.fromCardEntity(card, accountKey, - accountType) + media.card = card.toParcelable(accountKey, accountType) media.type = ParcelableMedia.Type.IMAGE media.media_url = photoImageFullSize.url media.width = photoImageFullSize.width diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt index d7dcca2f3..78223edb0 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableStatusUtils.kt @@ -4,6 +4,7 @@ import android.text.Spanned import android.text.style.URLSpan import org.mariotaku.microblog.library.twitter.model.Status import org.mariotaku.twidere.extension.model.api.getProfileImageOfSize +import org.mariotaku.twidere.extension.model.toParcelable import org.mariotaku.twidere.model.* import org.mariotaku.twidere.model.ParcelableStatus.FilterFlags import org.mariotaku.twidere.util.HtmlSpanBuilder @@ -163,9 +164,7 @@ object ParcelableStatusUtils { result.is_possibly_sensitive = status.isPossiblySensitive result.mentions = ParcelableUserMentionUtils.fromUserMentionEntities(user, status.userMentionEntities) - result.card = status.card?.let { - ParcelableCardEntityUtils.fromCardEntity(it, accountKey, accountType) - } + result.card = status.card?.toParcelable(accountKey, accountType) result.card_name = result.card?.name result.place_full_name = getPlaceFullName(status) result.lang = status.lang @@ -208,14 +207,6 @@ object ParcelableStatusUtils { return UserKey(inReplyToUserId, accountKey.host) } - fun fromStatuses(statuses: Array?, accountKey: UserKey, accountType: String, - profileImageSize: String): Array? { - if (statuses == null) return null - return Array(statuses.size) { i -> - fromStatus(statuses[i], accountKey, accountType, false, profileImageSize) - } - } - private fun getPlaceFullName(status: Status): String? { val place = status.place if (place != null) return place.fullName diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableUserListUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableUserListUtils.kt index 424e4adaa..b9c6e0d18 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableUserListUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableUserListUtils.kt @@ -50,13 +50,6 @@ object ParcelableUserListUtils { return obj } - fun fromUserLists(userLists: Array?, accountKey: UserKey, - profileImageSize: String = "normal"): Array? { - if (userLists == null) return emptyArray() - val size = userLists.size - return Array(size) { from(userLists[it], accountKey, profileImageSize = profileImageSize) } - } - fun check(userList: ParcelableUserList, accountKey: UserKey, listId: String?, userKey: UserKey?, screenName: String?, listName: String?): Boolean { if (userList.account_key != accountKey) return false diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableUserUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableUserUtils.kt index c5ba54750..5c1dca48d 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableUserUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableUserUtils.kt @@ -88,12 +88,6 @@ object ParcelableUserUtils { return obj } - fun fromUsers(users: Array, accountKey: UserKey, accountType: String, - profileImageSize: String = "normal"): Array? { - return users.map { - fromUser(it, accountKey, accountType, profileImageSize = profileImageSize) - }.toTypedArray() - } fun parseColor(colorString: String?): Int { if (colorString == null) return 0 diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/preference/RefreshIntervalPreference.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/preference/RefreshIntervalPreference.kt index 7c321b9e9..a93ac3bb8 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/preference/RefreshIntervalPreference.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/preference/RefreshIntervalPreference.kt @@ -7,7 +7,7 @@ import android.os.Build import android.support.v7.preference.PreferenceManager import android.util.AttributeSet import org.mariotaku.kpreferences.get -import org.mariotaku.ktextension.toLong +import org.mariotaku.ktextension.toLongOr import org.mariotaku.twidere.constant.autoRefreshCompatibilityModeKey import java.util.concurrent.TimeUnit @@ -42,7 +42,7 @@ class RefreshIntervalPreference( var index: Int = -1 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !sharedPreferences[autoRefreshCompatibilityModeKey]) { index = valuesBackup.indexOfFirst { - val intervalMinutes = it.toString().toLong(-1L) + val intervalMinutes = it.toString().toLongOr(-1L) if (intervalMinutes < 0) return@indexOfFirst false return@indexOfFirst TimeUnit.MINUTES.toMillis(intervalMinutes) >= JobInfo.getMinPeriodMillis() } @@ -56,8 +56,8 @@ class RefreshIntervalPreference( entryValues = valuesBackup entries = entriesBackup } - val valueMinutes = value.toLong(-1) - val minValue = entryValues.firstOrNull()?.toString().toLong(-1) + val valueMinutes = value.toLongOr(-1) + val minValue = entryValues.firstOrNull()?.toString().toLongOr(-1) if (valueMinutes > 0 && valueMinutes < minValue) { value = minValue.toString() } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/provider/TwidereDataProvider.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/provider/TwidereDataProvider.kt index 3fda0a9ad..82465504f 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/provider/TwidereDataProvider.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/provider/TwidereDataProvider.kt @@ -38,7 +38,6 @@ import android.support.v4.app.NotificationCompat import android.support.v4.text.BidiFormatter import com.squareup.otto.Bus import okhttp3.Dns -import org.apache.commons.lang3.ArrayUtils import org.mariotaku.ktextension.isNullOrEmpty import org.mariotaku.ktextension.toNulls import org.mariotaku.library.objectcursor.ObjectCursor @@ -169,7 +168,7 @@ class TwidereDataProvider : ContentProvider(), LazyLoadCallback { val map = permissionsManager.all val callingPackages = pm.getPackagesForUid(Binder.getCallingUid()) for ((key, value) in map) { - if (ArrayUtils.contains(callingPackages, key)) { + if (key in callingPackages) { c.addRow(arrayOf(key, value)) } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/receiver/NotificationReceiver.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/receiver/NotificationReceiver.kt index 4d02c68d2..8788e1771 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/receiver/NotificationReceiver.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/receiver/NotificationReceiver.kt @@ -23,7 +23,7 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import org.mariotaku.abstask.library.TaskStarter -import org.mariotaku.ktextension.toLong +import org.mariotaku.ktextension.toLongOr import org.mariotaku.twidere.TwidereConstants.* import org.mariotaku.twidere.annotation.NotificationType import org.mariotaku.twidere.annotation.ReadPositionTag @@ -53,19 +53,19 @@ class NotificationReceiver : BroadcastReceiver() { val positionTag = Utils.getReadPositionTagWithAccount(ReadPositionTag.HOME_TIMELINE, accountKey) val manager = holder.readStateManager - manager.setPosition(positionTag, paramReadPosition.toLong(-1)) + manager.setPosition(positionTag, paramReadPosition.toLongOr(-1L)) } NotificationType.INTERACTIONS -> { val positionTag = Utils.getReadPositionTagWithAccount(ReadPositionTag.ACTIVITIES_ABOUT_ME, accountKey) val manager = holder.readStateManager - manager.setPosition(positionTag, paramReadPosition.toLong(-1)) + manager.setPosition(positionTag, paramReadPosition.toLongOr(-1L)) } NotificationType.DIRECT_MESSAGES -> { if (accountKey == null) return val appContext = context.applicationContext val task = BatchMarkMessageReadTask(appContext, accountKey, - paramReadPosition.toLong(-1)) + paramReadPosition.toLongOr(-1L)) TaskStarter.execute(task) } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/service/AccountAuthenticatorService.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/service/AccountAuthenticatorService.kt index 4cc41524b..b25849f0a 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/service/AccountAuthenticatorService.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/service/AccountAuthenticatorService.kt @@ -4,7 +4,6 @@ import android.accounts.AbstractAccountAuthenticator import android.accounts.Account import android.accounts.AccountAuthenticatorResponse import android.accounts.AccountManager -import android.app.Service import android.content.Context import android.content.Intent import android.os.Bundle diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/service/AccountSyncService.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/service/AccountSyncService.kt index 19c8b98d2..5159aca31 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/service/AccountSyncService.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/service/AccountSyncService.kt @@ -1,7 +1,6 @@ package org.mariotaku.twidere.service import android.accounts.Account -import android.app.Service import android.content.* import android.os.Bundle import android.os.IBinder diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/service/BaseIntentService.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/service/BaseIntentService.kt index 2327923cc..9695cbcbf 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/service/BaseIntentService.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/service/BaseIntentService.kt @@ -3,7 +3,10 @@ package org.mariotaku.twidere.service import android.app.IntentService import com.twitter.Extractor import com.twitter.Validator -import org.mariotaku.twidere.util.* +import org.mariotaku.twidere.util.AsyncTwitterWrapper +import org.mariotaku.twidere.util.NotificationManagerWrapper +import org.mariotaku.twidere.util.SharedPreferencesWrapper +import org.mariotaku.twidere.util.UserColorNameManager import org.mariotaku.twidere.util.dagger.GeneralComponentHelper import javax.inject.Inject diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/service/LengthyOperationsService.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/service/LengthyOperationsService.kt index 6bb0d7a85..90da9e999 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/service/LengthyOperationsService.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/service/LengthyOperationsService.kt @@ -44,7 +44,7 @@ import nl.komponents.kovenant.ui.successUi import org.mariotaku.abstask.library.AbstractTask import org.mariotaku.abstask.library.ManualTaskStarter import org.mariotaku.ktextension.configure -import org.mariotaku.ktextension.toLong +import org.mariotaku.ktextension.toLongOr import org.mariotaku.ktextension.toTypedArray import org.mariotaku.ktextension.useCursor import org.mariotaku.library.objectcursor.ObjectCursor @@ -116,7 +116,7 @@ class LengthyOperationsService : BaseIntentService("lengthy_operations") { private fun handleSendDraftIntent(intent: Intent) { val uri = intent.data ?: return notificationManager.cancel(uri.toString(), NOTIFICATION_ID_DRAFTS) - val draftId = uri.lastPathSegment.toLong(-1) + val draftId = uri.lastPathSegment.toLongOr(-1L) if (draftId == -1L) return val where = Expression.equals(Drafts._ID, draftId) @SuppressLint("Recycle") @@ -166,7 +166,7 @@ class LengthyOperationsService : BaseIntentService("lengthy_operations") { private fun handleDiscardDraftIntent(intent: Intent) { val data = intent.data ?: return task { - if (deleteDrafts(this, longArrayOf(data.lastPathSegment.toLong(-1))) < 1) { + if (deleteDrafts(this, longArrayOf(data.lastPathSegment.toLongOr(-1L))) < 1) { throw IOException() } return@task data diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/service/StreamingService.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/service/StreamingService.kt index 0ec1eb9ae..a77290241 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/service/StreamingService.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/service/StreamingService.kt @@ -16,7 +16,7 @@ import org.mariotaku.abstask.library.TaskStarter import org.mariotaku.kpreferences.get import org.mariotaku.ktextension.addOnAccountsUpdatedListenerSafe import org.mariotaku.ktextension.removeOnAccountsUpdatedListenerSafe -import org.mariotaku.ktextension.toLong +import org.mariotaku.ktextension.toLongOr import org.mariotaku.library.objectcursor.ObjectCursor import org.mariotaku.microblog.library.MicroBlogException import org.mariotaku.microblog.library.twitter.TwitterUserStream @@ -357,7 +357,7 @@ class StreamingService : BaseService() { } else { val uri = resolver.insert(Activities.AboutMe.CONTENT_URI, values) if (uri != null) { - curActivity._id = uri.lastPathSegment.toLong(-1) + curActivity._id = uri.lastPathSegment.toLongOr(-1L) } } lastActivityAboutMe = curActivity diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/UpdateProfileBackgroundImageTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/UpdateProfileBackgroundImageTask.kt index 06c095091..4bf15c7ff 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/UpdateProfileBackgroundImageTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/UpdateProfileBackgroundImageTask.kt @@ -15,7 +15,6 @@ import org.mariotaku.twidere.model.event.ProfileUpdatedEvent import org.mariotaku.twidere.model.util.AccountUtils import org.mariotaku.twidere.model.util.ParcelableUserUtils import org.mariotaku.twidere.util.DebugLog -import org.mariotaku.twidere.util.MicroBlogAPIFactory import org.mariotaku.twidere.util.TwitterWrapper import org.mariotaku.twidere.util.Utils import java.io.IOException diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetActivitiesAboutMeTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetActivitiesAboutMeTask.kt index b1b9b132e..e916c6b4e 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetActivitiesAboutMeTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetActivitiesAboutMeTask.kt @@ -30,7 +30,6 @@ import org.mariotaku.twidere.extension.model.isOfficial import org.mariotaku.twidere.model.AccountDetails import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.provider.TwidereDataStore.Activities -import org.mariotaku.twidere.task.twitter.GetActivitiesTask import org.mariotaku.twidere.util.ErrorInfoStore import org.mariotaku.twidere.util.Utils diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetHomeTimelineTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetHomeTimelineTask.kt index 3188ef67e..b782d5800 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetHomeTimelineTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetHomeTimelineTask.kt @@ -28,7 +28,6 @@ import org.mariotaku.microblog.library.twitter.model.Paging import org.mariotaku.microblog.library.twitter.model.ResponseList import org.mariotaku.microblog.library.twitter.model.Status import org.mariotaku.twidere.provider.TwidereDataStore.Statuses -import org.mariotaku.twidere.task.twitter.GetStatusesTask import org.mariotaku.twidere.util.ErrorInfoStore /** diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetStatusesTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetStatusesTask.kt index 0c24a630d..00331aaf8 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetStatusesTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetStatusesTask.kt @@ -6,10 +6,9 @@ import android.content.Context import android.net.Uri import edu.tsinghua.hotmobi.HotMobiLogger import edu.tsinghua.hotmobi.model.RefreshEvent -import org.apache.commons.lang3.ArrayUtils -import org.apache.commons.lang3.math.NumberUtils import org.mariotaku.abstask.library.TaskStarter import org.mariotaku.kpreferences.get +import org.mariotaku.ktextension.toLongOr import org.mariotaku.library.objectcursor.ObjectCursor import org.mariotaku.microblog.library.MicroBlog import org.mariotaku.microblog.library.MicroBlogException @@ -89,7 +88,7 @@ abstract class GetStatusesTask( } if (sinceIds != null && sinceIds[i] != null) { sinceId = sinceIds[i] - val sinceIdLong = NumberUtils.toLong(sinceId, -1) + val sinceIdLong = sinceId.toLongOr(-1L) //TODO handle non-twitter case if (sinceIdLong != -1L) { paging.sinceId((sinceIdLong - 1).toString()) @@ -202,7 +201,7 @@ abstract class GetStatusesTask( // END HotMobi // Insert a gap. - val deletedOldGap = rowsDeleted > 0 && ArrayUtils.contains(statusIds, maxId) + val deletedOldGap = rowsDeleted > 0 && maxId in statusIds val noRowsDeleted = rowsDeleted == 0 // Why loadItemLimit / 2? because it will not acting strange in most cases val insertGap = minIdx != -1 && olderCount > 0 && (noRowsDeleted || deletedOldGap) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/UpdateStatusTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/UpdateStatusTask.kt index e25957cc0..e03af6cb3 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/UpdateStatusTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/UpdateStatusTask.kt @@ -19,8 +19,6 @@ import edu.tsinghua.hotmobi.HotMobiLogger import edu.tsinghua.hotmobi.model.MediaUploadEvent import net.ypresto.androidtranscoder.MediaTranscoder import net.ypresto.androidtranscoder.format.MediaFormatStrategyPresets -import org.apache.commons.lang3.ArrayUtils -import org.apache.commons.lang3.math.NumberUtils import org.mariotaku.ktextension.* import org.mariotaku.library.objectcursor.ObjectCursor import org.mariotaku.microblog.library.MicroBlog @@ -367,7 +365,7 @@ class UpdateStatusTask( @Throws(UploadException::class) private fun uploadMediaWithDefaultProvider(update: ParcelableStatusUpdate, pendingUpdate: PendingStatusUpdate) { // Return empty array if no media attached - if (ArrayUtils.isEmpty(update.media)) return + if (update.media.isNullOrEmpty()) return val ownersList = update.accounts.filter { AccountType.TWITTER == it.type }.map(AccountDetails::key) @@ -920,10 +918,10 @@ class UpdateStatusTask( if (extractedMimeType != null) { mediaType = extractedMimeType } - geometry.x = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH).toInt(-1) - geometry.y = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT).toInt(-1) - duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION).toLong(-1) - framerate = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CAPTURE_FRAMERATE).toDouble(-1.0) + geometry.x = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH).toIntOr(-1) + geometry.y = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT).toIntOr(-1) + duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION).toLongOr(-1) + framerate = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CAPTURE_FRAMERATE).toDoubleOr(-1.0) size = resolver.openFileDescriptor(mediaUri, "r").use { it.statSize } } catch (e: Exception) { @@ -1001,7 +999,7 @@ class UpdateStatusTask( val resolver = context.contentResolver val creator = ObjectCursor.valuesCreatorFrom(Draft::class.java) val draftUri = resolver.insert(Drafts.CONTENT_URI, creator.create(draft)) ?: return -1 - return NumberUtils.toLong(draftUri.lastPathSegment, -1) + return draftUri.lastPathSegment.toLongOr(-1) } fun deleteDraft(context: Context, id: Long) { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/message/GetMessagesTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/message/GetMessagesTask.kt index aead8d0c9..77a410eb9 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/message/GetMessagesTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/message/GetMessagesTask.kt @@ -22,8 +22,8 @@ package org.mariotaku.twidere.task.twitter.message import android.annotation.SuppressLint import android.content.ContentValues import android.content.Context -import org.mariotaku.ktextension.toInt -import org.mariotaku.ktextension.toLong +import org.mariotaku.ktextension.toIntOr +import org.mariotaku.ktextension.toLongOr import org.mariotaku.ktextension.useCursor import org.mariotaku.library.objectcursor.ObjectCursor import org.mariotaku.microblog.library.MicroBlog @@ -224,7 +224,7 @@ class GetMessagesTask( val accountKey = details.key val accountType = details.type val cursor = param.cursors?.get(index) - val page = cursor?.substringAfter("page:").toInt(-1) + val page = cursor?.substringAfter("page:").toIntOr(-1) val result = microBlog.getConversationList(Paging().apply { count(60) if (page >= 0) { @@ -529,10 +529,10 @@ class GetMessagesTask( private fun Map>.findLastReadTimestamp(conversationId: String, lastReadEventId: String?): Long { - val longEventId = lastReadEventId.toLong(-1) + val longEventId = lastReadEventId.toLongOr(-1L) return this[conversationId]?.filter { message -> if (message.id == lastReadEventId) return@filter true - if (longEventId > 0 && longEventId >= message.id.toLong(-1)) return@filter true + if (longEventId > 0 && longEventId >= message.id.toLongOr(-1L)) return@filter true return@filter false }?.maxBy(ParcelableMessage::message_timestamp)?.message_timestamp ?: -1 } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/DataStoreUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/DataStoreUtils.kt index 9fe7f7dea..6d36bb404 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/DataStoreUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/DataStoreUtils.kt @@ -29,8 +29,6 @@ import android.os.Parcelable import android.provider.BaseColumns import android.support.annotation.WorkerThread import android.text.TextUtils -import com.bluelinelabs.logansquare.LoganSquare -import org.apache.commons.lang3.ArrayUtils import org.apache.commons.lang3.StringUtils import org.mariotaku.kpreferences.get import org.mariotaku.ktextension.useCursor @@ -720,7 +718,9 @@ object DataStoreUtils { val keyString = cur.getString(cur.getColumnIndex(keyField)) if (keyString != null) { val accountKey = UserKey.valueOf(keyString) - val arrayIdx = ArrayUtils.indexOf(keys, accountKey) + val arrayIdx = keys.indexOfFirst { + accountKey == it + } if (arrayIdx >= 0) { creator.assign(resultArray, arrayIdx, cur, colIdx) } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/HttpClientFactory.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/HttpClientFactory.kt index f6f689e38..509f799b1 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/HttpClientFactory.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/HttpClientFactory.kt @@ -3,7 +3,7 @@ package org.mariotaku.twidere.util import android.content.Context import android.text.TextUtils.isEmpty import okhttp3.* -import org.apache.commons.lang3.math.NumberUtils +import org.mariotaku.ktextension.toIntOr import org.mariotaku.restfu.http.RestHttpClient import org.mariotaku.restfu.okhttp3.OkHttpRestClient import org.mariotaku.twidere.constant.SharedPreferenceConstants.* @@ -77,7 +77,7 @@ object HttpClientFactory { private fun configProxy(builder: OkHttpClient.Builder) { val proxyType = prefs.getString(KEY_PROXY_TYPE, null) val proxyHost = prefs.getString(KEY_PROXY_HOST, null) - val proxyPort = NumberUtils.toInt(prefs.getString(KEY_PROXY_PORT, null), -1) + val proxyPort = prefs.getString(KEY_PROXY_PORT, null).toIntOr(-1) if (!isEmpty(proxyHost) && proxyPort in (0..65535)) { val type = getProxyType(proxyType) if (type != Proxy.Type.DIRECT) { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/MenuUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/MenuUtils.kt index 3ed9bfc66..4bc15c452 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/MenuUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/MenuUtils.kt @@ -44,10 +44,8 @@ import org.mariotaku.ktextension.Bundle import org.mariotaku.ktextension.set import org.mariotaku.ktextension.setItemChecked import org.mariotaku.ktextension.setMenuItemIcon -import org.mariotaku.twidere.Constants import org.mariotaku.twidere.Constants.* import org.mariotaku.twidere.R -import org.mariotaku.twidere.TwidereConstants.* import org.mariotaku.twidere.activity.AccountSelectorActivity import org.mariotaku.twidere.activity.ColorPickerDialogActivity import org.mariotaku.twidere.constant.SharedPreferenceConstants diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/TwitterCardUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/TwitterCardUtils.kt index c48c1b66b..a8f38b457 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/TwitterCardUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/TwitterCardUtils.kt @@ -21,10 +21,10 @@ package org.mariotaku.twidere.util import android.graphics.Point import android.text.TextUtils -import org.apache.commons.lang3.math.NumberUtils +import org.mariotaku.twidere.extension.model.getAsInteger +import org.mariotaku.twidere.extension.model.getString import org.mariotaku.twidere.model.ParcelableCardEntity import org.mariotaku.twidere.model.ParcelableStatus -import org.mariotaku.twidere.model.util.ParcelableCardEntityUtils import java.util.regex.Pattern /** @@ -39,8 +39,8 @@ object TwitterCardUtils { const val CARD_NAME_ANIMATED_GIF = "animated_gif" fun getCardSize(card: ParcelableCardEntity): Point? { - val playerWidth = ParcelableCardEntityUtils.getAsInteger(card, "player_width", -1) - val playerHeight = ParcelableCardEntityUtils.getAsInteger(card, "player_height", -1) + val playerWidth = card.getAsInteger("player_width", -1) + val playerHeight = card.getAsInteger("player_height", -1) if (playerWidth > 0 && playerHeight > 0) { return Point(playerWidth, playerHeight) } @@ -52,7 +52,7 @@ object TwitterCardUtils { when (status.card_name) { CARD_NAME_PLAYER -> { status.media?.let { mediaArray -> - val appUrlResolved = ParcelableCardEntityUtils.getString(card, "app_url_resolved") + val appUrlResolved = card.getString("app_url_resolved") val cardUrl = card.url mediaArray.forEach { if (it.url == appUrlResolved || it.url == cardUrl) { @@ -60,7 +60,7 @@ object TwitterCardUtils { } } } - return TextUtils.isEmpty(ParcelableCardEntityUtils.getString(card, "player_stream_url")) + return TextUtils.isEmpty(card.getString("player_stream_url")) } CARD_NAME_AUDIO -> { return true @@ -75,7 +75,7 @@ object TwitterCardUtils { fun getChoicesCount(card: ParcelableCardEntity): Int { val matcher = PATTERN_POLL_TEXT_ONLY.matcher(card.name) if (!matcher.matches()) throw IllegalStateException() - return NumberUtils.toInt(matcher.group(1)) + return matcher.group(1).toInt() } fun isPoll(card: ParcelableCardEntity): Boolean { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/filter/UrlFiltersSubscriptionProvider.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/filter/UrlFiltersSubscriptionProvider.kt index f0b5fc90b..f89d66e91 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/filter/UrlFiltersSubscriptionProvider.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/filter/UrlFiltersSubscriptionProvider.kt @@ -2,7 +2,6 @@ package org.mariotaku.twidere.util.filter import android.content.Context import android.net.Uri -import com.bluelinelabs.logansquare.LoganSquare import com.bluelinelabs.logansquare.annotation.JsonField import com.bluelinelabs.logansquare.annotation.JsonObject import org.mariotaku.restfu.annotation.method.GET diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/net/TwidereDns.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/net/TwidereDns.kt index bcf5181a0..f5fa14d7b 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/net/TwidereDns.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/net/TwidereDns.kt @@ -25,7 +25,7 @@ import android.util.Log import android.util.TimingLogger import okhttp3.Dns import org.apache.commons.lang3.StringUtils -import org.mariotaku.ktextension.toInt +import org.mariotaku.ktextension.toIntOr import org.mariotaku.twidere.BuildConfig import org.mariotaku.twidere.TwidereConstants.HOST_MAPPING_PREFERENCES_NAME import org.mariotaku.twidere.constant.SharedPreferenceConstants.* @@ -200,7 +200,7 @@ class TwidereDns(context: Context, private val preferences: SharedPreferences) : if (!isValidIpAddress(segs[0])) return@mapNotNull null return@mapNotNull SimpleResolver(segs[0]).apply { if (segs.size == 2) { - val port = segs[1].toInt(-1) + val port = segs[1].toIntOr(-1) if (port in 0..65535) { setPort(port) } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/twitter/card/TwitterCardViewFactory.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/twitter/card/TwitterCardViewFactory.kt index 94fe33e08..87ef105d3 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/twitter/card/TwitterCardViewFactory.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/twitter/card/TwitterCardViewFactory.kt @@ -19,9 +19,9 @@ package org.mariotaku.twidere.util.twitter.card +import org.mariotaku.twidere.extension.model.getString import org.mariotaku.twidere.model.ParcelableCardEntity import org.mariotaku.twidere.model.ParcelableStatus -import org.mariotaku.twidere.model.util.ParcelableCardEntityUtils import org.mariotaku.twidere.util.TwitterCardUtils import org.mariotaku.twidere.view.ContainerView import org.mariotaku.twidere.view.controller.twitter.card.CardBrowserViewController @@ -69,7 +69,7 @@ abstract class TwitterCardViewFactory { } private fun createGenericPlayerFragment(card: ParcelableCardEntity): ContainerView.ViewController? { - val playerUrl = ParcelableCardEntityUtils.getString(card, "player_url") ?: return null + val playerUrl = card.getString("player_url") ?: return null return CardBrowserViewController.show(playerUrl) } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/ShortTimeView.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/ShortTimeView.kt index 43de83793..704e588d4 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/ShortTimeView.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/ShortTimeView.kt @@ -20,19 +20,15 @@ package org.mariotaku.twidere.view import android.content.Context -import android.os.Handler import android.os.SystemClock import android.support.v7.widget.AppCompatTextView import android.text.TextUtils import android.text.format.DateUtils import android.util.AttributeSet - import org.mariotaku.twidere.Constants import org.mariotaku.twidere.R - -import java.lang.ref.WeakReference - import org.mariotaku.twidere.util.Utils.formatSameDayTime +import java.lang.ref.WeakReference class ShortTimeView( context: Context, diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/controller/twitter/card/CardPollViewController.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/controller/twitter/card/CardPollViewController.kt index 3b4c74d6b..a627ca29a 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/controller/twitter/card/CardPollViewController.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/controller/twitter/card/CardPollViewController.kt @@ -32,19 +32,18 @@ import android.widget.TextView import kotlinx.android.synthetic.main.layout_twitter_card_poll.view.* import nl.komponents.kovenant.task import nl.komponents.kovenant.ui.successUi -import org.apache.commons.lang3.math.NumberUtils import org.mariotaku.abstask.library.AbstractTask import org.mariotaku.abstask.library.TaskStarter +import org.mariotaku.ktextension.toLongOr import org.mariotaku.microblog.library.MicroBlogException import org.mariotaku.microblog.library.twitter.TwitterCaps import org.mariotaku.microblog.library.twitter.model.CardDataMap import org.mariotaku.twidere.Constants.LOGTAG import org.mariotaku.twidere.R -import org.mariotaku.twidere.extension.model.newMicroBlogInstance +import org.mariotaku.twidere.extension.model.* import org.mariotaku.twidere.model.ParcelableCardEntity import org.mariotaku.twidere.model.ParcelableStatus import org.mariotaku.twidere.model.util.AccountUtils -import org.mariotaku.twidere.model.util.ParcelableCardEntityUtils import org.mariotaku.twidere.util.MicroBlogAPIFactory import org.mariotaku.twidere.util.TwitterCardUtils import org.mariotaku.twidere.util.support.ViewSupport @@ -104,8 +103,7 @@ class CardPollViewController : ContainerView.ViewController() { if (cardResponse == null || cardResponse.name == null) { throw IllegalStateException() } - return@task ParcelableCardEntityUtils.fromCardEntity(cardResponse, details.key, - details.type) + return@task cardResponse.toParcelable(details.key, details.type) }.successUi { data -> weakThis.get()?.displayPoll(data, status) } @@ -116,15 +114,15 @@ class CardPollViewController : ContainerView.ViewController() { fetchedCard = card val choicesCount = TwitterCardUtils.getChoicesCount(card) var votesSum = 0 - val countsAreFinal = ParcelableCardEntityUtils.getAsBoolean(card, "counts_are_final", false) - val selectedChoice = ParcelableCardEntityUtils.getAsInteger(card, "selected_choice", -1) - val endDatetimeUtc = ParcelableCardEntityUtils.getAsDate(card, "end_datetime_utc", Date()) + val countsAreFinal = card.getAsBoolean("counts_are_final", false) + val selectedChoice = card.getAsInteger("selected_choice", -1) + val endDatetimeUtc = card.getAsDate("end_datetime_utc", Date()) val hasChoice = selectedChoice != -1 val isMyPoll = status.account_key == status.user_key val showResult = countsAreFinal || isMyPoll || hasChoice for (i in 0..choicesCount - 1) { val choiceIndex = i + 1 - votesSum += ParcelableCardEntityUtils.getAsInteger(card, "choice" + choiceIndex + "_count", 0) + votesSum += card.getAsInteger("choice${choiceIndex}_count", 0) } val clickListener = object : View.OnClickListener { @@ -141,7 +139,7 @@ class CardPollViewController : ContainerView.ViewController() { choiceRadioButton.isChecked = checked if (checked) { val cardData = CardDataMap() - cardData.putLong("original_tweet_id", NumberUtils.toLong(status.id)) + cardData.putLong("original_tweet_id", status.id.toLongOr(-1L)) cardData.putString("card_uri", card.url) cardData.putString("cards_platform", MicroBlogAPIFactory.CARDS_PLATFORM_ANDROID_12) cardData.putString("response_card_name", card.name) @@ -161,8 +159,7 @@ class CardPollViewController : ContainerView.ViewController() { val cardEntity = caps.sendPassThrough(cardDataMap).card ?: run { return null } - return ParcelableCardEntityUtils.fromCardEntity(cardEntity, - card.account_key, details.type) + return cardEntity.toParcelable(card.account_key, details.type) } catch (e: MicroBlogException) { Log.w(LOGTAG, e) } @@ -188,8 +185,8 @@ class CardPollViewController : ContainerView.ViewController() { val choiceRadioButton = pollItem.findViewById(R.id.choice_button) as RadioButton val choiceIndex = i + 1 - val label = ParcelableCardEntityUtils.getAsString(card, "choice" + choiceIndex + "_label", null) - val value = ParcelableCardEntityUtils.getAsInteger(card, "choice" + choiceIndex + "_count", 0) + val label = card.getAsString("choice${choiceIndex}_label", null) + val value = card.getAsInteger("choice${choiceIndex}_count", 0) if (label == null) throw NullPointerException() val choicePercent = if (votesSum == 0) 0f else value / votesSum.toFloat() choiceLabelView.text = label diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/DraftViewHolder.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/DraftViewHolder.kt index f269497ca..b5a9b8e3e 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/DraftViewHolder.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/DraftViewHolder.kt @@ -28,9 +28,7 @@ import org.mariotaku.twidere.R import org.mariotaku.twidere.extension.model.getActionName import org.mariotaku.twidere.model.Draft import org.mariotaku.twidere.model.ParcelableMedia -import org.mariotaku.twidere.model.ParcelableMediaUpdate import org.mariotaku.twidere.model.draft.StatusObjectExtras -import org.mariotaku.twidere.model.util.ParcelableMediaUtils import org.mariotaku.twidere.util.DataStoreUtils import org.mariotaku.twidere.util.Utils diff --git a/twidere/src/main/res/menu/menu_messages_conversation.xml b/twidere/src/main/res/menu/menu_messages_conversation.xml index f1ba3562d..115b3a0da 100644 --- a/twidere/src/main/res/menu/menu_messages_conversation.xml +++ b/twidere/src/main/res/menu/menu_messages_conversation.xml @@ -18,8 +18,7 @@ ~ along with this program. If not, see . --> - +