PublishQuestionAndTrendFragment.java 50.7 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9
package com.yidianling.dynamic.publishTrend.fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
霍志良 committed
10
import android.content.res.Resources;
konghaorui committed
11
import android.graphics.Point;
12
import android.graphics.Rect;
霍志良 committed
13
import android.os.Build;
konghaorui committed
14 15 16
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
17

霍志良 committed
18
import androidx.cardview.widget.CardView;
19
import androidx.constraintlayout.widget.ConstraintLayout;
YKai committed
20 21 22 23 24
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
霍志良 committed
25 26

import android.provider.Settings;
konghaorui committed
27 28 29 30 31 32 33 34
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
35
import android.view.ViewTreeObserver;
konghaorui committed
36 37 38 39 40 41 42 43
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

霍志良 committed
44
import com.blankj.utilcode.util.DeviceUtils;
konghaorui committed
45 46
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
konghaorui committed
47
import com.tbruyelle.rxpermissions2.RxPermissions;
霍志良 committed
48
import com.tencent.mmkv.MMKV;
konghaorui committed
49 50 51
import com.ydl.webview.H5Params;
import com.ydl.webview.NewH5Activity;
import com.ydl.ydl_image.module.GlideApp;
konghaorui committed
52
import com.ydl.ydlcommon.actions.imagepicker.YDLImagePicker;
konghaorui committed
53 54 55 56 57 58
import com.ydl.ydlcommon.base.BaseActivity;
import com.ydl.ydlcommon.base.BaseApp;
import com.ydl.ydlcommon.base.BaseFragment;
import com.ydl.ydlcommon.data.http.BaseResponse;
import com.ydl.ydlcommon.router.YdlCommonOut;
import com.ydl.ydlcommon.utils.BuryPointUtils;
霍志良 committed
59
import com.ydl.ydlcommon.utils.DisplayUtils;
konghaorui committed
60 61 62 63 64 65 66
import com.ydl.ydlcommon.utils.MoonUtil;
import com.ydl.ydlcommon.utils.SharedPreferencesEditor;
import com.ydl.ydlcommon.utils.StringUtils;
import com.ydl.ydlcommon.view.dialog.CommonDialog;
import com.ydl.ydlcommon.view.dialog.NormalDialog;
import com.yidianling.common.tools.LogUtil;
import com.yidianling.common.tools.RxFileTool;
霍志良 committed
67
import com.yidianling.dynamic.BuildConfig;
68
import com.yidianling.dynamic.bean.DynamicConstants;
konghaorui committed
69
import com.yidianling.dynamic.R;
霍志良 committed
70
import com.yidianling.dynamic.bean.DynamicDraftBean;
konghaorui committed
71 72 73 74 75
import com.yidianling.dynamic.common.browsePictures.BrowsePicturesActivity;
import com.yidianling.dynamic.common.emoji.EmoticonPickerView;
import com.yidianling.dynamic.common.emoji.IEmoticonSelectedListener;
import com.yidianling.dynamic.common.net.DynamicApiUtils;
import com.yidianling.dynamic.common.tool.ImageCompress;
霍志良 committed
76
import com.yidianling.dynamic.event.DynamicSaveEvent;
霍志良 committed
77
import com.yidianling.dynamic.event.EditContentEvent;
konghaorui committed
78 79
import com.yidianling.dynamic.model.Command;
import com.yidianling.dynamic.model.PublishTrendResult;
80
import com.ydl.ydlcommon.ui.ParcelableImage;
霍志良 committed
81
import com.yidianling.dynamic.publishTrend.DynamicPublishImageAdapter;
konghaorui committed
82 83 84 85
import com.yidianling.dynamic.publishTrend.PublishTrendActivity;
import com.yidianling.dynamic.publishTrend.SelecTopicActivity;
import com.yidianling.dynamic.router.DynamicIn;

霍志良 committed
86 87
import org.jetbrains.annotations.Nullable;

konghaorui committed
88 89 90 91
import java.io.File;
import java.util.ArrayList;
import java.util.List;

霍志良 committed
92
import de.greenrobot.event.EventBus;
konghaorui committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author jiucheng
 * @描述:提问、动态的fragment--从原有的发布动态activity移过来
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/10/12
 */

public class PublishQuestionAndTrendFragment extends BaseFragment implements View.OnClickListener, IEmoticonSelectedListener {
    /**
     * 动态
     */
    public static final String TYPE_TREND = "5";
    /**
     * 提问
     */
    public static final String TYPE_QUESTION = "6";
    /**
     * 页面标记:
     * TYPE_TREND:动态
     * TYPE_TREND:提问
     * <p>
     * ext_type 5动态 6问答
     * is_doctor_show 1仅专家可见 2所有可见
     */
    private String type;

    private TextView publish_trend_content_num_tv, publish_trend_bottom_topic_tv;
霍志良 committed
126
    public EditText publish_trend_title_editext, publish_trend_content_editext;
konghaorui committed
127 128
    private RecyclerView publish_trend_img_rcv;
    private ImageView publish_trend_bottom_img_iv;
霍志良 committed
129
    private View lineTitle;
konghaorui committed
130 131 132 133
    private LinearLayout publish_anonymity_rel;
    private LinearLayout ll_to_expert;
    private ImageView publish_anonymity_iv;
    private RelativeLayout publish_trend_ad_rel;
霍志良 committed
134
    private CardView publish_trend_ad_relcardView;
konghaorui committed
135 136 137 138
    private ImageView publish_trend_ad_iv;
    private ImageView iv_fm;
    private ImageView iv_to_expert;
    private TextView publish_trend_ad_tv;
139 140
    private ConstraintLayout trends_publish_question;
    private LinearLayout  ll_bottom_img_emoji;
konghaorui committed
141 142 143 144
    RxFileTool fileUtils;
    private String head_dir;//目录
    private String head_path;//路径
    File head_filec;
145
    private List<ParcelableImage> publishTrendImgs, publishTrendImgBrows;
霍志良 committed
146
    private DynamicPublishImageAdapter adapter;
konghaorui committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

    String topic_id;
    String topic_title;

    String publish_type;

