From d1472f0397c81356c012c4654b3b2c2476e72d1c Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Wed, 19 Apr 2017 19:30:07 +0800 Subject: [PATCH] hides other users' favs for mastodon --- .../util/stetho/AccountsDumperPlugin.kt | 30 ++++++++++++++++++- .../twidere/fragment/UserFragment.kt | 18 ++++++----- .../twidere/loader/MediaTimelineLoader.kt | 21 ++++++++++--- .../twidere/loader/UserTimelineLoader.kt | 8 +++++ 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/twidere/src/debug/kotlin/org/mariotaku/twidere/util/stetho/AccountsDumperPlugin.kt b/twidere/src/debug/kotlin/org/mariotaku/twidere/util/stetho/AccountsDumperPlugin.kt index a6f329ebe..c0f927565 100644 --- a/twidere/src/debug/kotlin/org/mariotaku/twidere/util/stetho/AccountsDumperPlugin.kt +++ b/twidere/src/debug/kotlin/org/mariotaku/twidere/util/stetho/AccountsDumperPlugin.kt @@ -69,7 +69,8 @@ class AccountsDumperPlugin(val context: Context) : DumperPlugin { override fun dump(dumpContext: DumperContext) { val argsAsList = dumpContext.argsAsList val subCommands = listOf(ExportCommand(context), ImportCommand(context), - ListCommand(context), GetCommand(context), SetCommand(context)) + ListCommand(context), GetCommand(context), SetCommand(context), + SetJsonCommand(context)) val subCommandName = argsAsList.firstOrNull() val subCommand = subCommands.find { it.name == subCommandName } ?: run { throw DumpException("Usage: accounts <${subCommands.joinToString("|", transform = SubCommand::name)}> ") @@ -210,6 +211,33 @@ class AccountsDumperPlugin(val context: Context) : DumperPlugin { } + class SetJsonCommand(val context: Context) : SubCommand("set-json") { + override fun execute(dumpContext: DumperContext, args: Array) { + if (args.size != 3) { + throw DumpException("Usage: accounts $name ") + } + val am = AccountManager.get(context) + val docContext = try { + am.docContext(args[0]) + } catch (e: Utils.NoAccountException) { + throw DumpException("Account not found") + } + val value = args[2] + val path = args[1] + if (value.startsWith("{")) { + docContext.set(path, JSONObject(value)) + } else if (value.startsWith("[")) { + docContext.set(path, JSONArray(value)) + } + val details = docContext.read("$", Object::class.java)?.let { + JsonSerializer.parse(it.toString(), AccountDetails::class.java) + } ?: return + details.account.updateDetails(am, details) + dumpContext.stdout.println("$path = ${docContext.read(path, Object::class.java)?.prettyPrint()}") + } + + } + companion object { private val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1") 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 79b9c60a7..2f6eea2e0 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt @@ -1463,14 +1463,16 @@ class UserFragment : BaseFragment(), OnClickListener, OnLinkClickListener, position = TAB_POSITION_STATUSES) pagerAdapter.add(cls = UserMediaTimelineFragment::class.java, args = tabArgs, name = getString(R.string.media), type = TAB_TYPE_MEDIA, position = TAB_POSITION_MEDIA) - if (preferences[iWantMyStarsBackKey]) { - pagerAdapter.add(cls = UserFavoritesFragment::class.java, args = tabArgs, - name = getString(R.string.title_favorites), type = TAB_TYPE_FAVORITES, - position = TAB_POSITION_FAVORITES) - } else { - pagerAdapter.add(cls = UserFavoritesFragment::class.java, args = tabArgs, - name = getString(R.string.title_likes), type = TAB_TYPE_FAVORITES, - position = TAB_POSITION_FAVORITES) + if (account?.type != AccountType.MASTODON || account?.key == userKey) { + if (preferences[iWantMyStarsBackKey]) { + pagerAdapter.add(cls = UserFavoritesFragment::class.java, args = tabArgs, + name = getString(R.string.title_favorites), type = TAB_TYPE_FAVORITES, + position = TAB_POSITION_FAVORITES) + } else { + pagerAdapter.add(cls = UserFavoritesFragment::class.java, args = tabArgs, + name = getString(R.string.title_likes), type = TAB_TYPE_FAVORITES, + position = TAB_POSITION_FAVORITES) + } } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/MediaTimelineLoader.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/MediaTimelineLoader.kt index eef856bdb..1c6e6caf3 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/MediaTimelineLoader.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/MediaTimelineLoader.kt @@ -25,8 +25,10 @@ import android.support.annotation.WorkerThread import org.mariotaku.ktextension.isNullOrEmpty import org.mariotaku.microblog.library.MicroBlog import org.mariotaku.microblog.library.MicroBlogException +import org.mariotaku.microblog.library.mastodon.Mastodon import org.mariotaku.microblog.library.twitter.model.* import org.mariotaku.twidere.annotation.AccountType +import org.mariotaku.twidere.extension.model.api.mastodon.toParcelable import org.mariotaku.twidere.extension.model.api.toParcelable import org.mariotaku.twidere.extension.model.isOfficial import org.mariotaku.twidere.extension.model.newMicroBlogInstance @@ -36,7 +38,7 @@ import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.util.DataStoreUtils import org.mariotaku.twidere.util.InternalTwitterContentUtils import org.mariotaku.twidere.util.TwitterWrapper - +import org.mariotaku.microblog.library.mastodon.model.TimelineOption as MastodonTimelineOption class MediaTimelineLoader( context: Context, @@ -62,14 +64,17 @@ class MediaTimelineLoader( return userKey.maybeEquals(accountKey) } else { val accountScreenName = DataStoreUtils.getAccountScreenName(context, accountKey) - return accountScreenName != null && accountScreenName.equals(screenName!!, ignoreCase = true) + return accountScreenName != null && accountScreenName.equals(screenName, ignoreCase = true) } } @Throws(MicroBlogException::class) override fun getStatuses(account: AccountDetails, paging: Paging): List { - return getMicroBlogStatuses(account, paging).map { - it.toParcelable(account.key, account.type, profileImageSize) + when (account.type) { + AccountType.MASTODON -> return getMastodonStatuses(account, paging) + else -> return getMicroBlogStatuses(account, paging).map { + it.toParcelable(account.key, account.type, profileImageSize) + } } } @@ -127,4 +132,12 @@ class MediaTimelineLoader( } throw MicroBlogException("Not implemented") } + + private fun getMastodonStatuses(account: AccountDetails, paging: Paging): List { + val mastodon = account.newMicroBlogInstance(context, Mastodon::class.java) + val option = MastodonTimelineOption() + option.onlyMedia(true) + return UserTimelineLoader.getMastodonStatuses(mastodon, userKey, screenName, paging, + option).map { it.toParcelable(account.key) } + } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserTimelineLoader.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserTimelineLoader.kt index 6194e4b62..f6491f2b2 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserTimelineLoader.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserTimelineLoader.kt @@ -191,4 +191,12 @@ class UserTimelineLoader( } } } + + companion object { + fun getMastodonStatuses(mastodon: Mastodon, userKey: UserKey?, screenName: String?, paging: Paging, + option: MastodonTimelineOption?): List { + val id = userKey?.id ?: throw MicroBlogException("Only ID are supported at this moment") + return mastodon.getStatuses(id, paging, option) + } + } }