答案编辑改进
This commit is contained in:
@ -18,7 +18,18 @@ import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber
|
||||
import com.facebook.imagepipeline.image.ImageInfo
|
||||
import com.facebook.imagepipeline.request.ImageRequest
|
||||
import com.facebook.imagepipeline.request.ImageRequestBuilder
|
||||
import com.gh.common.constant.Config
|
||||
import com.gh.gamecenter.R
|
||||
import com.gh.gamecenter.manager.UserManager
|
||||
import com.lightgame.download.FileUtils
|
||||
import com.lightgame.utils.Utils
|
||||
import org.json.JSONObject
|
||||
import rx.Observable
|
||||
import rx.Observer
|
||||
import rx.android.schedulers.AndroidSchedulers
|
||||
import rx.schedulers.Schedulers
|
||||
import java.io.File
|
||||
import java.net.HttpURLConnection
|
||||
|
||||
class ImageUtils private constructor() {
|
||||
|
||||
@ -102,15 +113,66 @@ class ImageUtils private constructor() {
|
||||
}
|
||||
|
||||
fun display(simpleDraweeView: SimpleDraweeView, url: String?) {
|
||||
// if (url.startsWith("http://image.ghzs666.com") && url.endsWith(".jpg")) {
|
||||
// url = url + "?x-oss-process=image/format,webp";
|
||||
// }
|
||||
simpleDraweeView.setImageURI(url)
|
||||
}
|
||||
|
||||
fun display(draweeView: SimpleDraweeView, @DrawableRes res: Int?) {
|
||||
draweeView.setImageURI("res:///" + res)
|
||||
}
|
||||
|
||||
fun postImageArr(context: Context, imgArr: List<String>, listener: OnPostImageListener) {
|
||||
val imgMap: HashMap<String, String> = HashMap()
|
||||
|
||||
Observable.create(Observable.OnSubscribe<JSONObject> { subscriber ->
|
||||
var path: String
|
||||
var index = 0
|
||||
for (s in imgArr) {
|
||||
path = context.getCacheDir().path + File.separator + System.currentTimeMillis() + index + ".jpg"
|
||||
if (BitmapUtils.savePicture(path, s)) {
|
||||
subscriber.onNext(FileUtils.uploadFile(Config.API_HOST + "support/upload/img?type=suggestion", path, s, UserManager.getInstance().token))
|
||||
index++
|
||||
} else {
|
||||
subscriber.onNext(FileUtils.uploadFile(Config.API_HOST + "support/upload/img?type=suggestion", s, s, UserManager.getInstance().token))
|
||||
}
|
||||
}
|
||||
subscriber.onCompleted()
|
||||
}).subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(object : Observer<JSONObject> {
|
||||
override fun onCompleted() {
|
||||
Utils.log("图片上传完成")
|
||||
listener.postSuccess(imgMap)
|
||||
}
|
||||
|
||||
override fun onError(e: Throwable) {
|
||||
Utils.log("图片上传失败" + e.toString())
|
||||
listener.postError()
|
||||
}
|
||||
|
||||
override fun onNext(result: JSONObject?) {
|
||||
if (result != null) {
|
||||
try {
|
||||
val statusCode = result.getInt("statusCode")
|
||||
if (statusCode == HttpURLConnection.HTTP_OK) {
|
||||
imgMap.put(result.getString("realPath"), result.getString("icon"))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
interface OnPostImageListener {
|
||||
/**
|
||||
* key: 图片本地路径
|
||||
* value: 图片提交成功后的链接
|
||||
*/
|
||||
fun postSuccess(imgMap: HashMap<String, String>)
|
||||
|
||||
fun postError()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ import com.gh.gamecenter.ask.questionsdetail.QuestionsInviteFragment;
|
||||
* Created by khy on 7/12/17.
|
||||
*/
|
||||
|
||||
public class AskQuestionsDetailActivity extends BaseActivity implements FragmentManager.OnBackStackChangedListener {
|
||||
public class AskQuestionsDetailActivity extends BaseActivity implements FragmentManager.OnBackStackChangedListener, View.OnClickListener {
|
||||
|
||||
public static final String QUESTIONS_DETAIL_ANSWER = "answer";
|
||||
public static final String QUESTIONS_DETAIL_INVITE = "invite";
|
||||
@ -28,6 +28,7 @@ public class AskQuestionsDetailActivity extends BaseActivity implements Fragment
|
||||
public static final String QUESTIONS_DETAIL = "questions_detail";
|
||||
|
||||
private QuestionsDetailFragment mQuestionsDetailFragment;
|
||||
private AnswerEditFragment mAnswerEditFragment;
|
||||
|
||||
private View mShareIv;
|
||||
private View mAnswerPost;
|
||||
@ -67,6 +68,16 @@ public class AskQuestionsDetailActivity extends BaseActivity implements Fragment
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if (view == mAnswerPost) {
|
||||
mAnswerEditFragment.postAnswer();
|
||||
} else if (view == mShareIv) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void addShareView() {
|
||||
RelativeLayout reuse_actionbar = mContentView.findViewById(R.id.reuse_actionbar);
|
||||
mShareIv = LayoutInflater.from(this).inflate(R.layout.menu_action_share, reuse_actionbar, false);
|
||||
@ -74,6 +85,7 @@ public class AskQuestionsDetailActivity extends BaseActivity implements Fragment
|
||||
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
||||
params.addRule(RelativeLayout.CENTER_VERTICAL);
|
||||
reuse_actionbar.addView(mShareIv, params);
|
||||
mShareIv.setOnClickListener(this);
|
||||
}
|
||||
|
||||
private void addAnswerPostView() {
|
||||
@ -84,6 +96,7 @@ public class AskQuestionsDetailActivity extends BaseActivity implements Fragment
|
||||
params.addRule(RelativeLayout.CENTER_VERTICAL);
|
||||
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
||||
reuse_actionbar.addView(mAnswerPost, params);
|
||||
mAnswerPost.setOnClickListener(this);
|
||||
}
|
||||
|
||||
public void show(String type) {
|
||||
@ -129,11 +142,12 @@ public class AskQuestionsDetailActivity extends BaseActivity implements Fragment
|
||||
}
|
||||
|
||||
private void showAnswerEdit(String type) {
|
||||
mAnswerEditFragment = new AnswerEditFragment();
|
||||
getSupportFragmentManager()
|
||||
.beginTransaction()
|
||||
.addToBackStack(type)
|
||||
.replace(R.id.layout_fragment_content,
|
||||
new AnswerEditFragment(), null).commit();
|
||||
mAnswerEditFragment, null).commit();
|
||||
}
|
||||
|
||||
private void showMenu(String type) {
|
||||
|
||||
@ -11,7 +11,7 @@ import android.widget.TextView;
|
||||
|
||||
import com.gh.base.adapter.FragmentAdapter;
|
||||
import com.gh.base.fragment.BaseFragment;
|
||||
import com.gh.gamecenter.AskSearchActivity;
|
||||
import com.gh.gamecenter.AskQuestionsDetailActivity;
|
||||
import com.gh.gamecenter.NormalActivity;
|
||||
import com.gh.gamecenter.R;
|
||||
import com.lightgame.view.NoScrollableViewPager;
|
||||
@ -62,7 +62,8 @@ public class AskFragment extends BaseFragment {
|
||||
NormalActivity.startFragment(getContext(), SelectGameFragment.class);
|
||||
break;
|
||||
case R.id.ask_search:
|
||||
startActivity(AskSearchActivity.getIntent(getContext()));
|
||||
startActivity(AskQuestionsDetailActivity.getIntent(getContext(), null));
|
||||
// startActivity(AskSearchActivity.getIntent(getContext()));
|
||||
break;
|
||||
case R.id.ask_hot:
|
||||
setTabbarPosition(INDEX_HOT);
|
||||
|
||||
@ -19,10 +19,17 @@ import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.gh.base.fragment.BaseFragment;
|
||||
import com.gh.common.util.ImageUtils;
|
||||
import com.gh.gamecenter.R;
|
||||
import com.gh.gamecenter.fragment.WaitingDialogFragment;
|
||||
import com.lightgame.utils.Utils;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.OnClick;
|
||||
@ -41,6 +48,8 @@ public class AnswerEditFragment extends BaseFragment {
|
||||
@BindView(R.id.answer_edit_img_icon)
|
||||
ImageView mImgIcon;
|
||||
|
||||
private List<String> mImgArr = new ArrayList<>();
|
||||
private WaitingDialogFragment postDialog;
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
@ -64,7 +73,7 @@ public class AnswerEditFragment extends BaseFragment {
|
||||
|
||||
Bitmap bitmap = compressPicture(picturePath);
|
||||
|
||||
insertPicToEt(bitmap); // todo 插入图片前应该向服务器提交图片
|
||||
insertPicToEt(bitmap, picturePath);
|
||||
|
||||
Utils.log("picturePath = " + picturePath);
|
||||
}
|
||||
@ -80,11 +89,36 @@ public class AnswerEditFragment extends BaseFragment {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
}
|
||||
|
||||
public void postAnswer() {
|
||||
if (getContext() == null) return;
|
||||
postDialog = WaitingDialogFragment.newInstance(getString(R.string.vote_post));
|
||||
postDialog.show(getChildFragmentManager(), null);
|
||||
ImageUtils.Companion.postImageArr(getContext(), mImgArr, new ImageUtils.OnPostImageListener() {
|
||||
@Override
|
||||
public void postSuccess(@NotNull HashMap<String, String> imgMap) {
|
||||
if (imgMap.size() != mImgArr.size()) {
|
||||
toast("有部分图片提交失败");
|
||||
}
|
||||
String answerContent = mEditContent.getText().toString();
|
||||
for (String s : imgMap.keySet()) {
|
||||
answerContent = answerContent.replace(s, imgMap.get(s));
|
||||
}
|
||||
postDialog.dismissAllowingStateLoss();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postError() {
|
||||
postDialog.dismissAllowingStateLoss();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩图片
|
||||
* 规则:
|
||||
* 1.如果图片大于当前EditText的最大宽度则按比例缩放,使图片宽度和EditText的宽度保持一致 (高度不处理)
|
||||
* 2.如果图片小于当前EditText 由于比例限制 将不处理
|
||||
*
|
||||
* @param srcPath 图片路径
|
||||
* @return
|
||||
*/
|
||||
@ -101,42 +135,53 @@ public class AnswerEditFragment extends BaseFragment {
|
||||
// 缩放图片的尺寸
|
||||
float w = op.outWidth;
|
||||
float h = op.outHeight;
|
||||
float index = w/h;
|
||||
float index = w / h;
|
||||
int width = mEditContent.getWidth();
|
||||
float hh = width/index;
|
||||
float hh = width / index;
|
||||
float ww = width;
|
||||
|
||||
float be = 1.0f;
|
||||
if (w > h && w > ww) {
|
||||
be = (float) (w / ww);
|
||||
be = w / ww;
|
||||
} else if (w < h && h > hh) {
|
||||
be = (float) (h / hh);
|
||||
}
|
||||
if (be <= 0) {
|
||||
be = 1.0f;
|
||||
be = h / hh;
|
||||
} else if (w > h && w > ww / 2) {
|
||||
be = w / ww;
|
||||
} else if (w < h && h > hh / 2) {
|
||||
be = h / hh;
|
||||
}
|
||||
// if (be <= 0) {
|
||||
// be = 1.0f;
|
||||
// }
|
||||
Utils.log("==========" + w + "==" + h + "==" + ww + "==" + hh + "==" + be);
|
||||
op.inSampleSize = (int) be;// 设置缩放比例,这个数字越大,图片大小越小.
|
||||
// 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
|
||||
bitmap = BitmapFactory.decodeFile(srcPath, op);
|
||||
int desWidth = (int) (w / be);
|
||||
int desHeight = (int) (h / be);
|
||||
return Bitmap.createScaledBitmap(bitmap, desWidth, desHeight, true);
|
||||
return Bitmap.createScaledBitmap(bitmap, desWidth, desHeight, true);
|
||||
|
||||
}
|
||||
|
||||
@OnClick(R.id.answer_edit_img_icon)
|
||||
@OnClick({R.id.answer_edit_img_icon})
|
||||
public void onClick(View view) {
|
||||
if (view.getId() == R.id.answer_edit_img_icon) {
|
||||
if (mImgArr.size() >= 20) {
|
||||
toast("最多只能上次20张图片");
|
||||
return;
|
||||
}
|
||||
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
||||
startActivityForResult(intent, MEDIA_ICON_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
private void insertPicToEt(Bitmap bitmap) {
|
||||
private void insertPicToEt(Bitmap bitmap, String picturePath) {
|
||||
if (bitmap == null) return;
|
||||
mImgArr.add(picturePath);
|
||||
|
||||
ImageSpan imageSpan = new ImageSpan(getContext(), bitmap);
|
||||
int index = mEditContent.getSelectionStart();
|
||||
String tempUrl = "<img src=\"" + "dasdasdasdasddasd" + "\" />";
|
||||
String tempUrl = "<img src=\"" + picturePath + "\" />";
|
||||
SpannableString spannableString = new SpannableString(tempUrl);
|
||||
spannableString.setSpan(imageSpan, 0, tempUrl.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
Editable editableText = mEditContent.getEditableText();
|
||||
@ -145,8 +190,7 @@ public class AnswerEditFragment extends BaseFragment {
|
||||
} else {
|
||||
editableText.insert(index, spannableString);
|
||||
}
|
||||
|
||||
Utils.log("insertPicToEt = ");
|
||||
|
||||
if (bitmap.getWidth() >= mEditContent.getWidth())
|
||||
editableText.insert(index + spannableString.length(), "\n");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,11 +1,9 @@
|
||||
package com.gh.gamecenter.ask.questionsdetail;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.gh.base.OnListClickListener;
|
||||
import com.gh.common.constant.ItemViewType;
|
||||
@ -93,27 +91,27 @@ public class QuestionsDetailAdapter extends ListAdapter {
|
||||
|
||||
private void initQuestionsDetailItemViewHolder(QuestionsDetailItemViewHolder holder) {
|
||||
|
||||
holder.mTagRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
|
||||
holder.mTagRv.setAdapter(new RecyclerView.Adapter() {
|
||||
@Override
|
||||
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view = mLayoutInflater.inflate(R.layout.questionsdetail_tag_item, parent, false);
|
||||
return new ReuseViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
||||
View item = holder.itemView;
|
||||
if (item instanceof TextView) {
|
||||
TextView tag = (TextView) item;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
// holder.mTagRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
|
||||
// holder.mTagRv.setAdapter(new RecyclerView.Adapter() {
|
||||
// @Override
|
||||
// public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
// View view = mLayoutInflater.inflate(R.layout.questionsdetail_tag_item, parent, false);
|
||||
// return new ReuseViewHolder(view);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
||||
// View item = holder.itemView;
|
||||
// if (item instanceof TextView) {
|
||||
// TextView tag = (TextView) item;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public int getItemCount() {
|
||||
// return 0;
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -43,7 +43,7 @@ class OkHttpRetryInterceptor internal constructor(context: Context) : Intercepto
|
||||
var tryCount = 0
|
||||
val token = chain.request().header("token")
|
||||
val url = request.url().toString()
|
||||
while ((response == null || (token != null && response.code() == 401 && (!url.contains("REFRESH")))) // 排除刷新token接口,避免进入死循环
|
||||
while ((response == null || (token != null && response.code() == 401 && (!url.contains("refresh")))) // 排除刷新token接口,避免进入死循环
|
||||
&& ++tryCount <= mMaxRetryCount) {
|
||||
if (CommonDebug.IS_DEBUG) {
|
||||
CommonDebug.logMethodWithParams(this, "Retrying ${request.url()} for $tryCount")
|
||||
|
||||
@ -714,12 +714,12 @@ public interface ApiService {
|
||||
* 社区问题详情
|
||||
*/
|
||||
@GET("questions/{questions_id}")
|
||||
Observable<QuestionsEntity> getQuestionsById(@Path("community_id") String questionsId);
|
||||
Observable<QuestionsEntity> getQuestionsById(@Path("questions_id") String questionsId);
|
||||
|
||||
/**
|
||||
* 社区问题的答案列表
|
||||
*/
|
||||
@GET("questions/{questions_id}/answers")
|
||||
Observable<List<AnswerEntity>> getQuestionsAnswer(@Path("community_id") String questionsId, @Query("fold") boolean isFold);
|
||||
Observable<List<AnswerEntity>> getQuestionsAnswer(@Path("questions_id") String questionsId, @Query("fold") boolean isFold);
|
||||
|
||||
}
|
||||
@ -55,7 +55,7 @@ public interface UserseaService {
|
||||
* 刷新accessToken
|
||||
*/
|
||||
@Headers({"Content-Type: application/json", "Accept: application/json"})
|
||||
@POST("tokens/REFRESH")
|
||||
@POST("tokens/refresh")
|
||||
Observable<LoginTokenEntity> refreshToken(@Body RequestBody body);
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user