    int is_niming = 1;//是否匿名:1否,2匿名
    /**
     * 仅对专家可见
     * 1仅专家可见 2所有可见
     * 默认所有可见
     */
    private int isDoctorShow = 2;

    String content;//发布动态内容
    boolean is_publis_ing = false;

    List<File> imgFiles;

    ImageView publish_emoji_iv;
    private EmoticonPickerView emoticon_picker_view;
    /**
     * 表情弹窗是否显示
     */
    private boolean emojilayot_ishow;
    boolean writeing_title;

    protected Handler uiHandler;
    private boolean isKeyboardShowed = true; // 是否显示键盘
    //测试结果页(或者fm页面、文章页面)传过来的数据
    private String Test_url;
    private String Test_cover;
    private String Test_title;
    private int isFromFm = 0;
    private RxPermissions rxPermissions;
    private AlertDialog loadingDialog;

    private static final String URL = "Test_url";
    private static final String COVER = "Test_cover";
    private static final String TITLE = "Test_title";
    private View dialogContentView;
    private Activity activity;
189
    private int isCanlayout=0;
190 191 192 193 194
    DynamicDraftBean dynamicTrendsDraftBean;
    DynamicDraftBean dynamicquestionDraftBean;
    String dynamic_publish_trends_save="dynamic_publish_trends_save";
    String dynamic_question_save="dynamic_question_save";

konghaorui committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208
    public static Fragment getInstance(String type) {
        PublishQuestionAndTrendFragment fragment = new PublishQuestionAndTrendFragment();
        fragment.type = type;
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    @Override
    public int layoutResId() {
konghaorui committed
209
        return R.layout.dynamic_fragment_publish_trend;
konghaorui committed
210 211 212 213 214 215 216
    }

    @Override
    public void initDataAndEvent() {

    }

217 218 219 220 221 222 223 224 225 226 227
    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStop() {
        super.onStop();
        isCanlayout=0;
    }

霍志良 committed
228 229 230 231 232 233 234 235 236 237 238 239 240
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

   public void onEventMainThread(DynamicSaveEvent event) {
241
        Boolean isPublishTrends= publish_trend_title_editext.getVisibility() != View.VISIBLE;
霍志良 committed
242 243
        if (event.getSaveOrNot()){
            //保留草稿数据  标题、内容、话题、是否专家可见、是否匿名、图片
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
            if (isPublishTrends){//true是发布动态保存
                MMKV.defaultMMKV().encode(dynamic_publish_trends_save,new DynamicDraftBean(publish_trend_title_editext.getText().toString(),
                        publish_trend_content_editext.getText().toString(),
                        topic_id,
                        topic_title,
                        isDoctorShow,
                        is_niming,
                        publishTrendImgs
                ));
            }else{
                MMKV.defaultMMKV().encode(dynamic_question_save,new DynamicDraftBean(publish_trend_title_editext.getText().toString(),
                        publish_trend_content_editext.getText().toString(),
                        topic_id,
                        topic_title,
                        isDoctorShow,
                        is_niming,
                        publishTrendImgs
                ));
            }
霍志良 committed
263

264 265 266 267 268 269 270 271 272 273
        }else{
            if (isPublishTrends){
                publishTrendImgs.clear();
                MMKV.defaultMMKV().encode(dynamic_publish_trends_save,new DynamicDraftBean(
                        "","","44","今日打卡",2,1,publishTrendImgs));
            }else{
                publishTrendImgs.clear();
                MMKV.defaultMMKV().encode(dynamic_question_save,new DynamicDraftBean(
                        "","","44","今日打卡",2,1,publishTrendImgs));
            }
霍志良 committed
274
        }
霍志良 committed
275
    }
konghaorui committed
276 277 278 279
    @Override
    public void initDataAndEventLazy() {
        rxPermissions = new RxPermissions(getActivity());
        imgFiles = new ArrayList<File>();
霍志良 committed
280
        publishTrendImgs = new ArrayList<>();
281 282 283 284 285
        if (TextUtils.equals(type, TYPE_TREND)){
            dynamicTrendsDraftBean=MMKV.defaultMMKV().decodeParcelable(dynamic_publish_trends_save,DynamicDraftBean.class);
        }else{
            dynamicquestionDraftBean=MMKV.defaultMMKV().decodeParcelable(dynamic_question_save,DynamicDraftBean.class);
        }
konghaorui committed
286
        initView();
霍志良 committed
287 288 289 290 291
        if (TextUtils.equals(type, TYPE_TREND)){
            publish_trend_content_editext.requestFocus();
        }else{
            publish_trend_title_editext.requestFocus();
        }
konghaorui committed
292 293 294 295 296 297 298 299 300 301 302 303 304
        uiHandler = new Handler();
        initEvent();
        Intent intent = activity.getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        publish_type = bundle.getString(DynamicConstants.PUBLISH_TYPE);
        if (TextUtils.equals(type, TYPE_TREND)) {//测试结果默认在动态页面展示
            Test_url = bundle.getString("Test_url");
            Test_cover = bundle.getString("Test_cover");
            Test_title = bundle.getString("Test_title");
            isFromFm = bundle.getInt("isfromfm", 0);
        }
        if ("home_publish".equals(publish_type)) {// 从首页进来的发布
            topic_id = bundle.getString("topic_ic");
霍志良 committed
305 306 307
            if (TextUtils.isEmpty(topic_title)){
                topic_title = bundle.getString("topic_title");
            }
konghaorui committed
308 309
            publish_trend_bottom_topic_tv.setVisibility(View.VISIBLE);
            publish_trend_bottom_topic_tv.setText("# " + topic_title);
310
            publish_trend_bottom_topic_tv.setTextColor(ContextCompat.getColor(activity, R.color.platform_main_theme));
konghaorui committed
311 312
        } else if ("topic_publish".equals(publish_type)) {// 从话题详情进来的发布
            topic_id = bundle.getString("topic_id");
霍志良 committed
313 314 315
            if (TextUtils.isEmpty(topic_title)){
                topic_title = bundle.getString("topic_title");
            }
konghaorui committed
316 317
            publish_trend_bottom_topic_tv.setVisibility(View.VISIBLE);
            publish_trend_bottom_topic_tv.setText("# " + topic_title);
318
            publish_trend_bottom_topic_tv.setTextColor(ContextCompat.getColor(activity, R.color.platform_main_theme));
konghaorui committed
319 320 321 322
        } else if ("default_publish".equals(publish_type)) {// 默认发布类型
            publish_trend_bottom_topic_tv.setVisibility(View.VISIBLE);
        }

霍志良 committed
323

konghaorui committed
324 325 326
        if (!"".equals(Test_url) && Test_url != null && !"".equals(Test_cover) && Test_cover != null && !"".equals(Test_title) && Test_title != null) {
            publish_trend_img_rcv.setVisibility(View.GONE);
            publish_trend_ad_rel.setVisibility(View.VISIBLE);
霍志良 committed
327
            publish_trend_ad_relcardView.setVisibility(View.VISIBLE);
konghaorui committed
328 329
            GlideApp.with(activity)
                    .load(Test_cover)
konghaorui committed
330 331
                    .placeholder(R.drawable.dynamic_default_img)
                    .error(R.drawable.dynamic_default_img)
konghaorui committed
332 333 334 335 336 337 338 339 340 341
                    .into(publish_trend_ad_iv);
            publish_trend_ad_tv.setText(Test_title);
            if (isFromFm == 1) {
                iv_fm.setVisibility(View.VISIBLE);
            } else {
                iv_fm.setVisibility(View.GONE);
            }
        } else {
            publish_trend_img_rcv.setVisibility(View.VISIBLE);
            publish_trend_ad_rel.setVisibility(View.GONE);
霍志良 committed
342
            publish_trend_ad_relcardView.setVisibility(View.GONE);
konghaorui committed
343
            publish_trend_bottom_img_iv.setImageResource(R.drawable.dynamic_edit_img);
konghaorui committed
344 345 346
            head_dir = RxFileTool.getStorageDirectory(activity) + "/img/publish_img/";

            if (adapter == null) {
霍志良 committed
347
                adapter = new DynamicPublishImageAdapter(publishTrendImgs, activity);
konghaorui committed
348 349 350
            } else {
                adapter.notifyDataSetChanged();
            }
霍志良 committed
351
            publish_trend_img_rcv.setLayoutManager(new GridLayoutManager(activity, 3));
霍志良 committed
352
            publish_trend_img_rcv.setLayoutManager(new GridLayoutManager(activity, 3));
konghaorui committed
353
            publish_trend_img_rcv.setAdapter(adapter);
霍志良 committed
354
            adapter.setOnItemClickLister(new DynamicPublishImageAdapter.OnItemClickLister() {
konghaorui committed
355 356
                @Override
                public void onItemClick(View view, int position) {
357
                    publishTrendImgBrows = new ArrayList<ParcelableImage>();
konghaorui committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
                    publishTrendImgBrows.clear();
                    publishTrendImgBrows.addAll(publishTrendImgs);
                    if (publishTrendImgBrows.size() < 9) {
                        publishTrendImgBrows.remove(publishTrendImgBrows.size() - 1);
                        if (publishTrendImgBrows.size() > 0) {
                            Intent intent = new Intent();
                            intent.setClass(activity, BrowsePicturesActivity.class);
                            intent.putExtra("browse_type", "preview");
                            intent.putExtra("position", position);
                            Bundle bd = new Bundle();
                            bd.putParcelableArrayList("allTrendImages", (ArrayList<? extends Parcelable>) publishTrendImgBrows);
                            intent.putExtra("allTrendImages_bd", bd);
                            startActivityForResult(intent, DynamicConstants.PUBLISH_PIC_BROW);
                        }
                    } else {
                        if ("default".equals(publishTrendImgBrows.get(8).getImage_url())) {
                            publishTrendImgBrows.remove(8);
                        }
                        Intent intent = new Intent();
                        intent.setClass(activity, BrowsePicturesActivity.class);
                        intent.putExtra("browse_type", "preview");
                        intent.putExtra("position", position);
                        Bundle bd = new Bundle();
                        bd.putParcelableArrayList("allTrendImages", (ArrayList<? extends Parcelable>) publishTrendImgBrows);
                        intent.putExtra("allTrendImages_bd", bd);
                        startActivityForResult(intent, DynamicConstants.PUBLISH_PIC_BROW);
                    }

                }

                @Override
                public void onItemLongClick(View view, int position) {

                }
            });
霍志良 committed
393
            adapter.setOnDeleteClickLister(new DynamicPublishImageAdapter.OnDeleteClickLister() {
konghaorui committed
394 395 396 397 398 399 400 401 402 403
                @Override
                public void onDeleteClick(View view, int position) {
                    DeleteImg(position);
                }

                @Override
                public void onDeleteLongClick(View view, int position) {

                }
            });
霍志良 committed
404
            adapter.setOnAddClickLister(new DynamicPublishImageAdapter.OnAddClickLister() {
konghaorui committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
                @Override
                public void OnAddClick(View view, int position) {
                    if (publishTrendImgs.size() < 9) {
                        showSelector();
                    } else {
                        if ("default".equals(publishTrendImgs.get(8).getImage_url())) {
                            showSelector();
                        } else {
                            YdlCommonOut.Companion.showToast("最多选择9张照片");
                        }
                    }

                }

                @Override
                public void OnAddLongClick(View view, int position) {

                }
            });
        }
    }

    private void initView() {
428 429
        trends_publish_question=getRootView().findViewById(R.id.trends_publish_question);
        ll_bottom_img_emoji=getRootView().findViewById(R.id.ll_bottom_img_emoji);
konghaorui committed
430
        publish_trend_ad_rel = getRootView().findViewById(R.id.publish_trend_ad_rel);
霍志良 committed
431
        publish_trend_ad_relcardView = getRootView().findViewById(R.id.publish_trend_ad_relcardview);
konghaorui committed
432 433 434 435 436 437 438
        iv_fm = getRootView().findViewById(R.id.iv_fm);
        publish_trend_ad_iv = getRootView().findViewById(R.id.publish_trend_ad_iv);
        publish_trend_ad_tv = getRootView().findViewById(R.id.publish_trend_ad_tv);
        publish_anonymity_iv = getRootView().findViewById(R.id.publish_anonymity_iv);
        publish_trend_content_num_tv = getRootView().findViewById(R.id.publish_trend_content_num_tv);
        publish_trend_bottom_topic_tv = getRootView().findViewById(R.id.publish_trend_bottom_topic_tv);
        publish_trend_title_editext = getRootView().findViewById(R.id.publish_trend_title_editext);
霍志良 committed
439
        lineTitle = getRootView().findViewById(R.id.lineTitle);
konghaorui committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453
        publish_trend_content_editext = getRootView().findViewById(R.id.publish_trend_content_editext);
        publish_trend_img_rcv = getRootView().findViewById(R.id.publish_trend_img_rcv);
        publish_trend_bottom_img_iv = getRootView().findViewById(R.id.publish_trend_bottom_img_iv);
        publish_anonymity_rel = getRootView().findViewById(R.id.publish_anonymity_rel);
        ll_to_expert = getRootView().findViewById(R.id.ll_to_expert);
        iv_to_expert = getRootView().findViewById(R.id.iv_to_expert);
        publish_emoji_iv = getRootView().findViewById(R.id.publish_emoji_iv);
        publish_trend_title_editext.addTextChangedListener(textWatcher1);
        publish_trend_content_editext.addTextChangedListener(textWatcher2);
        emoticon_picker_view = getRootView().findViewById(R.id.emoticon_picker_view);
        publish_trend_content_editext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        publish_trend_title_editext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
454
                isCanlayout=1;
konghaorui committed
455 456 457 458
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    writeing_title = true;
                    hideEmojiLayout();
konghaorui committed
459
                    publish_emoji_iv.setImageResource(R.drawable.dynamic_edict_ico_emoji_dis);
konghaorui committed
460 461 462 463 464
                } else {
                    // 此处为失去焦点时的处理内容
                    writeing_title = false;
                    if (writeing_title == false && ("".equals(Test_url) || Test_url == null || "".equals(Test_cover) || Test_cover == null ||
                            "".equals(Test_title) || Test_title == null)) {
konghaorui committed
465 466
                        publish_emoji_iv.setImageResource(R.drawable.dynamic_edict_ico_emoji);
                        //                        publish_trend_bottom_img_iv.setImageResource(R.drawable.dynamic_edit_img);
konghaorui committed
467 468 469 470 471
                    }
                }
            }
        });
        publish_trend_content_editext.setOnTouchListener((v, event) -> {
472
            isCanlayout=1;
konghaorui committed
473
            hideEmojiLayout();
konghaorui committed
474
            publish_emoji_iv.setImageResource(R.drawable.dynamic_edict_ico_emoji);
konghaorui committed
475 476 477 478 479 480
            return false;
        });


        if (TextUtils.equals(type, TYPE_TREND)) {//动态
            publish_trend_title_editext.setVisibility(View.GONE);
霍志良 committed
481
            lineTitle.setVisibility(View.GONE);
konghaorui committed
482 483 484
            ll_to_expert.setVisibility(View.GONE);
            publish_trend_content_editext.setFilters(new InputFilter[]{new InputFilter.LengthFilter(500)});
            publish_trend_content_num_tv.setText("0/500");
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
            if (dynamicTrendsDraftBean!=null){
                publish_trend_title_editext.setText(dynamicTrendsDraftBean.getTitle());
                //展示保留的草稿内容: 内容、话题、是否匿名、图片
                topic_title=dynamicTrendsDraftBean.getTopicTitle();
                publish_trend_content_editext.setText(dynamicTrendsDraftBean.getContent());
                publish_trend_bottom_topic_tv.setText(dynamicTrendsDraftBean.getTopicTitle());
                if (dynamicTrendsDraftBean.getPublishTrendImgs()!=null){
                    publishTrendImgs= dynamicTrendsDraftBean.getPublishTrendImgs();
                }
                is_niming=dynamicTrendsDraftBean.isUserAnonymous();
                isDoctorShow=dynamicTrendsDraftBean.isExpertSeen();
                if (is_niming==1){
                    publish_anonymity_iv.setImageResource(R.drawable.dynamic_checkbox);
                }else{
                    publish_anonymity_iv.setImageResource(R.drawable.dynamic_checkbox_sel);
                }
                if (isDoctorShow==1){
霍志良 committed
502 503 504
                    iv_to_expert.setImageResource(R.drawable.dynamic_checkbox);
                } else{
                    iv_to_expert.setImageResource(R.drawable.dynamic_checkbox_sel);
505 506
                }
            }
konghaorui committed
507 508
        } else {//提问
            publish_trend_title_editext.setVisibility(View.VISIBLE);
霍志良 committed
509
            lineTitle.setVisibility(View.VISIBLE);
konghaorui committed
510 511
            ll_to_expert.setVisibility(View.VISIBLE);
            publish_trend_content_editext.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1000)});
