PublishTrendActivity.java 12 KB
Newer Older
konghaorui committed
1 2 3
package com.yidianling.dynamic.publishTrend;

import android.app.Activity;
霍志良 committed
4
import android.content.Context;
konghaorui committed
5 6 7 8
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
YKai committed
9 10 11
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
konghaorui committed
12 13 14
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
15
import android.view.WindowManager;
konghaorui committed
16 17 18 19
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
konghaorui committed
20
import android.widget.LinearLayout;
konghaorui committed
21 22 23 24 25 26 27 28 29
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.ydl.ydlcommon.base.BaseActivity;
import com.ydl.ydlcommon.base.config.YDLConstants;
霍志良 committed
30
import com.ydl.ydlcommon.bean.StatusBarOptions;
霍志良 committed
31
import com.ydl.ydlcommon.utils.LogUtil;
konghaorui committed
32
import com.ydl.ydlcommon.utils.SharedPreferencesEditor;
konghaorui committed
33
import com.ydl.ydlcommon.utils.StatusBarUtils;
konghaorui committed
34
import com.ydl.ydlcommon.view.NoScrollViewPager;
霍志良 committed
35
import com.ydl.ydlcommon.view.dialog.CommonDialog;
36
import com.yidianling.dynamic.bean.DynamicConstants;
konghaorui committed
37 38
import com.yidianling.dynamic.R;
import com.yidianling.dynamic.bean.TrendTabEntity;
39 40 41
import com.yidianling.dynamic.dialog.DynamicSaveDraftDialog;
import com.yidianling.dynamic.dialog.TrendsAndQuestionDialog;
import com.yidianling.dynamic.dialog.listener.DynamicQuestionSaveListener;
霍志良 committed
42
import com.yidianling.dynamic.event.DynamicSaveEvent;
霍志良 committed
43
import com.yidianling.dynamic.event.EditContentEvent;
konghaorui committed
44 45
import com.yidianling.dynamic.publishTrend.fragment.PublishQuestionAndTrendFragment;

霍志良 committed
46
import org.jetbrains.annotations.NotNull;
47 48
import org.jetbrains.annotations.Nullable;

konghaorui committed
49 50 51
import java.util.ArrayList;
import java.util.List;

霍志良 committed
52 53
import de.greenrobot.event.EventBus;

konghaorui committed
54 55 56 57 58 59 60 61 62 63

/**
 * 发布动态
 */
@Route(path = "/trend/publish")
public class PublishTrendActivity extends BaseActivity {
    private static final String URL = "Test_url";
    private static final String COVER = "Test_cover";
    private static final String TITLE = "Test_title";
    private static final String ISFROMFM = "isfromfm";
霍志良 committed
64
    private TextView mBackIv;
konghaorui committed
65 66 67
    private TextView mToastView;
    private NoScrollViewPager mViewPager;
    private CommonTabLayout mTableLayout;
霍志良 committed
68
    private TextView publish_trend_sure_tv;
konghaorui committed
69 70 71 72 73
    //    private RelativeLayout mNoticeLayout;
    private ArrayList<CustomTabEntity> titleList = new ArrayList<>();
    private List<Fragment> fragmentList = new ArrayList<>();
    private PopupWindow noticePopupWindow;
    private Handler handler;
konghaorui committed
74
    private LinearLayout mPublishTrendTitle;
75
    private DynamicSaveDraftDialog dynamicSaveDraftDialog = null;
konghaorui committed
76

77 78 79
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
霍志良 committed
80
        EventBus.getDefault().register(this);
81 82
    }

