1
0
mirror of https://github.com/TwidereProject/Twidere-Android synced 2025-02-17 04:00:48 +01:00

added fanfou get conversations

This commit is contained in:
Mariotaku Lee 2017-02-11 18:15:49 +08:00
parent 64b05c6bc1
commit 6284e575e9
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
6 changed files with 88 additions and 30 deletions

View File

@ -29,6 +29,7 @@ import org.mariotaku.microblog.library.twitter.model.ResponseList;
import org.mariotaku.restfu.annotation.method.GET;
import org.mariotaku.restfu.annotation.method.POST;
import org.mariotaku.restfu.annotation.param.Param;
import org.mariotaku.restfu.annotation.param.Query;
/**
* Created by mariotaku on 16/3/31.
@ -38,14 +39,12 @@ public interface DirectMessagesResources {
@POST("/direct_messages/new.json")
DirectMessage sendFanfouDirectMessage(@Param("user") String user, @Param("text") String text,
@Param("in_reply_to_id") String inReplyToId)
throws MicroBlogException;
@Param("in_reply_to_id") String inReplyToId) throws MicroBlogException;
@POST("/direct_messages/new.json")
DirectMessage sendFanfouDirectMessage(@Param("user") String user, @Param("text") String text)
throws MicroBlogException;
@GET("/direct_messages/conversation_list.json")
ResponseList<Conversation> getConversationList(@Param Paging paging)
throws MicroBlogException;
ResponseList<Conversation> getConversationList(@Query Paging paging) throws MicroBlogException;
}

View File

@ -93,8 +93,7 @@ public class MicroBlogAPIFactory implements TwidereConstants {
}
@WorkerThread
public static MicroBlog getInstance(@NonNull final Context context,
@NonNull final UserKey accountKey) {
public static MicroBlog getInstance(@NonNull final Context context, @NonNull final UserKey accountKey) {
final AccountManager am = AccountManager.get(context);
final Account account = AccountUtils.findByAccountKey(am, accountKey);
if (account == null) return null;
@ -106,7 +105,8 @@ public class MicroBlogAPIFactory implements TwidereConstants {
}
@NonNull
public static HashMap<String, String> getExtraParams(@NonNull @AccountType String accountType, boolean includeEntities, boolean includeRetweets) {
public static HashMap<String, String> getExtraParams(@NonNull @AccountType String accountType,
boolean includeEntities, boolean includeRetweets) {
final HashMap<String, String> extraParams = new HashMap<>();
switch (accountType) {
case AccountType.FANFOU: {
@ -243,14 +243,14 @@ public class MicroBlogAPIFactory implements TwidereConstants {
}
public static Endpoint getOAuthEndpoint(String apiUrlFormat, @Nullable String domain,
@Nullable String versionSuffix,
boolean sameOAuthSigningUrl) {
@Nullable String versionSuffix,
boolean sameOAuthSigningUrl) {
return getOAuthEndpoint(apiUrlFormat, domain, versionSuffix, sameOAuthSigningUrl, false);
}
public static Endpoint getOAuthEndpoint(@NonNull String apiUrlFormat, @Nullable String domain,
@Nullable String versionSuffix,
boolean sameOAuthSigningUrl, boolean fixUrl) {
@Nullable String versionSuffix,
boolean sameOAuthSigningUrl, boolean fixUrl) {
String endpointUrl, signEndpointUrl;
endpointUrl = getApiUrl(apiUrlFormat, domain, versionSuffix);
if (fixUrl) {
@ -297,8 +297,8 @@ public class MicroBlogAPIFactory implements TwidereConstants {
@Override
public <E extends Exception> HttpRequest create(@NonNull Endpoint endpoint, @NonNull RestRequest info,
@Nullable Authorization authorization,
RestConverter.Factory<E> converterFactory)
@Nullable Authorization authorization,
RestConverter.Factory<E> converterFactory)
throws IOException, RestConverter.ConvertException, E {
final String restMethod = info.getMethod();
final String url = Endpoint.constructUrl(endpoint.getUrl(), info);
@ -362,8 +362,8 @@ public class MicroBlogAPIFactory implements TwidereConstants {
@Override
public RestRequest create(RestMethod<MicroBlogException> restMethod,
RestConverter.Factory<MicroBlogException> factory,
ValueMap valuePool) throws RestConverter.ConvertException, IOException, MicroBlogException {
RestConverter.Factory<MicroBlogException> factory,
ValueMap valuePool) throws RestConverter.ConvertException, IOException, MicroBlogException {
final HttpMethod method = restMethod.getMethod();
final String path = restMethod.getPath();
final MultiValueMap<String> headers = restMethod.getHeaders(valuePool);

View File

@ -8,7 +8,7 @@ import org.mariotaku.microblog.library.MicroBlogException
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
import org.mariotaku.twidere.provider.TwidereDataStore.Statuses
import org.mariotaku.twidere.task.twitter.GetStatusesTask
import org.mariotaku.twidere.util.ErrorInfoStore
@ -23,7 +23,7 @@ class GetHomeTimelineTask(context: Context) : GetStatusesTask(context) {
}
override val contentUri: Uri
get() = TwidereDataStore.Statuses.CONTENT_URI
get() = Statuses.CONTENT_URI
@TimelineType
override val timelineType: String

View File

@ -3,6 +3,7 @@ package org.mariotaku.twidere.task
import android.accounts.AccountManager
import android.annotation.SuppressLint
import android.content.Context
import org.mariotaku.ktextension.toInt
import org.mariotaku.ktextension.useCursor
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
@ -54,11 +55,11 @@ class GetMessagesTask(
bus.post(GetMessagesTaskEvent(Messages.CONTENT_URI, false, null))
}
private fun getMessages(microBlog: MicroBlog, details: AccountDetails, param: RefreshTaskParam, index: Int): GetMessagesData {
private fun getMessages(microBlog: MicroBlog, details: AccountDetails, param: RefreshMessagesTaskParam, index: Int): GetMessagesData {
when (details.type) {
AccountType.FANFOU -> {
// Use fanfou DM api
return getFanfouMessages(microBlog)
return getFanfouMessages(microBlog, details, param, index)
}
AccountType.TWITTER -> {
// Use official DM api
@ -71,15 +72,20 @@ class GetMessagesTask(
return getDefaultMessages(microBlog, details, param, index)
}
private fun getFanfouMessages(microBlog: MicroBlog): GetMessagesData {
return GetMessagesData(emptyList(), emptyList())
private fun getFanfouMessages(microBlog: MicroBlog, details: AccountDetails, param: RefreshMessagesTaskParam, index: Int): GetMessagesData {
val conversationId = param.conversationId
if (conversationId == null) {
return getFanfouConversations(microBlog, details, param, index)
} else {
return GetMessagesData(emptyList(), emptyList())
}
}
private fun getTwitterOfficialMessages(microBlog: MicroBlog, details: AccountDetails, param: RefreshTaskParam, index: Int): GetMessagesData {
private fun getTwitterOfficialMessages(microBlog: MicroBlog, details: AccountDetails, param: RefreshMessagesTaskParam, index: Int): GetMessagesData {
return getDefaultMessages(microBlog, details, param, index)
}
private fun getDefaultMessages(microBlog: MicroBlog, details: AccountDetails, param: RefreshTaskParam, index: Int): GetMessagesData {
private fun getDefaultMessages(microBlog: MicroBlog, details: AccountDetails, param: RefreshMessagesTaskParam, index: Int): GetMessagesData {
val accountKey = details.key
val sinceIds = if (param.hasSinceIds) param.sinceIds else null
@ -136,6 +142,35 @@ class GetMessagesTask(
return GetMessagesData(conversations.values, insertMessages)
}
private fun getFanfouConversations(microBlog: MicroBlog, details: AccountDetails, param: RefreshMessagesTaskParam, index: Int): GetMessagesData {
val accountKey = details.key
val cursor = param.cursors?.get(index)
val page = cursor?.substringAfter("page:").toInt(-1)
val result = microBlog.getConversationList(Paging().apply {
count(60)
if (page >= 0) {
page(page)
}
})
val conversations = hashMapOf<String, ParcelableMessageConversation>()
val conversationIds = hashSetOf<String>()
result.mapTo(conversationIds) { "${accountKey.id}-${it.otherId}" }
conversations.addLocalConversations(accountKey, conversationIds)
result.forEachIndexed { i, item ->
val dm = item.dm
// Sender is our self, treat as outgoing message
val message = if (dm.senderId == accountKey.id) {
ParcelableMessageUtils.outgoingMessage(accountKey, dm, 1.0 - (i.toDouble() / result.size))
} else {
ParcelableMessageUtils.incomingMessage(accountKey, dm, 1.0 - (i.toDouble() / result.size))
}
val mc = conversations.addConversation(details, message, dm.sender, dm.recipient)
mc.request_cursor = "page:$page"
}
return GetMessagesData(conversations.values, emptyList())
}
@SuppressLint("Recycle")
private fun MutableMap<String, ParcelableMessageConversation>.addLocalConversations(accountKey: UserKey, conversationIds: Set<String>) {
val where = Expression.and(Expression.inArgs(Conversations.CONVERSATION_ID, conversationIds.size),
@ -199,7 +234,7 @@ class GetMessagesTask(
details: AccountDetails,
message: ParcelableMessage,
vararg users: User
) {
): ParcelableMessageConversation {
val conversation = this[message.conversation_id] ?: run {
val obj = ParcelableMessageConversation()
obj.id = message.conversation_id
@ -214,6 +249,7 @@ class GetMessagesTask(
users.forEach { user ->
conversation.addParticipant(details.key, user)
}
return conversation
}

View File

@ -42,6 +42,7 @@ class MessageEntryViewHolder(itemView: View, val adapter: MessagesEntriesAdapter
private val name by lazy { itemView.name }
private val text by lazy { itemView.text }
private val profileImage by lazy { itemView.profileImage }
private val stateIndicator by lazy { itemView.stateIndicator }
init {
setup()
@ -50,7 +51,7 @@ class MessageEntryViewHolder(itemView: View, val adapter: MessagesEntriesAdapter
fun display(conversation: ParcelableMessageConversation) {
if (adapter.drawAccountColors) {
content.drawEnd(conversation.account_color)
}else {
} else {
content.drawEnd()
}
val (name, secondaryName) = conversation.getConversationName(itemView.context)
@ -59,6 +60,12 @@ class MessageEntryViewHolder(itemView: View, val adapter: MessagesEntriesAdapter
this.name.screenName = secondaryName
this.name.updateText(adapter.bidiFormatter)
this.text.text = conversation.getSummaryText(itemView.context)
if (conversation.is_outgoing) {
stateIndicator.visibility = View.VISIBLE
stateIndicator.setImageResource(R.drawable.ic_activity_action_reply)
} else {
stateIndicator.visibility = View.GONE
}
if (conversation.conversation_type == ConversationType.ONE_TO_ONE) {
val user = conversation.user
if (user != null) {

View File

@ -31,7 +31,8 @@
android:paddingLeft="@dimen/element_spacing_normal"
android:paddingRight="@dimen/element_spacing_normal"
app:ignorePadding="true"
tools:context=".adapter.MessagesEntriesAdapter">
tools:context=".adapter.MessagesEntriesAdapter"
tools:layout_height="?android:listPreferredItemHeight">
<org.mariotaku.twidere.view.ProfileImageView
android:id="@+id/profileImage"
@ -90,18 +91,33 @@
android:textColor="?android:attr/textColorSecondary"
tools:text="12:00"/>
<org.mariotaku.twidere.view.FixedTextView
<org.mariotaku.twidere.view.TimelineContentTextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_alignStart="@+id/name"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/name"
android:layout_marginTop="@dimen/element_spacing_xsmall"
android:layout_toEndOf="@+id/stateIndicator"
android:layout_toRightOf="@+id/stateIndicator"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="?android:attr/textColorSecondary"
tools:text="@string/sample_status_text"/>
<org.mariotaku.twidere.view.IconActionView
android:id="@+id/stateIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/text"
android:layout_alignLeft="@+id/name"
android:layout_alignStart="@+id/name"
android:layout_alignTop="@+id/text"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@drawable/ic_activity_action_reply"/>
</RelativeLayout>
</org.mariotaku.twidere.view.ColorLabelRelativeLayout>