霍志良 committed
512
            publish_trend_content_editext.setHint("添加问题描述");
konghaorui committed
513
            publish_trend_content_num_tv.setText("0/1000");
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
            if (dynamicquestionDraftBean!=null){
                publish_trend_title_editext.setText(dynamicquestionDraftBean.getTitle());
                //展示保留的草稿内容: 内容、话题、是否匿名、图片
                topic_title=dynamicquestionDraftBean.getTopicTitle();
                publish_trend_content_editext.setText(dynamicquestionDraftBean.getContent());
                publish_trend_bottom_topic_tv.setText(dynamicquestionDraftBean.getTopicTitle());
                if (dynamicquestionDraftBean.getPublishTrendImgs()!=null){
                    publishTrendImgs= dynamicquestionDraftBean.getPublishTrendImgs();
                }
                is_niming=dynamicquestionDraftBean.isUserAnonymous();
                isDoctorShow=dynamicquestionDraftBean.isExpertSeen();
                if (is_niming==1){
                    publish_anonymity_iv.setImageResource(R.drawable.dynamic_checkbox);
                }else{
                    publish_anonymity_iv.setImageResource(R.drawable.dynamic_checkbox_sel);
                }
                if (isDoctorShow==1){
霍志良 committed
531
                    iv_to_expert.setImageResource(R.drawable.dynamic_checkbox_sel);
532
                }else{
霍志良 committed
533
                    iv_to_expert.setImageResource(R.drawable.dynamic_checkbox);
534
                }
霍志良 committed
535
            }
