Mercury-IM/app/src/main/java/org/mercury_im/messenger/ui/chat/ChatInputFragment.java

133 lines
3.7 KiB
Java
Raw Normal View History

2019-05-13 03:19:17 +02:00
package org.mercury_im.messenger.ui.chat;
2019-05-04 00:27:02 +02:00
2019-06-03 01:16:49 +02:00
import android.content.Context;
2019-05-04 00:27:02 +02:00
import android.os.Bundle;
2019-06-21 03:45:33 +02:00
import android.util.Log;
2019-05-04 00:27:02 +02:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
2019-12-09 13:50:26 +01:00
import androidx.lifecycle.ViewModelProvider;
2019-05-04 00:27:02 +02:00
2019-06-06 23:32:41 +02:00
import butterknife.BindView;
2019-06-21 03:45:33 +02:00
import butterknife.ButterKnife;
2019-06-06 23:32:41 +02:00
import butterknife.OnClick;
import org.mercury_im.messenger.R;
2019-06-21 03:45:33 +02:00
2019-05-04 00:27:02 +02:00
public class ChatInputFragment extends Fragment implements View.OnClickListener {
2019-06-06 23:32:41 +02:00
@BindView(R.id.text_body)
EditText textInput;
@BindView(R.id.btn_media)
ImageButton addAttachement;
@BindView(R.id.btn_send)
ImageButton buttonSend;
2019-05-04 00:27:02 +02:00
2019-06-23 02:34:14 +02:00
@BindView(R.id.btn_emoji)
ImageButton buttonEmoji;
2019-05-04 00:27:02 +02:00
private ChatInputViewModel mViewModel;
2019-06-03 01:16:49 +02:00
private OnChatInputActionListener actionListener;
2019-05-04 00:27:02 +02:00
public static ChatInputFragment newInstance() {
return new ChatInputFragment();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
2019-06-23 02:34:14 +02:00
View view = inflater.inflate(R.layout.view_compose, container, false);
2019-06-21 03:45:33 +02:00
ButterKnife.bind(this, view);
return view;
2019-05-04 00:27:02 +02:00
}
2019-06-06 13:49:13 +02:00
@Override
2019-05-04 00:27:02 +02:00
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
2019-12-09 13:50:26 +01:00
mViewModel = new ViewModelProvider(this).get(ChatInputViewModel.class);
2019-06-06 13:49:13 +02:00
observeViewModel(mViewModel);
2019-05-04 00:27:02 +02:00
}
2019-06-06 23:32:41 +02:00
/**
* Observe the {@link ChatInputViewModel} for changes and reflect those in the UI.
*
* @param viewModel ViewModel
*/
2019-05-04 00:27:02 +02:00
private void observeViewModel(ChatInputViewModel viewModel) {
// viewModel.getDraft().observe(ChatInputFragment.this, draft -> textInput.setText(draft));
2019-05-04 00:27:02 +02:00
}
2019-06-06 23:32:41 +02:00
/**
* Attach this fragment to the parent {@link android.app.Activity} which MUST implement
* {@link OnChatInputActionListener} and is bound to handle input actions like send button
* clicked etc.
*
* @param context parent activity
*/
2019-06-03 01:16:49 +02:00
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnChatInputActionListener) {
this.actionListener = (OnChatInputActionListener) context;
} else {
throw new RuntimeException(context.toString()
2019-08-20 20:14:57 +02:00
+ " must implement OnAccountListItemClickListener");
2019-06-03 01:16:49 +02:00
}
}
@Override
public void onDetach() {
super.onDetach();
this.actionListener = null;
}
2019-05-04 00:27:02 +02:00
@Override
@OnClick({R.id.btn_send, R.id.btn_media, R.id.btn_emoji})
2019-05-04 00:27:02 +02:00
public void onClick(View view) {
switch (view.getId()) {
// Add media
2019-06-06 23:32:41 +02:00
case R.id.btn_media:
2019-06-03 01:16:49 +02:00
if (actionListener != null) {
2019-06-06 23:32:41 +02:00
actionListener.onButtonMediaClicked();
2019-06-03 01:16:49 +02:00
}
2019-05-04 00:27:02 +02:00
break;
// Send message
2019-06-06 23:32:41 +02:00
case R.id.btn_send:
2019-06-03 01:16:49 +02:00
if (actionListener != null) {
actionListener.onComposingBodySend(textInput.getText().toString());
}
2019-05-04 00:27:02 +02:00
textInput.setText(null);
break;
2019-06-23 02:34:14 +02:00
case R.id.btn_emoji:
if (actionListener != null) {
actionListener.onButtonEmojiClicked();
}
2019-06-23 02:34:14 +02:00
break;
2019-05-04 00:27:02 +02:00
}
}
2019-06-03 01:16:49 +02:00
public interface OnChatInputActionListener {
2019-06-06 23:32:41 +02:00
void onButtonMediaClicked();
2019-06-03 01:16:49 +02:00
void onButtonEmojiClicked();
2019-06-03 01:16:49 +02:00
void onComposingBodyChanged(String body);
void onComposingBodySend(String body);
}
2019-05-04 00:27:02 +02:00
}