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

112 lines
3.5 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-05-18 10:06:16 +02:00
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
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-05-18 10:06:16 +02:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.fragment.app.Fragment;
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 android.widget.Toast;
2019-05-13 03:19:17 +02:00
import org.mercury_im.messenger.R;
2019-05-04 00:27:02 +02:00
public class ChatInputFragment extends Fragment implements View.OnClickListener {
private EditText textInput;
2019-05-27 21:34:11 +02:00
private ImageButton addAttachement;
2019-05-04 00:27:02 +02:00
private ImageButton buttonSend;
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) {
View view = inflater.inflate(R.layout.fragment_chat, container, false);
textInput = view.findViewById(R.id.chat_field__text_input);
2019-05-27 21:34:11 +02:00
addAttachement = view.findViewById(R.id.chat_field__button_attachment);
2019-05-04 00:27:02 +02:00
buttonSend = view.findViewById(R.id.chat_field__button_send);
addAttachement.setOnClickListener(this);
buttonSend.setOnClickListener(this);
return view;
}
2019-06-03 01:16:49 +02:00
@Override2
2019-05-04 00:27:02 +02:00
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(ChatInputViewModel.class);
// observeViewModel(mViewModel);
}
private void observeViewModel(ChatInputViewModel viewModel) {
viewModel.getDraft().observe(ChatInputFragment.this, new Observer<String>() {
@Override
public void onChanged(@Nullable String draft) {
textInput.setText(draft);
}
});
}
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()
+ " must implement OnListFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
this.actionListener = null;
}
2019-05-04 00:27:02 +02:00
@Override
public void onClick(View view) {
switch (view.getId()) {
// Add media
2019-05-27 21:34:11 +02:00
case R.id.chat_field__button_attachment:
2019-06-03 01:16:49 +02:00
if (actionListener != null) {
actionListener.onButtonAddAttachementClicked();
}
2019-05-04 00:27:02 +02:00
Toast.makeText(getContext(), R.string.not_yet_implemented, Toast.LENGTH_SHORT).show();
break;
// Send message
case R.id.chat_field__button_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-03 01:16:49 +02:00
public interface OnChatInputActionListener {
void onButtonAddAttachementClicked();
void onComposingBodyChanged(String body);
void onComposingBodySend(String body);
}
2019-05-04 00:27:02 +02:00
}