konghaorui committed
536
        }
537

538 539 540 541 542 543 544
        trends_publish_question.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (isCanlayout!=0){
                    Rect rect=new Rect();
                    trends_publish_question.getWindowVisibleDisplayFrame(rect);

霍志良 committed
545 546 547 548 549 550 551 552 553
//                    if (isMiuiFullScreen()){
//                        ll_bottom_img_emoji.layout(0,
//                                rect.height()-ll_bottom_img_emoji.getHeight()-160,
//                                ll_bottom_img_emoji.getWidth(),rect.height());
//                    }else{
//                        ll_bottom_img_emoji.layout(0,
//                                rect.height()-ll_bottom_img_emoji.getHeight()- getNavigationBarHeight(getContext()),
//                                ll_bottom_img_emoji.getWidth(),rect.height());
//                    }
554 555 556
                }
            }
        });
konghaorui committed
557
    }
霍志良 committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571
    private Integer getNavigationBarHeight( Context context) {
        Integer result = 0;
        Resources resources = context.getResources();
        Integer resourceId =
                resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) result = resources.getDimensionPixelSize(resourceId);
        return result;
    }
    /*
    * 判断小米手机是否开启了全屏手势
    * */
    private Boolean isMiuiFullScreen(){
       return  Settings.Global.getInt(getContext().getContentResolver(), "force_fsg_nav_bar", 0) != 0;
    }
