fixed translation not displayed

fixed quick search bar search button
This commit is contained in:
Mariotaku Lee 2015-04-28 21:50:11 +08:00
parent 49593c0c4e
commit 72506e853f
6 changed files with 157 additions and 147 deletions

View File

@ -1459,7 +1459,8 @@ final class TwitterImpl extends TwitterBaseImpl implements Twitter {
final String signUrl = conf.getSigningRestBaseURL() + ENDPOINT_TRANSLATIONS_SHOW;
final HttpParameter paramStatus = new HttpParameter("id", statusId);
final HttpParameter paramDest = new HttpParameter("dest", dest);
return factory.createTranslationResult(get(url, signUrl, paramStatus, paramDest));
return factory.createTranslationResult(get(url, signUrl, paramStatus, paramDest, INCLUDE_ENTITIES,
INCLUDE_CARDS));
}
@Override

View File

@ -353,7 +353,6 @@ public abstract class AbsStatusesFragment<Data> extends AbsContentRecyclerViewFr
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mReadStateManager = getReadStateManager();
final FragmentActivity activity = getActivity();
final AbsStatusesAdapter<Data> adapter = getAdapter();
final RecyclerView recyclerView = getRecyclerView();
final LinearLayoutManager layoutManager = getLayoutManager();

View File

