package com.electric.chargingpile.activity; import android.Manifest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.electric.chargingpile.R; import com.electric.chargingpile.adapter.TopicAdapter; import com.electric.chargingpile.application.MainApplication; import com.electric.chargingpile.data.ChatRecommendBean; import com.electric.chargingpile.iview.RecyclerItemTypeClickListener; import com.electric.chargingpile.util.BarColorUtil; import com.electric.chargingpile.util.CommonParams; import com.electric.chargingpile.util.JsonUtils; import com.electric.chargingpile.util.ToastUtil; import com.umeng.analytics.MobclickAgent; import com.zhy.http.okhttp.OkHttpUtils; import com.zhy.http.okhttp.callback.StringCallback; import java.util.HashMap; import java.util.List; import java.util.Map; import okhttp3.Call; import pub.devrel.easypermissions.AfterPermissionGranted; import pub.devrel.easypermissions.AppSettingsDialog; import pub.devrel.easypermissions.EasyPermissions; /** * @author lenovo */ public class TopicActivity extends Activity implements OnClickListener, EasyPermissions.PermissionCallbacks { private static final String TAG = "TopicActivity"; private ImageView mTopicBack; private RelativeLayout mTopicInputLl; private View mTopicInputStart; private View mTopicInputEnd; private EditText mTopicCon; private View mTopicInputBelowLine; private RecyclerView mTopicRv; private TextView mTopicNotCon; private LinearLayout mTopicRetryLl; private TextView mTopicRetry; private TopicAdapter topicAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_topic); BarColorUtil.initStatusBarColor(TopicActivity.this); initView(); recommendTopic(); } private void initView() { mTopicBack = (ImageView) findViewById(R.id.topic_back); mTopicInputLl = (RelativeLayout) findViewById(R.id.topic_input_ll); mTopicInputStart = (View) findViewById(R.id.topic_input_start); mTopicInputEnd = (View) findViewById(R.id.topic_input_end); mTopicCon = (EditText) findViewById(R.id.topic_con); mTopicInputBelowLine = (View) findViewById(R.id.topic_input_below_line); mTopicRv = (RecyclerView) findViewById(R.id.topic_rv); mTopicNotCon = (TextView) findViewById(R.id.topic_not_con); mTopicRetryLl = (LinearLayout) findViewById(R.id.topic_retry_ll); mTopicRetry = (TextView) findViewById(R.id.topic_retry); mTopicRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); mTopicInputEnd.setOnClickListener(this); topicAdapter = new TopicAdapter(this); mTopicRv.setAdapter(topicAdapter); topicAdapter.setRecyclerItemTypeClickListener(new RecyclerItemTypeClickListener() { @Override public void onItemClickListener(int position, int index) { MobclickAgent.onEvent(getApplicationContext(), "1033"); ChatRecommendBean item = topicAdapter.getItem(position); Intent intent = new Intent(); intent.putExtra("topic", item); setResult(-100, intent); finish(); } }); mTopicBack.setOnClickListener(this); mTopicCon.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (editable.length() == 0) { recommendTopic(); } else { searchTopic(editable.toString()); MobclickAgent.onEvent(getApplicationContext(), "1032"); } mTopicInputEnd.setVisibility(editable.length() == 0 ? View.GONE : View.VISIBLE); } }); } private void recommendTopic() { String url = MainApplication.urlNew + "/topic/list.do"; Map map = new HashMap<>(); map.put("selected", "1"); CommonParams.addCommonParams(map); OkHttpUtils.get().params(map).url(url).build().connTimeOut(6000).readTimeOut(6000).execute(new StringCallback() { @Override public void onError(Call call, Exception e) { mTopicRetryLl.setVisibility(View.VISIBLE); mTopicNotCon.setVisibility(View.GONE); mTopicRv.setVisibility(View.GONE); ToastUtil.showToast(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT); } @Override public void onResponse(String response) { String code = JsonUtils.getKeyResult(response, "code"); String desc = JsonUtils.getKeyResult(response, "desc"); Log.d(TAG, "onResponse: " + response); if ("1000".equals(code)) { String data = JsonUtils.getKeyResult(response, "data"); List beans = JsonUtils.parseToObjectList(data, ChatRecommendBean.class); mTopicRetryLl.setVisibility(View.GONE); if (beans.size() == 0) { mTopicNotCon.setVisibility(View.VISIBLE); mTopicRv.setVisibility(View.GONE); } else { mTopicNotCon.setVisibility(View.GONE); mTopicRv.setVisibility(View.VISIBLE); topicAdapter.addData(TopicAdapter.RECOMMEND_ITEM, beans); } } else { ToastUtil.showToast(getApplicationContext(), desc, Toast.LENGTH_SHORT); } } }); } private void searchTopic(CharSequence charSequence) { String url = MainApplication.urlNew + "/topic/search.do"; Map map = new HashMap<>(); map.put("key", charSequence.toString()); CommonParams.addCommonParams(map); Log.d(TAG, "searchTopic: " + map); OkHttpUtils.get().params(map).url(url).build().connTimeOut(6000).readTimeOut(6000).execute(new StringCallback() { @Override public void onError(Call call, Exception e) { mTopicRetryLl.setVisibility(View.VISIBLE); mTopicNotCon.setVisibility(View.GONE); mTopicRv.setVisibility(View.GONE); ToastUtil.showToast(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT); } @Override public void onResponse(String response) { Log.d(TAG, "onResponse: " + response); String code = JsonUtils.getKeyResult(response, "code"); String desc = JsonUtils.getKeyResult(response, "desc"); if ("1000".equals(code)) { String data = JsonUtils.getKeyResult(response, "data"); List beans = JsonUtils.parseToObjectList(data, ChatRecommendBean.class); mTopicRetryLl.setVisibility(View.GONE); if (beans.size() == 0) { mTopicNotCon.setVisibility(View.VISIBLE); mTopicRv.setVisibility(View.GONE); } else { mTopicNotCon.setVisibility(View.GONE); mTopicRv.setVisibility(View.VISIBLE); } topicAdapter.addData(TopicAdapter.SEARCH_ITEM, beans); } else { ToastUtil.showToast(getApplicationContext(), desc, Toast.LENGTH_SHORT); } } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.topic_back: finish(); break; case R.id.publishtopic_publish: telTask(); break; case R.id.topic_input_end: mTopicCon.setText(""); break; default: break; } } @Override protected void onResume() { super.onResume(); MobclickAgent.onResume(this); } @Override protected void onPause() { super.onPause(); MobclickAgent.onPause(this); } @Override public void onPermissionsGranted(int requestCode, List perms) { } @Override public void onPermissionsDenied(int requestCode, List perms) { if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { // TODO add by hyg20200525 new AppSettingsDialog.Builder(TopicActivity.this).build().show(); // new AppSettingsDialog.Builder(TopicActivity.this, // "该功能需要开启拨号权限,是否前往开启?").build().show(); } } private boolean hasCallPhonePermission() { return EasyPermissions.hasPermissions(this, Manifest.permission.CALL_PHONE); } final int RC_CALL_PERM = 123; @AfterPermissionGranted(RC_CALL_PERM) public void telTask() { if (hasCallPhonePermission()) { } else { // Ask for one permission EasyPermissions.requestPermissions( this, "该功能需要开启拨号权限,是否前往开启?", RC_CALL_PERM, Manifest.permission.CALL_PHONE); } } }