konghaorui committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    private void initEvent() {
        publish_trend_bottom_img_iv.setOnClickListener(this);
        publish_trend_bottom_topic_tv.setOnClickListener(this);
        publish_anonymity_rel.setOnClickListener(this);
        publish_emoji_iv.setOnClickListener(this);
        ll_to_expert.setOnClickListener(this);
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.publish_anonymity_rel) {
            if (is_niming == 1) {//是否匿名:1否,2匿名
                NormalDialog.Builder builder = new NormalDialog.Builder(getActivity());
                builder.setTitle("");
                builder.setMessage("设置匿名后,将会隐藏你的个人头像、昵称");
                builder.setPositiveButton("确定匿名",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
konghaorui committed
592
                                publish_anonymity_iv.setImageResource(R.drawable.dynamic_checkbox_sel);
konghaorui committed
593 594 595 596 597 598 599 600 601 602 603 604 605
                                is_niming = 2;
                                dialog.dismiss();
                            }
                        });
                builder.setNegativeButton("公开",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
            } else {
konghaorui committed
606
                publish_anonymity_iv.setImageResource(R.drawable.dynamic_checkbox);
konghaorui committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
                is_niming = 1;
            }
        } else if (v.getId() == R.id.publish_trend_bottom_img_iv) {
            if (("".equals(Test_url) || Test_url == null || "".equals(Test_cover) || Test_cover == null ||
                    "".equals(Test_title) || Test_title == null)) {
                if (publishTrendImgs.size() < 9) {
                    showSelector();
                } else {
                    if ("default".equals(publishTrendImgs.get(8).getImage_url())) {
                        showSelector();
                    } else {
                        YdlCommonOut.Companion.showToast("最多选择9张照片");
                    }
                }
            }
        } else if (v.getId() == R.id.publish_trend_bottom_topic_tv) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), SelecTopicActivity.class);
            startActivityForResult(intent, DynamicConstants.REQUEST_CODE_SELECT_TOPIC);
        } else if (v.getId() == R.id.publish_emoji_iv) {
            if (writeing_title == false) {
                toggleEmojiLayout();
            }
        } else if (v.getId() == R.id.ll_to_expert) {//仅对专家可见
            if (isDoctorShow == 1) {
                isDoctorShow = 2;
konghaorui committed
633
                iv_to_expert.setImageResource(R.drawable.dynamic_checkbox);
konghaorui committed
634 635
            } else {
                isDoctorShow = 1;
konghaorui committed
636
                iv_to_expert.setImageResource(R.drawable.dynamic_checkbox_sel);
konghaorui committed
637 638 639 640 641 642 643 644
            }
        }
    }

    /**
     * 发布
     */
    public void publish() {
645

konghaorui committed
646 647 648 649 650 651
        String trend_title = publish_trend_title_editext.getText().toString();
        content = publish_trend_content_editext.getText().toString().trim();
        String phone = DynamicIn.INSTANCE.getUserInfo().getPhone();
        if ("".equals(phone) || phone == null) {
            NormalDialog.Builder builder = new NormalDialog.Builder(getActivity(), "left", true);
            builder.setTitle("");
konghaorui committed
652
            builder.setMessage(BaseApp.instance.getResources().getString(R.string.dynamic_bindphone));
konghaorui committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
            builder.setPositiveButton("果断绑定", (dialog, which) -> {
                startActivity(DynamicIn.INSTANCE.inputPhoneIntent(getActivity(), DynamicConstants.BIND_PHONE_ACTION));
                dialog.dismiss();
            });
            builder.setNegativeButton("忍痛放弃", (dialog, which) -> dialog.dismiss());
            builder.create().show();

        } else {
            if (is_publis_ing == false) { //不是在发布中

                //提问时标题字数不能少于5
                if (TextUtils.equals(type, TYPE_QUESTION)) {
                    if (TextUtils.isEmpty(trend_title) || trend_title.length() < 5) {
                        ((PublishTrendActivity) activity).showCoustomToast("标题不能少于5个字哦");
                        return;
                    }
                }
                if (content.length() < 5) {
                    ((PublishTrendActivity) activity).showCoustomToast("输入内容不能少于5个字哦");
                    //                    YdlCommonOut.INSTANCE.showToast("输入内容长度不能少于5");
                } else {
                    if ("".equals(topic_title) || topic_title == null) {
                        //                        YdlCommonOut.INSTANCE.showToast("请点击#号选择话题");
                        ((PublishTrendActivity) activity).showCoustomToast("选择合适的话题会获得更多关注哦~");
                        ObjectAnimator animator1 = ObjectAnimator.ofFloat(publish_trend_bottom_topic_tv, "scaleX", 1f, 1.5f, 1f);
                        ObjectAnimator animator2 = ObjectAnimator.ofFloat(publish_trend_bottom_topic_tv, "scaleY", 1f, 1.5f, 1f);
                        AnimatorSet set = new AnimatorSet();
                        set.setDuration(300);
                        set.playTogether(animator1, animator2);
                        set.start();
                    } else {
                        ShowDialog();
                        is_publis_ing = true;
                        if (!"".equals(Test_url) && Test_url != null && !"".equals(Test_cover) && Test_cover != null && !"".equals(Test_title) && Test_title != null) {
                            PublishO(is_niming, topic_id, content, trend_title, null);
                        } else {
689
                            List<ParcelableImage> publishImgs = new ArrayList<ParcelableImage>();
konghaorui committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
                            publishImgs.addAll(publishTrendImgs);
                            if (publishTrendImgs.size() < 9 && publishTrendImgs.size() > 1) {
                                publishImgs.remove(publishImgs.size() - 1);
                            } else if (publishTrendImgs.size() == 9) {
                                if ("default".equals(publishTrendImgs.get(8).getImage_url())) {
                                    publishImgs.remove(8);
                                }
                            }

                            if (publishImgs.size() > 0) {
                                DealFile(is_niming, topic_id, content, trend_title, publishImgs);
                            } else {
                                PublishO(is_niming, topic_id, content, trend_title, imgFiles);
                            }
                        }
                    }
                }

            }
        }

    }

    private TextWatcher textWatcher1 = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
            String editable = publish_trend_title_editext.getText().toString();
            String str = StringUtils.StringFilter(editable.toString());