@ -82,7 +82,13 @@ public class StatusTranslateDialogFragment extends BaseSupportDialogFragment imp
mProgressBar.setVisibility(View.VISIBLE);
mMessageView.setVisibility(View.VISIBLE);
mMessageView.setText(R.string.please_wait);
return new TranslationResultLoader(getActivity(), status.account_id, status.id);
final long statusId;
if (status.is_retweet) {
statusId = status.retweet_id;
} else {
statusId = status.id;
}
return new TranslationResultLoader(getActivity(), status.account_id, statusId);
}
@Override
@ -124,7 +130,7 @@ public class StatusTranslateDialogFragment extends BaseSupportDialogFragment imp
private void displayTranslatedStatus(final ParcelableStatus status, final TranslationResult translated) {
if (status == null || translated == null) return;
mHolder.displayStatus(status, null, false, true);
mHolder.displayStatus(status, translated, false, true);
mStatusContainer.findViewById(R.id.item_menu).setVisibility(View.GONE);
mStatusContainer.findViewById(R.id.action_buttons).setVisibility(View.GONE);

View File

@ -53,172 +53,172 @@ import static org.mariotaku.twidere.util.Utils.getDefaultTwitterInstance;
public class TranslationDestinationPreference extends Preference implements Constants, OnClickListener {
private SharedPreferences mPreferences;
private SharedPreferences mPreferences;
private String mSelectedLanguageCode = "en";
private String mSelectedLanguageCode = "en";
private GetLanguagesTask mGetAvailableTrendsTask;
private GetLanguagesTask mGetAvailableTrendsTask;
private final LanguagesAdapter mAdapter;
private final LanguagesAdapter mAdapter;
private AlertDialog mDialog;
private AlertDialog mDialog;
public TranslationDestinationPreference(final Context context) {
this(context, null);
}
public TranslationDestinationPreference(final Context context) {
this(context, null);
}
public TranslationDestinationPreference(final Context context, final AttributeSet attrs) {
this(context, attrs, android.R.attr.preferenceStyle);
}
public TranslationDestinationPreference(final Context context, final AttributeSet attrs) {
this(context, attrs, android.R.attr.preferenceStyle);
}
public TranslationDestinationPreference(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
mAdapter = new LanguagesAdapter(context);
}
public TranslationDestinationPreference(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
mAdapter = new LanguagesAdapter(context);
}
@Override
public void onClick(final DialogInterface dialog, final int which) {
final SharedPreferences.Editor editor = getEditor();
if (editor == null) return;
final Language item = mAdapter.getItem(which);
if (item != null) {
editor.putString(KEY_TRANSLATION_DESTINATION, item.getCode());
editor.commit();
}
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
}
@Override
public void onClick(final DialogInterface dialog, final int which) {
final SharedPreferences.Editor editor = getEditor();
if (editor == null) return;
final Language item = mAdapter.getItem(which);
if (item != null) {
editor.putString(KEY_TRANSLATION_DESTINATION, item.getCode());
editor.commit();
}
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
}
@Override
protected void onClick() {
mPreferences = getSharedPreferences();
if (mPreferences == null) return;
if (mGetAvailableTrendsTask != null) {
mGetAvailableTrendsTask.cancel(false);
}
mGetAvailableTrendsTask = new GetLanguagesTask(getContext());
mGetAvailableTrendsTask.execute();
}
@Override
protected void onClick() {
mPreferences = getSharedPreferences();
if (mPreferences == null) return;
if (mGetAvailableTrendsTask != null) {
mGetAvailableTrendsTask.cancel(false);
}
mGetAvailableTrendsTask = new GetLanguagesTask(getContext());
mGetAvailableTrendsTask.execute();
}
private static class LanguageComparator implements Comparator<Language> {
private final Collator mCollator;
private static class LanguageComparator implements Comparator<Language> {
private final Collator mCollator;
LanguageComparator(final Context context) {
mCollator = Collator.getInstance(context.getResources().getConfiguration().locale);
}
LanguageComparator(final Context context) {
mCollator = Collator.getInstance(context.getResources().getConfiguration().locale);
}
@Override
public int compare(final Language object1, final Language object2) {
return mCollator.compare(object1.getName(), object2.getName());
}
@Override
public int compare(final Language object1, final Language object2) {
return mCollator.compare(object1.getName(), object2.getName());
}
}
}
private static class LanguagesAdapter extends ArrayAdapter<Language> {
private static class LanguagesAdapter extends ArrayAdapter<Language> {
private final Context mContext;
private final Context mContext;
public LanguagesAdapter(final Context context) {
super(context, android.R.layout.simple_list_item_single_choice);
mContext = context;
}
public LanguagesAdapter(final Context context) {
super(context, android.R.layout.simple_list_item_single_choice);
mContext = context;
}
public int findItemPosition(final String code) {
if (TextUtils.isEmpty(code)) return -1;
final int count = getCount();
for (int i = 0; i < count; i++) {
final Language item = getItem(i);
if (code.equalsIgnoreCase(item.getCode())) return i;
}
return -1;
}
public int findItemPosition(final String code) {
if (TextUtils.isEmpty(code)) return -1;
final int count = getCount();
for (int i = 0; i < count; i++) {
final Language item = getItem(i);
if (code.equalsIgnoreCase(item.getCode())) return i;
}
return -1;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
final TextView text = (TextView) (view instanceof TextView ? view : view.findViewById(android.R.id.text1));
final Language item = getItem(position);
if (item != null && text != null) {
text.setSingleLine();
text.setText(item.getName());
}
return view;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
final TextView text = (TextView) (view instanceof TextView ? view : view.findViewById(android.R.id.text1));
final Language item = getItem(position);
if (item != null && text != null) {
text.setSingleLine();
text.setText(item.getName());
}
return view;
}
public void setData(final List<Language> data) {
clear();
if (data != null) {
addAll(data);
}
sort(new LanguageComparator(mContext));
}
public void setData(final List<Language> data) {
clear();
if (data != null) {
addAll(data);
}
sort(new LanguageComparator(mContext));
}
}
}
class GetLanguagesTask extends AsyncTask<Object, Object, ResponseList<Language>> implements OnCancelListener {
class GetLanguagesTask extends AsyncTask<Object, Object, ResponseList<Language>> implements OnCancelListener {
private final ProgressDialog mProgress;
private final ProgressDialog mProgress;
public GetLanguagesTask(final Context context) {
mProgress = new ProgressDialog(context);
}
public GetLanguagesTask(final Context context) {
mProgress = new ProgressDialog(context);
}
@Override
public void onCancel(final DialogInterface dialog) {
cancel(true);
}
@Override
public void onCancel(final DialogInterface dialog) {
cancel(true);
}
@Override
protected ResponseList<Language> doInBackground(final Object... args) {
final Twitter twitter = getDefaultTwitterInstance(getContext(), false);
final String pref = mPreferences.getString(KEY_TRANSLATION_DESTINATION, null);
if (twitter == null) return null;
try {
if (pref == null) {
mSelectedLanguageCode = twitter.getAccountSettings().getLanguage();
final Editor editor = mPreferences.edit();
editor.putString(KEY_TRANSLATION_DESTINATION, mSelectedLanguageCode);
editor.apply();
} else {
mSelectedLanguageCode = pref;
}
return twitter.getLanguages();
} catch (final TwitterException e) {
e.printStackTrace();
}
return null;
}
@Override
protected ResponseList<Language> doInBackground(final Object... args) {
final Twitter twitter = getDefaultTwitterInstance(getContext(), false);
final String pref = mPreferences.getString(KEY_TRANSLATION_DESTINATION, null);
if (twitter == null) return null;
try {
if (pref == null) {
mSelectedLanguageCode = twitter.getAccountSettings().getLanguage();
final Editor editor = mPreferences.edit();
editor.putString(KEY_TRANSLATION_DESTINATION, mSelectedLanguageCode);
editor.apply();
} else {
mSelectedLanguageCode = pref;
}
return twitter.getLanguages();
} catch (final TwitterException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(final ResponseList<Language> result) {
if (mProgress != null && mProgress.isShowing()) {
mProgress.dismiss();
}
mAdapter.setData(result);
if (result == null) return;
final AlertDialog.Builder selectorBuilder = new AlertDialog.Builder(getContext());
selectorBuilder.setTitle(getTitle());
selectorBuilder.setSingleChoiceItems(mAdapter, mAdapter.findItemPosition(mSelectedLanguageCode),
TranslationDestinationPreference.this);
selectorBuilder.setNegativeButton(android.R.string.cancel, null);
mDialog = selectorBuilder.create();
final ListView lv = mDialog.getListView();
if (lv != null) {
lv.setFastScrollEnabled(true);
}
mDialog.show();
}
@Override
protected void onPostExecute(final ResponseList<Language> result) {
if (mProgress.isShowing()) {
mProgress.dismiss();
}
mAdapter.setData(result);
if (result == null) return;
final AlertDialog.Builder selectorBuilder = new AlertDialog.Builder(getContext());
selectorBuilder.setTitle(getTitle());
selectorBuilder.setSingleChoiceItems(mAdapter, mAdapter.findItemPosition(mSelectedLanguageCode),
TranslationDestinationPreference.this);
selectorBuilder.setNegativeButton(android.R.string.cancel, null);
mDialog = selectorBuilder.create();
final ListView lv = mDialog.getListView();
if (lv != null) {
lv.setFastScrollEnabled(true);
}
mDialog.show();
}
@Override
protected void onPreExecute() {
if (mProgress != null && mProgress.isShowing()) {
mProgress.dismiss();
}
mProgress.setMessage(getContext().getString(R.string.please_wait));
mProgress.setOnCancelListener(this);
mProgress.show();
}
@Override
protected void onPreExecute() {
if (mProgress.isShowing()) {
mProgress.dismiss();
}
mProgress.setMessage(getContext().getString(R.string.please_wait));
mProgress.setOnCancelListener(this);
mProgress.show();
}
}
}
}

View File

@ -177,7 +177,9 @@ public class StatusViewHolder extends ViewHolder implements Constants, OnClickLi
screenNameView.setText("@" + status.quoted_by_user_screen_name);
final int idx = status.quote_text_unescaped.lastIndexOf(" twitter.com");
if (adapter.getLinkHighlightingStyle() == VALUE_LINK_HIGHLIGHT_OPTION_CODE_NONE) {
if (translation != null) {
quoteTextView.setText(translation.getText());
} else if (adapter.getLinkHighlightingStyle() == VALUE_LINK_HIGHLIGHT_OPTION_CODE_NONE) {
final String text = status.quote_text_unescaped;
quoteTextView.setText(idx > 0 ? text.substring(0, idx) : text);
} else {
@ -269,7 +271,9 @@ public class StatusViewHolder extends ViewHolder implements Constants, OnClickLi
} else {
mediaPreview.setVisibility(View.GONE);
}
if (adapter.getLinkHighlightingStyle() == VALUE_LINK_HIGHLIGHT_OPTION_CODE_NONE) {
if (!status.is_quote && translation != null) {
textView.setText(translation.getText());
} else if (adapter.getLinkHighlightingStyle() == VALUE_LINK_HIGHLIGHT_OPTION_CODE_NONE) {
textView.setText(status.text_unescaped);
} else {
textView.setText(Html.fromHtml(status.text_html));

View File

@ -71,7 +71,7 @@
android:layout_width="@dimen/element_size_normal"
android:layout_height="match_parent"
android:layout_weight="0"
android:background="?android:actionBarItemBackground"
android:background="?actionBarItemBackground"
android:color="?menuIconColor"
android:src="@drawable/ic_action_search" />