Java tutorial
/* * Copyright (C) 2016 Claymain Twinkle. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.kesar.lazy.lazychat.presentation.module.chat; import android.content.Context; import android.content.Intent; import android.support.v4.widget.SwipeRefreshLayout; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import org.kesar.lazy.lazychat.R; import org.kesar.lazy.primary.android.activity.MVPActivity; import org.kesar.lazy.primary.utils.KeyBoardUtils; import org.kesar.lazy.primary.utils.ToastUtils; import butterknife.BindView; import butterknife.OnCheckedChanged; import butterknife.OnClick; import butterknife.OnTouch; /** * ?? */ public class ChatActivity extends MVPActivity<ChatPresenter> implements IChatView, VoiceVolumeDialog.VoiceMessageListener { private final static String Extra_Name_ChatUserName = "chat_user"; @BindView(R.id.mSwipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.mListViewContent) ListView mListViewContent; @BindView(R.id.mEtInputText) EditText mEtInputText; @BindView(R.id.mBtnSendChatText) Button mBtnSendChatText; @BindView(R.id.mLLMoreBottom) LinearLayout mLLMoreBottom; @BindView(R.id.mLLBottomExpression) LinearLayout mLLBottomExpression; @BindView(R.id.mCBVoiceAndKeyBoard) CheckBox mCBVoiceAndKeyBoard; @BindView(R.id.mCBExpression) CheckBox mCBExpression; @BindView(R.id.mLlSendAndMore) LinearLayout mLlSendAndMore; @BindView(R.id.mRLInputBottom) RelativeLayout mRLInputBottom; @BindView(R.id.mCBMore) CheckBox mCBMore; @BindView(R.id.mBtnPressToSpeak) Button mBtnPressToSpeak; @BindView(R.id.mLLTextInputAndVoiceBtn) RelativeLayout mLLTextInputAndVoiceBtn; @BindView(R.id.mLLBottomMore) LinearLayout mLLBottomMore; // @BindView(R.id.mViewVoiceVolume) // View mViewVoiceVolume; private MessageAdapter mAdapter; private String toChatUserName; @Override protected void afterBindView() { super.afterBindView(); // init listeners // ? mSwipeRefreshLayout.setOnRefreshListener(() -> getPresenter().doLoadMore()); // ???? mEtInputText.addTextChangedListener(new SendTextButtonIsEnableTextWatcher()); // init data toChatUserName = getIntent().getStringExtra(Extra_Name_ChatUserName); setTitle("" + toChatUserName + "?"); mAdapter = new MessageAdapter(this, toChatUserName); mListViewContent.setAdapter(mAdapter); setListViewToTheBottom(); getPresenter().initData(toChatUserName); } @Override protected int getLayoutId() { return R.layout.activity_chat; } @Override public Context getContext() { return this; } @Override public void showToast(String msg) { ToastUtils.show(getApplicationContext(), msg); } @Override protected ChatPresenter initPresenter() { return new ChatPresenter(this); } @Override public String getSendChatText() { return mEtInputText.getText().toString(); } @Override public void setSendChatText(String text) { mEtInputText.setText(text); } @Override public void loadAllNewDataList() { mAdapter.loadAllNewAndShowData(); } @Override public void loadOldDataList(int count) { mAdapter.loadOldAndShowData(count); } @Override public void setListViewToTheBottom() { mListViewContent.setSelection(mAdapter.getCount() - 1); } @Override public int getListCount() { return mAdapter.getCount(); } @Override public void setListViewSelection(int position) { mListViewContent.setSelection(position); } @Override public void clearMessageListMemoryCache() { mAdapter.clearAllMessagesMemoryCache(); } @Override public void dismissRefreshing() { runOnUiThread(() -> mSwipeRefreshLayout.setRefreshing(false)); } @OnClick(R.id.mBtnSendChatText) public void onClickSendMessage() { getPresenter().doSendChatText(); } /** * ????list view? */ @OnTouch(R.id.mEtInputText) boolean onTouchEditTextInputText() { setListViewToTheBottom(); mCBMore.setChecked(false); mCBExpression.setChecked(false); return false; } /** * list view ???? */ @OnTouch(R.id.mListViewContent) boolean onTouchListView() { hideKeyBoard(); mCBMore.setChecked(false); mCBExpression.setChecked(false); return false; } private VoiceVolumeDialog mVoiceDialog; @OnTouch(R.id.mBtnPressToSpeak) boolean onTouchToSpeak(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: v.setPressed(true); if (mVoiceDialog == null) { mVoiceDialog = VoiceVolumeDialog.newInstance(toChatUserName, this); } mVoiceDialog.show(getSupportFragmentManager()); break; case MotionEvent.ACTION_MOVE: if (mVoiceDialog != null && mVoiceDialog.getState() != VoiceVolumeDialog.STATE_LOADING) { float x = event.getX(); float y = event.getY(); if (x < 0 || y < 0 || y > v.getWidth()) { mVoiceDialog.showCancel(); } else { mVoiceDialog.showVolume(); } } break; default: v.setPressed(false); mVoiceDialog.dismiss(); break; } return true; } /** * ? * * @param isOpen ?? */ @OnCheckedChanged(R.id.mCBVoiceAndKeyBoard) void toggleVoice(boolean isOpen) { if (isOpen) { hideKeyBoard(); mCBMore.setChecked(false); mCBExpression.setChecked(false); } mEtInputText.setVisibility(isOpen ? View.GONE : View.VISIBLE); mBtnPressToSpeak.setVisibility(isOpen ? View.VISIBLE : View.GONE); mCBExpression.setVisibility(isOpen ? View.GONE : View.VISIBLE); } /** * * * @param isOpen ?? */ @OnCheckedChanged(R.id.mCBExpression) void toggleExpression(boolean isOpen) { if (isOpen) { hideKeyBoard(); mCBMore.setChecked(false); mLLBottomExpression.setVisibility(View.VISIBLE); } else { mLLBottomExpression.setVisibility(View.GONE); } } /** * ?? * * @param isOpen ?? */ @OnCheckedChanged(R.id.mCBMore) void toggleMore(boolean isOpen) { if (isOpen) { hideKeyBoard(); mCBVoiceAndKeyBoard.setChecked(false); mCBExpression.setChecked(false); mLLBottomMore.setVisibility(View.VISIBLE); } else { mLLBottomMore.setVisibility(View.GONE); } } /** * */ private void showKeyBoard() { mEtInputText.requestFocus(); KeyBoardUtils.show(getApplicationContext(), mEtInputText); } /** * ?? */ private void hideKeyBoard() { mEtInputText.clearFocus(); KeyBoardUtils.hide(getApplicationContext(), mEtInputText); } @Override public void onRecordSuccess(String voiceFilePath, int seconds) { getPresenter().doSendChatVoice(voiceFilePath, seconds); } /** * ?????? */ private class SendTextButtonIsEnableTextWatcher implements TextWatcher { private int line = 1; @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { boolean isEmpty = TextUtils.isEmpty(s); // ?? mBtnSendChatText.setVisibility(isEmpty ? View.GONE : View.VISIBLE); mCBMore.setVisibility(isEmpty ? View.VISIBLE : View.GONE); // if (line != mEtInputText.getLineCount()) { setListViewToTheBottom(); line = mEtInputText.getLineCount(); } } } public static Intent getCallingIntent(Context context, String chatUserName) { Intent intent = new Intent(context, ChatActivity.class); intent.putExtra(Extra_Name_ChatUserName, chatUserName); return intent; } }