霍志良 committed
720
            EventBus.getDefault().post(str.length() > 0 ? new EditContentEvent(true) : new EditContentEvent(false));
konghaorui committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
            if (!editable.equals(str)) {
                publish_trend_title_editext.setText(str);
                publish_trend_title_editext.setSelection(str.length()); //光标置后
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {
        }

    };

    private TextWatcher textWatcher2 = new TextWatcher() {
        private int start;
        private int count;

        @Override
        public void onTextChanged(CharSequence arg0, int start, int before, int count) {
            this.start = start;
            this.count = count;

            String editable = publish_trend_content_editext.getText().toString();
            String str = StringUtils.StringFilter(editable.toString());
霍志良 committed
748
            EventBus.getDefault().post(str.length() > 0 ? new EditContentEvent(true) : new EditContentEvent(false));
konghaorui committed
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
            if (!editable.equals(str)) {
                publish_trend_content_editext.setText(str);
                publish_trend_content_editext.setSelection(str.length()); //光标置后
            }

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            publish_trend_content_editext.removeTextChangedListener(this);
            MoonUtil.replaceEmoticons(getActivity(), arg0, start, count);
            int editEnd = publish_trend_content_editext.getSelectionEnd();
            publish_trend_content_editext.removeTextChangedListener(this);
            while (StringUtils.counterChars(arg0.toString()) > 5000 && editEnd > 0) {
                arg0.delete(editEnd - 1, editEnd);
                editEnd--;
            }
            publish_trend_content_editext.setSelection(editEnd);
            publish_trend_content_editext.addTextChangedListener(this);


            if (TextUtils.equals(type, TYPE_TREND)) {//动态
                publish_trend_content_num_tv.setText(String.valueOf((Integer.valueOf(arg0.toString().length())) + "/500"));
            } else {//提问
                publish_trend_content_num_tv.setText(String.valueOf((Integer.valueOf(arg0.toString().length())) + "/1000"));
            }
        }

    };


    private void ShowDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            return;
        }

        if (loadingDialog == null) {
            loadingDialog = new AlertDialog.Builder(getActivity()).create();
        }
        loadingDialog.setOnKeyListener((dialog, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                is_publis_ing = false;
            }
            return false;
        });
        if (dialogContentView == null) {
            dialogContentView = LayoutInflater.from(getActivity()).inflate(R.layout.platform_fragment_loading_dialog, null);
            dialogContentView.findViewById(R.id.tvMsg).setVisibility(View.GONE);
        }

        loadingDialog.show();
        loadingDialog.getWindow().setContentView(dialogContentView);
        WindowManager.LayoutParams p = loadingDialog.getWindow().getAttributes();  //获取对话框当前的参数值
        Point point = new Point();
        getActivity().getWindowManager().getDefaultDisplay().getSize(point);
        p.height = (int) (point.y * 0.2);
        p.width = (int) (point.x * 0.35);
        loadingDialog.getWindow().setAttributes(p);     //设置生效
        loadingDialog.show();
    }


    /**
     * 发布动态
     *
     * @param is_niming        是否匿名
     * @param topic_id         话题id
     * @param content          内容
     * @param trend_title      话题标题
     * @param publishTrendImgs 图片附件
     */
霍志良 committed
824
    @SuppressLint({"WrongConstant", "CheckResult"})
825
    private void DealFile(final int is_niming, final String topic_id, final String content, final String trend_title, final List<ParcelableImage> publishTrendImgs) {
konghaorui committed
826 827 828
        imgFiles = new ArrayList<File>();
        Observable.fromIterable(publishTrendImgs)
                .subscribeOn(Schedulers.io())
829 830 831
                .map(parcelableImage -> {
                    LogUtil.d("url: " + parcelableImage.getImage_url());
                    return DealFile2(parcelableImage.getImage_url(), 1500 / publishTrendImgs.size());
konghaorui committed
832 833 834 835 836 837 838 839 840 841 842 843
                })
                .subscribe(file -> {
                    if (file != null) {
                        LogUtil.d("xyutest", "图片压缩后大小: ==>" + file.length() + " file path: " + file.getAbsolutePath());
                        imgFiles.add(file);
                        if (imgFiles.size() == publishTrendImgs.size()) {
                            PublishO(is_niming, topic_id, content, trend_title, imgFiles);
                        }
                    } else {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
konghaorui committed
844
                                //                                    ToastUtil.toastShort(PublishTrendActivity.this, "有图片已被删除");
konghaorui committed
845 846 847 848 849 850 851 852 853 854 855 856 857
                                YdlCommonOut.Companion.showToast("有图片已被删除");
                                loadingDialog.dismiss();
                                loadingDialog = null;
                            }
                        });
                        is_publis_ing = false;
                    }
                });

    }

    private synchronized File DealFile2(final String path, int size) {
        File deal_file = null;
konghaorui committed
858
        deal_file = ImageCompress.scal(getActivity(),path, size);
konghaorui committed
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return deal_file;
    }

    /**
     * 发布动态(OkHttp)
     *
     * @param is_niming   是否匿名
     * @param topic_id    话题id
     * @param content     内容
     * @param trend_title 话题标题
     * @param imgFiles    图片附件
     */
