improved api instance creation
This commit is contained in:
parent
d819fa9bd1
commit
64b05c6bc1
|
@ -21,10 +21,14 @@
|
||||||
|
|
||||||
package org.mariotaku.microblog.library.fanfou.api;
|
package org.mariotaku.microblog.library.fanfou.api;
|
||||||
|
|
||||||
|
import org.mariotaku.microblog.library.MicroBlogException;
|
||||||
|
import org.mariotaku.microblog.library.fanfou.model.Conversation;
|
||||||
|
import org.mariotaku.microblog.library.twitter.model.DirectMessage;
|
||||||
|
import org.mariotaku.microblog.library.twitter.model.Paging;
|
||||||
|
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.method.POST;
|
||||||
import org.mariotaku.restfu.annotation.param.Param;
|
import org.mariotaku.restfu.annotation.param.Param;
|
||||||
import org.mariotaku.microblog.library.MicroBlogException;
|
|
||||||
import org.mariotaku.microblog.library.twitter.model.DirectMessage;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by mariotaku on 16/3/31.
|
* Created by mariotaku on 16/3/31.
|
||||||
|
@ -41,4 +45,7 @@ public interface DirectMessagesResources {
|
||||||
DirectMessage sendFanfouDirectMessage(@Param("user") String user, @Param("text") String text)
|
DirectMessage sendFanfouDirectMessage(@Param("user") String user, @Param("text") String text)
|
||||||
throws MicroBlogException;
|
throws MicroBlogException;
|
||||||
|
|
||||||
|
@GET("/direct_messages/conversation_list.json")
|
||||||
|
ResponseList<Conversation> getConversationList(@Param Paging paging)
|
||||||
|
throws MicroBlogException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,99 @@
|
||||||
|
/*
|
||||||
|
* Twidere - Twitter client for Android
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.mariotaku.microblog.library.fanfou.model;
|
||||||
|
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.os.Parcelable;
|
||||||
|
|
||||||
|
import com.bluelinelabs.logansquare.annotation.JsonField;
|
||||||
|
import com.bluelinelabs.logansquare.annotation.JsonObject;
|
||||||
|
import com.hannesdorfmann.parcelableplease.annotation.ParcelablePlease;
|
||||||
|
|
||||||
|
import org.mariotaku.microblog.library.twitter.model.DirectMessage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fanfou DM conversation object
|
||||||
|
* <p>
|
||||||
|
* Created by mariotaku on 2017/2/11.
|
||||||
|
*
|
||||||
|
* @see <a href="https://github.com/FanfouAPI/FanFouAPIDoc/wiki/direct-messages.conversation-list">GET /privete_messages/conversation_list</a>
|
||||||
|
*/
|
||||||
|
@ParcelablePlease
|
||||||
|
@JsonObject
|
||||||
|
public class Conversation implements Parcelable {
|
||||||
|
@JsonField(name = "dm")
|
||||||
|
DirectMessage dm;
|
||||||
|
@JsonField(name = "otherid")
|
||||||
|
String otherId;
|
||||||
|
@JsonField(name = "msg_num")
|
||||||
|
int messageNumber;
|
||||||
|
@JsonField(name = "new_conv")
|
||||||
|
boolean newConversation;
|
||||||
|
|
||||||
|
public DirectMessage getDm() {
|
||||||
|
return dm;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMessageNumber() {
|
||||||
|
return messageNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isNewConversation() {
|
||||||
|
return newConversation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOtherId() {
|
||||||
|
return otherId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Conversation{" +
|
||||||
|
"dm=" + dm +
|
||||||
|
", otherId='" + otherId + '\'' +
|
||||||
|
", messageNumber=" + messageNumber +
|
||||||
|
", newConversation=" + newConversation +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int describeContents() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
|
ConversationParcelablePlease.writeToParcel(this, dest, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Creator<Conversation> CREATOR = new Creator<Conversation>() {
|
||||||
|
public Conversation createFromParcel(Parcel source) {
|
||||||
|
Conversation target = new Conversation();
|
||||||
|
ConversationParcelablePlease.readFromParcel(target, source);
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Conversation[] newArray(int size) {
|
||||||
|
return new Conversation[size];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
|
@ -101,8 +101,8 @@ public class MicroBlogAPIFactory implements TwidereConstants {
|
||||||
final Credentials credentials = AccountExtensionsKt.getCredentials(account, am);
|
final Credentials credentials = AccountExtensionsKt.getCredentials(account, am);
|
||||||
final String accountType = AccountExtensionsKt.getAccountType(account, am);
|
final String accountType = AccountExtensionsKt.getAccountType(account, am);
|
||||||
final HashMap<String, String> extraParams = getExtraParams(accountType, true, true);
|
final HashMap<String, String> extraParams = getExtraParams(accountType, true, true);
|
||||||
return CredentialsExtensionsKt.newMicroBlogInstance(credentials, context,
|
return CredentialsExtensionsKt.newMicroBlogInstance(credentials, context, accountType,
|
||||||
AccountType.TWITTER.equals(accountType), extraParams, MicroBlog.class);
|
extraParams, MicroBlog.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
|
|
@ -203,7 +203,7 @@ class BrowserSignInActivity : BaseActivity() {
|
||||||
val endpoint = MicroBlogAPIFactory.getOAuthSignInEndpoint(apiConfig.apiUrlFormat,
|
val endpoint = MicroBlogAPIFactory.getOAuthSignInEndpoint(apiConfig.apiUrlFormat,
|
||||||
apiConfig.isSameOAuthUrl)
|
apiConfig.isSameOAuthUrl)
|
||||||
val auth = OAuthAuthorization(apiConfig.consumerKey, apiConfig.consumerSecret)
|
val auth = OAuthAuthorization(apiConfig.consumerKey, apiConfig.consumerSecret)
|
||||||
val oauth = newMicroBlogInstance(activity, endpoint, auth, true, null,
|
val oauth = newMicroBlogInstance(activity, endpoint, auth, apiConfig.type, null,
|
||||||
TwitterOAuth::class.java)
|
TwitterOAuth::class.java)
|
||||||
return SingleResponse(oauth.getRequestToken(TwidereConstants.OAUTH_CALLBACK_OOB))
|
return SingleResponse(oauth.getRequestToken(TwidereConstants.OAUTH_CALLBACK_OOB))
|
||||||
} catch (e: MicroBlogException) {
|
} catch (e: MicroBlogException) {
|
||||||
|
|
|
@ -596,6 +596,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, APIEditorDi
|
||||||
apiConfig.isSameOAuthUrl)
|
apiConfig.isSameOAuthUrl)
|
||||||
val oauth = newMicroBlogInstance(context, endpoint = endpoint,
|
val oauth = newMicroBlogInstance(context, endpoint = endpoint,
|
||||||
auth = OAuthAuthorization(apiConfig.consumerKey, apiConfig.consumerSecret),
|
auth = OAuthAuthorization(apiConfig.consumerKey, apiConfig.consumerSecret),
|
||||||
|
accountType = apiConfig.type,
|
||||||
cls = TwitterOAuth::class.java)
|
cls = TwitterOAuth::class.java)
|
||||||
val accessToken: OAuthToken
|
val accessToken: OAuthToken
|
||||||
if (oauthVerifier != null) {
|
if (oauthVerifier != null) {
|
||||||
|
@ -609,7 +610,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, APIEditorDi
|
||||||
apiConfig.isSameOAuthUrl)
|
apiConfig.isSameOAuthUrl)
|
||||||
|
|
||||||
val twitter = newMicroBlogInstance(context, endpoint = endpoint, auth = auth,
|
val twitter = newMicroBlogInstance(context, endpoint = endpoint, auth = auth,
|
||||||
cls = MicroBlog::class.java)
|
accountType = apiConfig.type, cls = MicroBlog::class.java)
|
||||||
val apiUser = twitter.verifyCredentials()
|
val apiUser = twitter.verifyCredentials()
|
||||||
var color = analyseUserProfileColor(apiUser)
|
var color = analyseUserProfileColor(apiUser)
|
||||||
val accountType = SignInActivity.detectAccountType(twitter, apiUser, apiConfig.type)
|
val accountType = SignInActivity.detectAccountType(twitter, apiUser, apiConfig.type)
|
||||||
|
@ -845,7 +846,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, APIEditorDi
|
||||||
val auth = OAuthAuthorization(apiConfig.consumerKey,
|
val auth = OAuthAuthorization(apiConfig.consumerKey,
|
||||||
apiConfig.consumerSecret)
|
apiConfig.consumerSecret)
|
||||||
val oauth = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
val oauth = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
||||||
cls = TwitterOAuth::class.java)
|
accountType = apiConfig.type, cls = TwitterOAuth::class.java)
|
||||||
val authenticator = OAuthPasswordAuthenticator(oauth,
|
val authenticator = OAuthPasswordAuthenticator(oauth,
|
||||||
verificationCallback, userAgent)
|
verificationCallback, userAgent)
|
||||||
val accessToken = authenticator.getOAuthAccessToken(username, password)
|
val accessToken = authenticator.getOAuthAccessToken(username, password)
|
||||||
|
@ -862,7 +863,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, APIEditorDi
|
||||||
var auth = OAuthAuthorization(apiConfig.consumerKey,
|
var auth = OAuthAuthorization(apiConfig.consumerKey,
|
||||||
apiConfig.consumerSecret)
|
apiConfig.consumerSecret)
|
||||||
val oauth = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
val oauth = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
||||||
cls = TwitterOAuth::class.java)
|
accountType = apiConfig.type, cls = TwitterOAuth::class.java)
|
||||||
val accessToken = oauth.getAccessToken(username, password)
|
val accessToken = oauth.getAccessToken(username, password)
|
||||||
var userId: String? = accessToken.userId
|
var userId: String? = accessToken.userId
|
||||||
if (userId == null) {
|
if (userId == null) {
|
||||||
|
@ -872,7 +873,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, APIEditorDi
|
||||||
endpoint = MicroBlogAPIFactory.getOAuthRestEndpoint(apiUrlFormat, apiConfig.isSameOAuthUrl,
|
endpoint = MicroBlogAPIFactory.getOAuthRestEndpoint(apiUrlFormat, apiConfig.isSameOAuthUrl,
|
||||||
apiConfig.isNoVersionSuffix)
|
apiConfig.isNoVersionSuffix)
|
||||||
val microBlog = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
val microBlog = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
||||||
cls = MicroBlog::class.java)
|
accountType = apiConfig.type, cls = MicroBlog::class.java)
|
||||||
userId = microBlog.verifyCredentials().id
|
userId = microBlog.verifyCredentials().id
|
||||||
}
|
}
|
||||||
return getOAuthSignInResponse(activity, accessToken, userId!!, Credentials.Type.XAUTH)
|
return getOAuthSignInResponse(activity, accessToken, userId!!, Credentials.Type.XAUTH)
|
||||||
|
@ -886,7 +887,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, APIEditorDi
|
||||||
versionSuffix))
|
versionSuffix))
|
||||||
val auth = BasicAuthorization(username, password)
|
val auth = BasicAuthorization(username, password)
|
||||||
val twitter = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
val twitter = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
||||||
cls = MicroBlog::class.java)
|
accountType = apiConfig.type, cls = MicroBlog::class.java)
|
||||||
val apiUser: User
|
val apiUser: User
|
||||||
try {
|
try {
|
||||||
apiUser = twitter.verifyCredentials()
|
apiUser = twitter.verifyCredentials()
|
||||||
|
@ -927,7 +928,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, APIEditorDi
|
||||||
versionSuffix))
|
versionSuffix))
|
||||||
val auth = EmptyAuthorization()
|
val auth = EmptyAuthorization()
|
||||||
val twitter = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
val twitter = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
||||||
cls = MicroBlog::class.java)
|
accountType = apiConfig.type, cls = MicroBlog::class.java)
|
||||||
val apiUser = twitter.verifyCredentials()
|
val apiUser = twitter.verifyCredentials()
|
||||||
val userId = apiUser.id!!
|
val userId = apiUser.id!!
|
||||||
var color = analyseUserProfileColor(apiUser)
|
var color = analyseUserProfileColor(apiUser)
|
||||||
|
@ -949,14 +950,14 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, APIEditorDi
|
||||||
|
|
||||||
@Throws(MicroBlogException::class)
|
@Throws(MicroBlogException::class)
|
||||||
private fun getOAuthSignInResponse(activity: SignInActivity,
|
private fun getOAuthSignInResponse(activity: SignInActivity,
|
||||||
accessToken: OAuthToken,
|
accessToken: OAuthToken,
|
||||||
userId: String, @Credentials.Type authType: String): SignInResponse {
|
userId: String, @Credentials.Type authType: String): SignInResponse {
|
||||||
val auth = OAuthAuthorization(apiConfig.consumerKey,
|
val auth = OAuthAuthorization(apiConfig.consumerKey,
|
||||||
apiConfig.consumerSecret, accessToken)
|
apiConfig.consumerSecret, accessToken)
|
||||||
val endpoint = MicroBlogAPIFactory.getOAuthRestEndpoint(apiUrlFormat,
|
val endpoint = MicroBlogAPIFactory.getOAuthRestEndpoint(apiUrlFormat,
|
||||||
apiConfig.isSameOAuthUrl, apiConfig.isNoVersionSuffix)
|
apiConfig.isSameOAuthUrl, apiConfig.isNoVersionSuffix)
|
||||||
val twitter = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
val twitter = newMicroBlogInstance(activity, endpoint = endpoint, auth = auth,
|
||||||
cls = MicroBlog::class.java)
|
accountType = apiConfig.type, cls = MicroBlog::class.java)
|
||||||
val apiUser = twitter.verifyCredentials()
|
val apiUser = twitter.verifyCredentials()
|
||||||
var color = analyseUserProfileColor(apiUser)
|
var color = analyseUserProfileColor(apiUser)
|
||||||
val accountType = SignInActivity.detectAccountType(twitter, apiUser, apiConfig.type)
|
val accountType = SignInActivity.detectAccountType(twitter, apiUser, apiConfig.type)
|
||||||
|
|
|
@ -38,10 +38,11 @@ fun <T> AccountDetails.newMicroBlogInstance(
|
||||||
context: Context,
|
context: Context,
|
||||||
includeEntities: Boolean = true,
|
includeEntities: Boolean = true,
|
||||||
includeRetweets: Boolean = true,
|
includeRetweets: Boolean = true,
|
||||||
extraRequestParams: Map<String, String>? = MicroBlogAPIFactory.getExtraParams(type, includeEntities, includeRetweets),
|
extraRequestParams: Map<String, String>? = MicroBlogAPIFactory.getExtraParams(type,
|
||||||
|
includeEntities, includeRetweets),
|
||||||
cls: Class<T>
|
cls: Class<T>
|
||||||
): T {
|
): T {
|
||||||
return credentials.newMicroBlogInstance(context, type == AccountType.TWITTER, extraRequestParams, cls)
|
return credentials.newMicroBlogInstance(context, type, extraRequestParams, cls)
|
||||||
}
|
}
|
||||||
|
|
||||||
val AccountDetails.is_oauth: Boolean
|
val AccountDetails.is_oauth: Boolean
|
||||||
|
|
|
@ -11,11 +11,11 @@ import org.mariotaku.microblog.library.twitter.util.TwitterConverterFactory
|
||||||
import org.mariotaku.restfu.RestAPIFactory
|
import org.mariotaku.restfu.RestAPIFactory
|
||||||
import org.mariotaku.restfu.http.Authorization
|
import org.mariotaku.restfu.http.Authorization
|
||||||
import org.mariotaku.restfu.http.Endpoint
|
import org.mariotaku.restfu.http.Endpoint
|
||||||
import org.mariotaku.restfu.http.SimpleValueMap
|
|
||||||
import org.mariotaku.restfu.oauth.OAuthAuthorization
|
import org.mariotaku.restfu.oauth.OAuthAuthorization
|
||||||
import org.mariotaku.restfu.oauth.OAuthEndpoint
|
import org.mariotaku.restfu.oauth.OAuthEndpoint
|
||||||
import org.mariotaku.restfu.oauth.OAuthToken
|
import org.mariotaku.restfu.oauth.OAuthToken
|
||||||
import org.mariotaku.twidere.TwidereConstants.DEFAULT_TWITTER_API_URL_FORMAT
|
import org.mariotaku.twidere.TwidereConstants.DEFAULT_TWITTER_API_URL_FORMAT
|
||||||
|
import org.mariotaku.twidere.annotation.AccountType
|
||||||
import org.mariotaku.twidere.model.ConsumerKeyType
|
import org.mariotaku.twidere.model.ConsumerKeyType
|
||||||
import org.mariotaku.twidere.model.account.cred.BasicCredentials
|
import org.mariotaku.twidere.model.account.cred.BasicCredentials
|
||||||
import org.mariotaku.twidere.model.account.cred.Credentials
|
import org.mariotaku.twidere.model.account.cred.Credentials
|
||||||
|
@ -23,6 +23,7 @@ import org.mariotaku.twidere.model.account.cred.EmptyCredentials
|
||||||
import org.mariotaku.twidere.model.account.cred.OAuthCredentials
|
import org.mariotaku.twidere.model.account.cred.OAuthCredentials
|
||||||
import org.mariotaku.twidere.util.HttpClientFactory
|
import org.mariotaku.twidere.util.HttpClientFactory
|
||||||
import org.mariotaku.twidere.util.MicroBlogAPIFactory
|
import org.mariotaku.twidere.util.MicroBlogAPIFactory
|
||||||
|
import org.mariotaku.twidere.util.MicroBlogAPIFactory.sFanfouConstantPool
|
||||||
import org.mariotaku.twidere.util.MicroBlogAPIFactory.sTwitterConstantPool
|
import org.mariotaku.twidere.util.MicroBlogAPIFactory.sTwitterConstantPool
|
||||||
import org.mariotaku.twidere.util.TwitterContentUtils
|
import org.mariotaku.twidere.util.TwitterContentUtils
|
||||||
import org.mariotaku.twidere.util.dagger.DependencyHolder
|
import org.mariotaku.twidere.util.dagger.DependencyHolder
|
||||||
|
@ -100,24 +101,15 @@ fun Credentials.getEndpoint(cls: Class<*>): Endpoint {
|
||||||
return Endpoint(endpointUrl)
|
return Endpoint(endpointUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> Credentials.newMicroBlogInstance(
|
fun <T> Credentials.newMicroBlogInstance(context: Context, @AccountType accountType: String? = null,
|
||||||
context: Context,
|
extraRequestParams: Map<String, String>? = null, cls: Class<T>): T {
|
||||||
twitterExtraQueries: Boolean = true,
|
return newMicroBlogInstance(context, getEndpoint(cls), getAuthorization(), accountType,
|
||||||
extraRequestParams: Map<String, String>? = null,
|
extraRequestParams, cls)
|
||||||
cls: Class<T>
|
|
||||||
): T {
|
|
||||||
return newMicroBlogInstance(context, getEndpoint(cls), getAuthorization(),
|
|
||||||
twitterExtraQueries, extraRequestParams, cls)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> newMicroBlogInstance(
|
fun <T> newMicroBlogInstance(context: Context, endpoint: Endpoint, auth: Authorization,
|
||||||
context: Context,
|
@AccountType accountType: String? = null, extraRequestParams: Map<String, String>? = null,
|
||||||
endpoint: Endpoint,
|
cls: Class<T>): T {
|
||||||
auth: Authorization,
|
|
||||||
twitterExtraQueries: Boolean = true,
|
|
||||||
extraRequestParams: Map<String, String>? = null,
|
|
||||||
cls: Class<T>
|
|
||||||
): T {
|
|
||||||
val factory = RestAPIFactory<MicroBlogException>()
|
val factory = RestAPIFactory<MicroBlogException>()
|
||||||
val userAgent: String
|
val userAgent: String
|
||||||
if (auth is OAuthAuthorization) {
|
if (auth is OAuthAuthorization) {
|
||||||
|
@ -149,10 +141,13 @@ fun <T> newMicroBlogInstance(
|
||||||
}
|
}
|
||||||
factory.setAuthorization(auth)
|
factory.setAuthorization(auth)
|
||||||
factory.setEndpoint(endpoint)
|
factory.setEndpoint(endpoint)
|
||||||
if (twitterExtraQueries) {
|
when (accountType) {
|
||||||
factory.setConstantPool(sTwitterConstantPool)
|
AccountType.TWITTER -> {
|
||||||
} else {
|
factory.setConstantPool(sTwitterConstantPool)
|
||||||
factory.setConstantPool(SimpleValueMap())
|
}
|
||||||
|
AccountType.FANFOU -> {
|
||||||
|
factory.setConstantPool(sFanfouConstantPool)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
val converterFactory = TwitterConverterFactory()
|
val converterFactory = TwitterConverterFactory()
|
||||||
factory.setRestConverterFactory(converterFactory)
|
factory.setRestConverterFactory(converterFactory)
|
||||||
|
|
|
@ -19,9 +19,17 @@
|
||||||
|
|
||||||
package org.mariotaku.twidere.view.holder.message
|
package org.mariotaku.twidere.view.holder.message
|
||||||
|
|
||||||
|
import android.os.Build
|
||||||
|
import android.support.v4.view.GravityCompat
|
||||||
import android.support.v7.widget.RecyclerView
|
import android.support.v7.widget.RecyclerView
|
||||||
|
import android.text.format.DateUtils
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
import android.widget.FrameLayout
|
||||||
|
import android.widget.LinearLayout
|
||||||
|
import android.widget.RelativeLayout
|
||||||
|
import android.widget.TextView
|
||||||
import org.mariotaku.twidere.adapter.MessagesConversationAdapter
|
import org.mariotaku.twidere.adapter.MessagesConversationAdapter
|
||||||
|
import org.mariotaku.twidere.extension.model.timestamp
|
||||||
import org.mariotaku.twidere.model.ParcelableMessage
|
import org.mariotaku.twidere.model.ParcelableMessage
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,7 +38,41 @@ import org.mariotaku.twidere.model.ParcelableMessage
|
||||||
|
|
||||||
abstract class AbsMessageViewHolder(itemView: View, val adapter: MessagesConversationAdapter) : RecyclerView.ViewHolder(itemView) {
|
abstract class AbsMessageViewHolder(itemView: View, val adapter: MessagesConversationAdapter) : RecyclerView.ViewHolder(itemView) {
|
||||||
|
|
||||||
open fun display(message: ParcelableMessage, showDate: Boolean) {
|
protected abstract val date: TextView
|
||||||
|
protected abstract val messageContent: View
|
||||||
|
|
||||||
|
open fun display(message: ParcelableMessage, showDate: Boolean) {
|
||||||
|
setMessageContentGravity(messageContent, message.is_outgoing)
|
||||||
|
if (showDate) {
|
||||||
|
date.visibility = View.VISIBLE
|
||||||
|
date.text = DateUtils.getRelativeTimeSpanString(message.timestamp, System.currentTimeMillis(),
|
||||||
|
DateUtils.DAY_IN_MILLIS, DateUtils.FORMAT_SHOW_DATE)
|
||||||
|
} else {
|
||||||
|
date.visibility = View.GONE
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open fun setMessageContentGravity(view: View, outgoing: Boolean) {
|
||||||
|
val lp = view.layoutParams
|
||||||
|
when (lp) {
|
||||||
|
is FrameLayout.LayoutParams -> {
|
||||||
|
lp.gravity = if (outgoing) GravityCompat.END else GravityCompat.START
|
||||||
|
}
|
||||||
|
is LinearLayout.LayoutParams -> {
|
||||||
|
lp.gravity = if (outgoing) GravityCompat.END else GravityCompat.START
|
||||||
|
}
|
||||||
|
is RelativeLayout.LayoutParams -> {
|
||||||
|
val endRule = if (outgoing) 1 else 0
|
||||||
|
val startRule = if (outgoing) 0 else 1
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||||
|
lp.addRule(RelativeLayout.ALIGN_PARENT_START, startRule)
|
||||||
|
lp.addRule(RelativeLayout.ALIGN_PARENT_END, endRule)
|
||||||
|
} else {
|
||||||
|
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, startRule)
|
||||||
|
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, endRule)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,12 +19,9 @@
|
||||||
|
|
||||||
package org.mariotaku.twidere.view.holder.message
|
package org.mariotaku.twidere.view.holder.message
|
||||||
|
|
||||||
import android.support.v4.view.GravityCompat
|
|
||||||
import android.text.SpannableStringBuilder
|
import android.text.SpannableStringBuilder
|
||||||
import android.text.format.DateUtils
|
import android.text.format.DateUtils
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.FrameLayout
|
|
||||||
import android.widget.LinearLayout
|
|
||||||
import kotlinx.android.synthetic.main.list_item_message_conversation_text.view.*
|
import kotlinx.android.synthetic.main.list_item_message_conversation_text.view.*
|
||||||
import org.mariotaku.ktextension.empty
|
import org.mariotaku.ktextension.empty
|
||||||
import org.mariotaku.ktextension.isNullOrEmpty
|
import org.mariotaku.ktextension.isNullOrEmpty
|
||||||
|
@ -35,6 +32,7 @@ import org.mariotaku.twidere.extension.model.applyTo
|
||||||
import org.mariotaku.twidere.extension.model.timestamp
|
import org.mariotaku.twidere.extension.model.timestamp
|
||||||
import org.mariotaku.twidere.model.ParcelableMessage
|
import org.mariotaku.twidere.model.ParcelableMessage
|
||||||
import org.mariotaku.twidere.model.SpanItem
|
import org.mariotaku.twidere.model.SpanItem
|
||||||
|
import org.mariotaku.twidere.view.FixedTextView
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by mariotaku on 2017/2/9.
|
* Created by mariotaku on 2017/2/9.
|
||||||
|
@ -42,11 +40,12 @@ import org.mariotaku.twidere.model.SpanItem
|
||||||
|
|
||||||
class MessageViewHolder(itemView: View, adapter: MessagesConversationAdapter) : AbsMessageViewHolder(itemView, adapter) {
|
class MessageViewHolder(itemView: View, adapter: MessagesConversationAdapter) : AbsMessageViewHolder(itemView, adapter) {
|
||||||
|
|
||||||
private val date by lazy { itemView.date }
|
override val date: FixedTextView by lazy { itemView.date }
|
||||||
|
override val messageContent: MessageBubbleView by lazy { itemView.messageContent }
|
||||||
|
|
||||||
private val text by lazy { itemView.text }
|
private val text by lazy { itemView.text }
|
||||||
private val time by lazy { itemView.time }
|
private val time by lazy { itemView.time }
|
||||||
private val mediaPreview by lazy { itemView.mediaPreview }
|
private val mediaPreview by lazy { itemView.mediaPreview }
|
||||||
private val messageContent by lazy { itemView.messageContent }
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val textSize = adapter.textSize
|
val textSize = adapter.textSize
|
||||||
|
@ -58,15 +57,7 @@ class MessageViewHolder(itemView: View, adapter: MessagesConversationAdapter) :
|
||||||
|
|
||||||
override fun display(message: ParcelableMessage, showDate: Boolean) {
|
override fun display(message: ParcelableMessage, showDate: Boolean) {
|
||||||
super.display(message, showDate)
|
super.display(message, showDate)
|
||||||
setOutgoingStatus(messageContent, message.is_outgoing)
|
messageContent.setOutgoing(message.is_outgoing)
|
||||||
if (showDate) {
|
|
||||||
date.visibility = View.VISIBLE
|
|
||||||
date.text = DateUtils.getRelativeTimeSpanString(message.timestamp, System.currentTimeMillis(),
|
|
||||||
DateUtils.DAY_IN_MILLIS, DateUtils.FORMAT_SHOW_DATE)
|
|
||||||
} else {
|
|
||||||
date.visibility = View.GONE
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Loop through text and spans to found non-space char count
|
// Loop through text and spans to found non-space char count
|
||||||
val hideText = run {
|
val hideText = run {
|
||||||
|
@ -121,21 +112,8 @@ class MessageViewHolder(itemView: View, adapter: MessagesConversationAdapter) :
|
||||||
companion object {
|
companion object {
|
||||||
const val layoutResource = R.layout.list_item_message_conversation_text
|
const val layoutResource = R.layout.list_item_message_conversation_text
|
||||||
|
|
||||||
fun setOutgoingStatus(view: MessageBubbleView, outgoing: Boolean) {
|
fun MessageBubbleView.setOutgoing(outgoing: Boolean) {
|
||||||
view.setCaretPosition(if (outgoing) MessageBubbleView.BOTTOM_END else MessageBubbleView.BOTTOM_START)
|
setCaretPosition(if (outgoing) MessageBubbleView.BOTTOM_END else MessageBubbleView.BOTTOM_START)
|
||||||
setMessageContentGravity(view, outgoing)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setMessageContentGravity(view: View, outgoing: Boolean) {
|
|
||||||
val lp = view.layoutParams
|
|
||||||
when (lp) {
|
|
||||||
is FrameLayout.LayoutParams -> {
|
|
||||||
lp.gravity = if (outgoing) GravityCompat.END else GravityCompat.START
|
|
||||||
}
|
|
||||||
is LinearLayout.LayoutParams -> {
|
|
||||||
lp.gravity = if (outgoing) GravityCompat.END else GravityCompat.START
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,11 +20,13 @@
|
||||||
package org.mariotaku.twidere.view.holder.message
|
package org.mariotaku.twidere.view.holder.message
|
||||||
|
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
import android.widget.RelativeLayout
|
||||||
import kotlinx.android.synthetic.main.list_item_message_conversation_sticker.view.*
|
import kotlinx.android.synthetic.main.list_item_message_conversation_sticker.view.*
|
||||||
import org.mariotaku.twidere.R
|
import org.mariotaku.twidere.R
|
||||||
import org.mariotaku.twidere.adapter.MessagesConversationAdapter
|
import org.mariotaku.twidere.adapter.MessagesConversationAdapter
|
||||||
import org.mariotaku.twidere.model.ParcelableMessage
|
import org.mariotaku.twidere.model.ParcelableMessage
|
||||||
import org.mariotaku.twidere.model.message.StickerExtras
|
import org.mariotaku.twidere.model.message.StickerExtras
|
||||||
|
import org.mariotaku.twidere.view.FixedTextView
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by mariotaku on 2017/2/9.
|
* Created by mariotaku on 2017/2/9.
|
||||||
|
@ -32,12 +34,13 @@ import org.mariotaku.twidere.model.message.StickerExtras
|
||||||
|
|
||||||
class StickerMessageViewHolder(itemView: View, adapter: MessagesConversationAdapter) : AbsMessageViewHolder(itemView, adapter) {
|
class StickerMessageViewHolder(itemView: View, adapter: MessagesConversationAdapter) : AbsMessageViewHolder(itemView, adapter) {
|
||||||
|
|
||||||
private val messageContent by lazy { itemView.messageContent }
|
override val date: FixedTextView by lazy { itemView.date }
|
||||||
|
override val messageContent: RelativeLayout by lazy { itemView.messageContent }
|
||||||
|
|
||||||
private val stickerIcon by lazy { itemView.stickerIcon }
|
private val stickerIcon by lazy { itemView.stickerIcon }
|
||||||
|
|
||||||
override fun display(message: ParcelableMessage, showDate: Boolean) {
|
override fun display(message: ParcelableMessage, showDate: Boolean) {
|
||||||
super.display(message, showDate)
|
super.display(message, showDate)
|
||||||
MessageViewHolder.setMessageContentGravity(messageContent, message.is_outgoing)
|
|
||||||
val extras = message.extras as StickerExtras
|
val extras = message.extras as StickerExtras
|
||||||
adapter.mediaLoader.displayStickerImage(stickerIcon, extras.url)
|
adapter.mediaLoader.displayStickerImage(stickerIcon, extras.url)
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,16 +18,28 @@
|
||||||
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<FrameLayout
|
<RelativeLayout
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:padding="@dimen/element_spacing_normal">
|
android:padding="@dimen/element_spacing_normal">
|
||||||
|
|
||||||
|
<org.mariotaku.twidere.view.FixedTextView
|
||||||
|
android:id="@+id/date"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:padding="@dimen/element_spacing_normal"
|
||||||
|
android:textAppearance="?android:textAppearanceSmall"
|
||||||
|
android:textColor="?android:textColorTertiary"
|
||||||
|
tools:text="Yesterday"/>
|
||||||
|
|
||||||
<RelativeLayout
|
<RelativeLayout
|
||||||
android:id="@+id/messageContent"
|
android:id="@+id/messageContent"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_below="@+id/date">
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/stickerIcon"
|
android:id="@+id/stickerIcon"
|
||||||
|
@ -37,4 +49,4 @@
|
||||||
android:scaleType="fitCenter"/>
|
android:scaleType="fitCenter"/>
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
|
||||||
</FrameLayout>
|
</RelativeLayout>
|
|
@ -73,6 +73,7 @@
|
||||||
android:id="@+id/text"
|
android:id="@+id/text"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="@dimen/element_spacing_small"
|
||||||
android:textAppearance="?android:attr/textAppearanceSmall"
|
android:textAppearance="?android:attr/textAppearanceSmall"
|
||||||
android:textColor="?android:attr/textColorPrimary"
|
android:textColor="?android:attr/textColorPrimary"
|
||||||
tools:text="@string/sample_status_text"/>
|
tools:text="@string/sample_status_text"/>
|
||||||
|
@ -81,7 +82,6 @@
|
||||||
android:id="@+id/time"
|
android:id="@+id/time"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="@dimen/element_spacing_small"
|
|
||||||
android:textColor="?android:attr/textColorTertiary"
|
android:textColor="?android:attr/textColorTertiary"
|
||||||
tools:text="12:00"/>
|
tools:text="12:00"/>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
Loading…
Reference in New Issue