konghaorui committed
83 84 85 86 87 88 89 90 91 92 93
    public static Intent newIntent(Activity activity, String url, String cover, String title) {
        Intent intent = new Intent(activity, PublishTrendActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(URL, url);
        bundle.putString(COVER, cover);
        bundle.putString(TITLE, title);
        intent.putExtra(YDLConstants.BUNDLE, bundle);
        return intent;
    }


霍志良 committed
94 95 96 97 98 99
    @NotNull
    @Override
    public StatusBarOptions getStatusViewOptions() {
        return new StatusBarOptions(true,  true);
    }

konghaorui committed
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 126 127 128 129
    public static Intent newIntentForArticle(Activity activity, String url, String cover, String title) {
        Intent intent = new Intent(activity, PublishTrendActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(URL, url);
        bundle.putString(COVER, cover);
        bundle.putString(TITLE, title);
        bundle.putString(DynamicConstants.PUBLISH_TYPE, "topic_publish");
        bundle.putString("topic_id", "44");
        bundle.putString("topic_title", "今日打卡");
        intent.putExtra(YDLConstants.BUNDLE, bundle);
        return intent;
    }


    public static Intent newIntentForFm(Activity activity, String url, String cover, String title, int isFromFm) {
        Intent intent = new Intent(activity, PublishTrendActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(URL, url);
        bundle.putString(COVER, cover);
        bundle.putString(TITLE, title);
        bundle.putInt(ISFROMFM, isFromFm);
        bundle.putString(DynamicConstants.PUBLISH_TYPE, "topic_publish");
        bundle.putString("topic_id", "44");
        bundle.putString("topic_title", "今日打卡");
        intent.putExtra(YDLConstants.BUNDLE, bundle);
        return intent;
    }

    @Override
    protected int layoutResId() {
konghaorui committed
130
        return R.layout.dynamic_activity_trend;
konghaorui committed
131 132 133 134 135 136
    }

    @Override
    protected void initDataAndEvent() {
        SharedPreferencesEditor.putString("trend_state", "");
        handler = new Handler();
霍志良 committed
137
        mBackIv = findViewById(R.id.mBackIv);
konghaorui committed
138
        mPublishTrendTitle= findViewById(R.id.publish_trend_title_rel);
konghaorui committed
139
        mBackIv.setOnClickListener(v -> {
霍志良 committed
140
            SaveOrNotDialog();
konghaorui committed
141 142

        });
霍志良 committed
143
        publish_trend_sure_tv=findViewById(R.id.publish_trend_sure_tv);
konghaorui committed
144
        //发布按钮
霍志良 committed
145
        publish_trend_sure_tv.setOnClickListener(v -> {
konghaorui committed
146 147 148 149 150 151 152 153 154 155 156
            if (fragmentList.get(mViewPager.getCurrentItem()) != null) {
                ((PublishQuestionAndTrendFragment) fragmentList.get(mViewPager.getCurrentItem())).publish();
            }
        });
        mViewPager = findViewById(R.id.view_pager);
        mViewPager.setScanScroll(true);
        mViewPager.setOffscreenPageLimit(2);
        mTableLayout = findViewById(R.id.tab_layout);
        mToastView = findViewById(R.id.tv_coustom_toast_view);
        initData();
    }
霍志良 committed
157 158 159
    public void onEventMainThread(EditContentEvent event) {
        if (event.getContaineText()){
            publish_trend_sure_tv.setBackground(getDrawable(R.drawable.dynamic_bg_publish_button));
霍志良 committed
160
            publish_trend_sure_tv.setTextColor(getResources().getColor(R.color.dynamic_white));
霍志良 committed
161 162
        }else{
            publish_trend_sure_tv.setBackground(getDrawable(R.drawable.dynamic_bg_publish_gray_button));
霍志良 committed
163
            publish_trend_sure_tv.setTextColor(getResources().getColor(R.color.dynamic_d0d0d0));
霍志良 committed
164 165
        }
    }
霍志良 committed
166
    private void SaveOrNotDialog() {
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        dynamicSaveDraftDialog=new DynamicSaveDraftDialog(this
                , new DynamicQuestionSaveListener() {
            @Override
            public void notSave() {
                EventBus.getDefault().post(new DynamicSaveEvent(false));
                finish();
            }

            @Override
            public void save() {
            //发送通知给Fragment保留此次编辑
                EventBus.getDefault().post(new DynamicSaveEvent(true));
                finish();
            }
        });
        dynamicSaveDraftDialog.show();
霍志良 committed
183 184
    }

konghaorui committed
185
    private void initData() {
霍志良 committed
186 187 188 189 190 191 192 193 194
        Integer dynamic_question = 1;
        Bundle bundle = getIntent().getBundleExtra("bundle");
        if (null != bundle) {
            dynamic_question= bundle.getInt("dynamic_question", 1);
        }
        if (dynamic_question==1){
            titleList.add(new TrendTabEntity("发动态", 0, 0));
            fragmentList.add(PublishQuestionAndTrendFragment.getInstance(PublishQuestionAndTrendFragment.TYPE_TREND));
        }else{
霍志良 committed
195
        titleList.add(new TrendTabEntity("提问题", 0, 0));
霍志良 committed
196 197
        fragmentList.add(PublishQuestionAndTrendFragment.getInstance(PublishQuestionAndTrendFragment.TYPE_QUESTION));
        }
konghaorui committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        mTableLayout.setTabData(titleList);


        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        mViewPager.setAdapter(adapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (mTableLayout.getCurrentTab() != position) {
                    mTableLayout.setCurrentTab(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mTableLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (mViewPager.getCurrentItem() != position) {
                    mViewPager.setCurrentItem(position);
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

霍志良 committed
244
//        handler.postDelayed(() -> showNoticeView(), 300);
konghaorui committed
245 246 247 248 249 250 251 252 253 254 255
    }

    /**
     * 展示提问页面的提示窗
     */
    private void showNoticeView() {
        if (mViewPager.getCurrentItem() == 1) {
            //当前已经是提问页面了
            return;
        }

霍志良 committed
256
        TextView textView = mTableLayout.getTitleView(0);
konghaorui committed
257 258
        textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int textViewWidth = textView.getMeasuredWidth();
konghaorui committed
259
        View view = LayoutInflater.from(this).inflate(R.layout.dynamic_popup_notice, null);
konghaorui committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int wid = view.getMeasuredWidth();
        noticePopupWindow = new PopupWindow(view);
        noticePopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        noticePopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置点击空白处消失
        noticePopupWindow.setTouchable(true);
        noticePopupWindow.setOutsideTouchable(true);
        noticePopupWindow.setBackgroundDrawable(new BitmapDrawable());
        noticePopupWindow.showAsDropDown(textView, textViewWidth / 2 - wid / 2, -15);
        handler.postDelayed(() -> {
            if (noticePopupWindow != null) {
                noticePopupWindow.dismiss();
            }
        }, 1000);
    }

    /**
     * 显示页面自定的提示
     *
     * @param msg
     */
    public void showCoustomToast(String msg) {
        if (mToastView == null) {
            return;
        }
        mToastView.setText(msg);
        if (mToastView.getVisibility() != View.VISIBLE) {
            TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, -(mToastView.getMeasuredHeight() / 4), 0);
            translateAnimation.setDuration(1000);
            mToastView.setVisibility(View.VISIBLE);
            mToastView.startAnimation(translateAnimation);
            mToastView.postDelayed(() -> {
                if (mToastView == null || mToastView.getVisibility() == View.GONE) {
                    return;
                }
                Animation animation = new AlphaAnimation(1f, 0f);
                animation.setDuration(1000);
                mToastView.setAnimation(animation);
                mToastView.setVisibility(View.GONE);
            }, 1000);
        }
    }

    @Override
    public void onBackPressed() {
        if (fragmentList.get(mViewPager.getCurrentItem()) != null) {
            if (((PublishQuestionAndTrendFragment) fragmentList.get(mViewPager.getCurrentItem())).getEmojiLayoutIsShow()) {
                ((PublishQuestionAndTrendFragment) fragmentList.get(mViewPager.getCurrentItem())).hideEmojiLayout();
            } else {
霍志良 committed
310
                SaveOrNotDialog();
konghaorui committed
311 312
            }
        } else {
霍志良 committed
313
            SaveOrNotDialog();
konghaorui committed
314 315 316 317 318 319
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
霍志良 committed
320
        EventBus.getDefault().unregister(this);
konghaorui committed
321 322 323 324 325 326
        //解除任务
        if (null != handler) {
            handler.removeCallbacksAndMessages(null);
        }
    }
}