霍志良 committed
876
    @SuppressLint("CheckResult")
konghaorui committed
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    private void PublishO(final int is_niming, final String topic_id, final String content, final String trend_title, final List<File> imgFiles) {
        BuryPointUtils.getInstance().createMap()
                .put("dynamic_type", topic_title)
                .put("dynamic_name", trend_title)
                .put("anonymity", is_niming);
        Command.PublishTrend cmd;
        if (!"".equals(Test_url) && Test_url != null && !"".equals(Test_cover) && Test_cover != null && !"".equals(Test_title) && Test_title != null) {
            cmd = new Command.PublishTrend(is_niming, topic_id, content, trend_title, Test_title, Test_cover, Test_url, type, isDoctorShow);
        } else {
            cmd = new Command.PublishTrend(is_niming, topic_id, content, trend_title, type, isDoctorShow);
        }
        File[] files = null;
        if (imgFiles != null) {
            files = new File[imgFiles.size()];
            for (int i = 0; i < imgFiles.size(); i++) {
                files[i] = imgFiles.get(i);
            }
        }

        DynamicApiUtils.publishTrend(cmd, files)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResponse<PublishTrendResult>>() {
                    @Override
                    public void accept(BaseResponse<PublishTrendResult> resp) {
                        is_publis_ing = false;
                        loadingDialog.dismiss();
                        if (resp.code == 0) {
                            BuryPointUtils.getInstance().put("publish_succeed", true).burryPoint("publishButtonClick");
                            YdlCommonOut.Companion.showToast("发布成功");
                            SharedPreferencesEditor.putString("trend_state", "success");
                            getActivity().finish();
                            publish_trend_content_editext.setText(null);
                            publish_trend_title_editext.setText(null);
                        } else if (resp.code == DynamicConstants.SILENCED_CODE) {  //被禁言
                            showUserSliencedDialog(resp.data);
                        } else {
                            BuryPointUtils.getInstance().put("publish_succeed", false).burryPoint("publishButtonClick");
                            YdlCommonOut.Companion.showToast(resp.msg);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        BuryPointUtils.getInstance().put("publish_succeed", false).burryPoint("publishButtonClick");
                        loadingDialog.dismiss();
                        is_publis_ing = false;
                        DynamicApiUtils.handleError(getActivity(), throwable);
                    }
                });
927 928 929 930 931 932 933 934 935 936
        //发布完,清空保存的草稿
        if (TextUtils.equals(type, TYPE_TREND)){
            publishTrendImgs.clear();
            MMKV.defaultMMKV().encode(dynamic_publish_trends_save,new DynamicDraftBean(
                    "","","44","今日打卡",2,1,publishTrendImgs));
        }else{
            publishTrendImgs.clear();
            MMKV.defaultMMKV().encode(dynamic_question_save,new DynamicDraftBean(
                    "","","44","今日打卡",2,1,publishTrendImgs));
        }
konghaorui committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
    }


    @Override
    public void onEmojiSelected(String key) {
        Editable mEditable = publish_trend_content_editext.getText();
        if (key.equals("/DEL")) {
            publish_trend_content_editext.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
        } else {
            int start = publish_trend_content_editext.getSelectionStart();
            int end = publish_trend_content_editext.getSelectionEnd();
            start = (start < 0 ? 0 : start);
            end = (start < 0 ? 0 : end);
            mEditable.replace(start, end, key);
        }
    }


    private void showSelector() {
konghaorui committed
956 957 958
        int selectedSize = publishTrendImgs.size()!=0?publishTrendImgs.size()-1:0;

        YDLImagePicker.INSTANCE.startPicker((BaseActivity) activity, 9 - selectedSize,new OnResultCallbackListener() {
konghaorui committed
959
            @Override
konghaorui committed
960 961
            public void onResult(List<LocalMedia> list) {
                if (list.isEmpty()) {
konghaorui committed
962 963
                    return;
                }
霍志良 committed
964 965 966 967 968 969 970 971
                if (Build.VERSION.SDK_INT>Build.VERSION_CODES.P){
                    for (int i = 0; i < list.size(); i++) {
                        AddImag(list.get(i).getAndroidQToPath());
                    }
                }else{
                    for (int i = 0; i < list.size(); i++) {
                        AddImag(list.get(i).getPath());
                    }
konghaorui committed
972
                }
霍志良 committed
973

konghaorui committed
974 975 976
            }

            @Override
konghaorui committed
977
            public void onCancel() {
konghaorui committed
978 979 980 981 982 983 984 985

            }
        });
    }

    private void showUserSliencedDialog(PublishTrendResult resp) {
        CommonDialog.create(getActivity())
                .setMessage(resp.tips)
konghaorui committed
986
                .setLeftOnclick(getActivity().getString(R.string.dynamic_details), new View.OnClickListener() {
konghaorui committed
987 988 989 990 991 992 993
                    @Override
                    public void onClick(View v) {
                        String realUrl = TextUtils.isEmpty(resp.url) ? DynamicConstants.HELP_URL : resp.url;
                        H5Params params = new H5Params(realUrl, "");
                        NewH5Activity.start(getActivity(), params);
                    }
                })
konghaorui committed
994
                .setRightClick(getActivity().getString(R.string.dynamic_approval), new View.OnClickListener() {
konghaorui committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
                    @Override
                    public void onClick(View v) {

                    }
                })
                .setCancelAble(true)
                .show();
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            switch (requestCode) {
                case DynamicConstants.PUBLISH_PIC_BROW:
                    if (data == null) {
                        break;
                    }
                    boolean chane_state = data.getBooleanExtra("chane_state", false);
                    if (chane_state) {
                        Bundle bundle = data.getBundleExtra("bundle");
                        publishTrendImgs = bundle.getParcelableArrayList("publish_img");
                        if (publishTrendImgs.size() > 0) {
1019
                            publishTrendImgs.add(new ParcelableImage("default"));
konghaorui committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
                        }
                        adapter.updateDate(publishTrendImgs);
                    }
                    break;
                case DynamicConstants.REQUEST_CODE_SELECT_TOPIC://选择话题
                    String type = data.getStringExtra("type");
                    if ("topic_select".equals(type)) {
                        Bundle bd = data.getBundleExtra("bundle");
                        if (bd != null) {
                            topic_id = bd.getString("topic_id");
                            topic_title = bd.getString("topic_title");
                        }
                        publish_trend_bottom_topic_tv.setVisibility(View.VISIBLE);
                        publish_trend_bottom_topic_tv.setText("# " + topic_title);
1034
                        publish_trend_bottom_topic_tv.setTextColor(ContextCompat.getColor(activity, R.color.platform_main_theme));
konghaorui committed
1035 1036 1037 1038
                        // 获取编辑框焦点
                        publish_trend_content_editext.setFocusable(true);

                        hideEmojiLayout();
1039
                        openSoftKeyBoard();
konghaorui committed
1040 1041 1042 1043 1044 1045
                    }
                    break;
            }
        }
    }

1046 1047 1048 1049 1050 1051
    private void openSoftKeyBoard() {
        //打开软键盘
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

konghaorui committed
1052 1053 1054 1055 1056 1057
    private static final String MIME_JPEG = "image/jpeg";

    /**
     * 添加照片
     */
    private void AddImag(String pic_path) {
1058
        ParcelableImage trendImage = new ParcelableImage(pic_path);
konghaorui committed
1059 1060 1061
        if (publishTrendImgs.size() < 9 && publishTrendImgs.size() > 0) {
            publishTrendImgs.add(publishTrendImgs.size() - 1, trendImage);
        } else if (publishTrendImgs.size() == 0) {
1062
            publishTrendImgs.add(new ParcelableImage("default"));
konghaorui committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
            publishTrendImgs.add(publishTrendImgs.size() - 1, trendImage);
        } else {
            publishTrendImgs.remove(8);
            publishTrendImgs.add(8, trendImage);
        }
        adapter.updateDate(publishTrendImgs);
    }

    // 点击表情,切换到表情布局
    @SuppressLint("WrongConstant")
    private void toggleEmojiLayout() {
        if (emoticon_picker_view == null || emoticon_picker_view.getVisibility() == View.GONE) {
            showEmojiLayout();
konghaorui committed
1076
            //            publish_emoji_iv.setImageResource(R.drawable.dynamic_edict_ico_key);
konghaorui committed
1077 1078 1079
        } else {
            hideEmojiLayout();
            showInputMethod(publish_trend_content_editext);
konghaorui committed
1080
            publish_emoji_iv.setImageResource(R.drawable.dynamic_edict_ico_emoji);
konghaorui committed
1081 1082 1083 1084 1085 1086 1087
        }
    }


    // 隐藏表情布局
    @SuppressLint("WrongConstant")
    public void hideEmojiLayout() {
霍志良 committed
1088 1089 1090
        if (uiHandler!=null){
            uiHandler.removeCallbacks(showEmojiRunnable);
        }
konghaorui committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
        if (emoticon_picker_view != null) {
            emoticon_picker_view.setVisibility(View.GONE);
        }
        emojilayot_ishow = false;
    }

    /**
     * 获取当前emoji表情布局显示状态
     *
     * @return
     */
    public boolean getEmojiLayoutIsShow() {
        return emojilayot_ishow;
    }

    private Runnable showEmojiRunnable = new Runnable() {
        @SuppressLint("WrongConstant")
        @Override
        public void run() {
            emoticon_picker_view.setVisibility(View.VISIBLE);
        }
    };
    private Runnable showTextRunnable = new Runnable() {
        @Override
        public void run() {
            showInputMethod(publish_trend_content_editext);
        }
    };

    // 显示表情布局
    @SuppressLint("WrongConstant")
    private void showEmojiLayout() {
        hideInputMethod();
        publish_trend_content_editext.requestFocus();
        emoticon_picker_view.setVisibility(View.VISIBLE);
        emoticon_picker_view.show(this);
        emojilayot_ishow = true;
    }

    // 隐藏键盘布局
    private void hideInputMethod() {
        isKeyboardShowed = false;
        uiHandler.removeCallbacks(showTextRunnable);
        @SuppressLint("WrongConstant") InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(publish_trend_content_editext.getWindowToken(), 0);
        publish_trend_content_editext.clearFocus();
    }

    // 显示键盘布局
    private void showInputMethod(EditText editTextMessage) {
        editTextMessage.requestFocus();
        //如果已经显示,则继续操作时不需要把光标定位到最后
        if (!isKeyboardShowed) {
            editTextMessage.setSelection(editTextMessage.getText().length());
            isKeyboardShowed = true;
        }

        @SuppressLint("WrongConstant") InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editTextMessage, 0);
    }

    /**
     * 删除照片
     */
    private void DeleteImg(int position) {
        if (publishTrendImgs.size() < 9 && publishTrendImgs.size() > 2) {
            publishTrendImgs.remove(position);
        } else if (publishTrendImgs.size() == 2) {
            publishTrendImgs.clear();
        } else {
            if ("default".equals(publishTrendImgs.get(8).getImage_url())) {
                publishTrendImgs.remove(position);
            } else {
                publishTrendImgs.remove(position);
1165
                publishTrendImgs.add(8, new ParcelableImage("default"));
konghaorui committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
            }

        }
        adapter.updateDate(publishTrendImgs);
    }

    /**
     * 退出发布
     */
    private void ExitPublish() {
        activity.finish();
    }

    @Override
    public void onStickerSelected(String categoryName, String stickerName) {

    }
}