|
@ -3,6 +3,9 @@ package gr.thmmy.mthmmy.activities.shoutbox; |
|
|
import android.app.Activity; |
|
|
import android.app.Activity; |
|
|
import android.os.Bundle; |
|
|
import android.os.Bundle; |
|
|
import android.view.LayoutInflater; |
|
|
import android.view.LayoutInflater; |
|
|
|
|
|
import android.view.Menu; |
|
|
|
|
|
import android.view.MenuInflater; |
|
|
|
|
|
import android.view.MenuItem; |
|
|
import android.view.View; |
|
|
import android.view.View; |
|
|
import android.view.ViewGroup; |
|
|
import android.view.ViewGroup; |
|
|
import android.view.inputmethod.InputConnection; |
|
|
import android.view.inputmethod.InputConnection; |
|
@ -34,7 +37,7 @@ public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKey |
|
|
private EmojiKeyboard emojiKeyboard; |
|
|
private EmojiKeyboard emojiKeyboard; |
|
|
private EditorView editorView; |
|
|
private EditorView editorView; |
|
|
|
|
|
|
|
|
private ShoutboxViewModel mViewModel; |
|
|
private ShoutboxViewModel shoutboxViewModel; |
|
|
|
|
|
|
|
|
public static ShoutboxFragment newInstance() { |
|
|
public static ShoutboxFragment newInstance() { |
|
|
return new ShoutboxFragment(); |
|
|
return new ShoutboxFragment(); |
|
@ -45,6 +48,7 @@ public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKey |
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, |
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, |
|
|
@Nullable Bundle savedInstanceState) { |
|
|
@Nullable Bundle savedInstanceState) { |
|
|
final View rootView = inflater.inflate(R.layout.fragment_shoutbox, container, false); |
|
|
final View rootView = inflater.inflate(R.layout.fragment_shoutbox, container, false); |
|
|
|
|
|
setHasOptionsMenu(true); |
|
|
|
|
|
|
|
|
progressBar = rootView.findViewById(R.id.progressBar); |
|
|
progressBar = rootView.findViewById(R.id.progressBar); |
|
|
CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview); |
|
|
CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview); |
|
@ -66,12 +70,12 @@ public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKey |
|
|
InputConnection ic = editorView.getInputConnection(); |
|
|
InputConnection ic = editorView.getInputConnection(); |
|
|
setEmojiKeyboardInputConnection(ic); |
|
|
setEmojiKeyboardInputConnection(ic); |
|
|
editorView.setOnSubmitListener(view -> { |
|
|
editorView.setOnSubmitListener(view -> { |
|
|
if (mViewModel.getShoutboxMutableLiveData().getValue() == null) return; |
|
|
if (shoutboxViewModel.getShoutboxMutableLiveData().getValue() == null) return; |
|
|
if (editorView.getText().toString().isEmpty()) { |
|
|
if (editorView.getText().toString().isEmpty()) { |
|
|
editorView.setError("Required"); |
|
|
editorView.setError("Required"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
mViewModel.sendShout(editorView.getText().toString()); |
|
|
shoutboxViewModel.sendShout(editorView.getText().toString()); |
|
|
}); |
|
|
}); |
|
|
editorView.hideMarkdown(); |
|
|
editorView.hideMarkdown(); |
|
|
editorView.setOnTouchListener((view, motionEvent) -> { |
|
|
editorView.setOnTouchListener((view, motionEvent) -> { |
|
@ -84,23 +88,38 @@ public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKey |
|
|
return rootView; |
|
|
return rootView; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
|
|
|
|
|
inflater.inflate(R.menu.shoutbox_menu, menu); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) { |
|
|
|
|
|
if (item.getItemId() == R.id.menu_refresh) { |
|
|
|
|
|
shoutboxViewModel.loadShoutbox(); |
|
|
|
|
|
return true; |
|
|
|
|
|
} else { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
|
|
super.onActivityCreated(savedInstanceState); |
|
|
super.onActivityCreated(savedInstanceState); |
|
|
mViewModel = ViewModelProviders.of(this).get(ShoutboxViewModel.class); |
|
|
shoutboxViewModel = ViewModelProviders.of(this).get(ShoutboxViewModel.class); |
|
|
mViewModel.getShoutboxMutableLiveData().observe(this, shoutbox -> { |
|
|
shoutboxViewModel.getShoutboxMutableLiveData().observe(this, shoutbox -> { |
|
|
if (shoutbox != null) { |
|
|
if (shoutbox != null) { |
|
|
Timber.i("Shoutbox loaded successfully"); |
|
|
Timber.i("Shoutbox loaded successfully"); |
|
|
shoutAdapter.setShouts(shoutbox.getShouts()); |
|
|
shoutAdapter.setShouts(shoutbox.getShouts()); |
|
|
shoutAdapter.notifyDataSetChanged(); |
|
|
shoutAdapter.notifyDataSetChanged(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
mViewModel.setOnShoutboxTaskStarted(this::onShoutboxTaskSarted); |
|
|
shoutboxViewModel.setOnShoutboxTaskStarted(this::onShoutboxTaskSarted); |
|
|
mViewModel.setOnShoutboxTaskFinished(this::onShoutboxTaskFinished); |
|
|
shoutboxViewModel.setOnShoutboxTaskFinished(this::onShoutboxTaskFinished); |
|
|
mViewModel.setOnSendShoutTaskStarted(this::onSendShoutTaskStarted); |
|
|
shoutboxViewModel.setOnSendShoutTaskStarted(this::onSendShoutTaskStarted); |
|
|
mViewModel.setOnSendShoutTaskFinished(this::onSendShoutTaskFinished); |
|
|
shoutboxViewModel.setOnSendShoutTaskFinished(this::onSendShoutTaskFinished); |
|
|
|
|
|
|
|
|
mViewModel.loadShoutbox(); |
|
|
shoutboxViewModel.loadShoutbox(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void onShoutboxTaskSarted() { |
|
|
private void onShoutboxTaskSarted() { |
|
@ -134,7 +153,7 @@ public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKey |
|
|
private void onShoutboxTaskFinished(int resultCode, Shoutbox shoutbox) { |
|
|
private void onShoutboxTaskFinished(int resultCode, Shoutbox shoutbox) { |
|
|
progressBar.setVisibility(View.INVISIBLE); |
|
|
progressBar.setVisibility(View.INVISIBLE); |
|
|
if (resultCode == NetworkResultCodes.SUCCESSFUL) { |
|
|
if (resultCode == NetworkResultCodes.SUCCESSFUL) { |
|
|
mViewModel.setShoutbox(shoutbox); |
|
|
shoutboxViewModel.setShoutbox(shoutbox); |
|
|
} else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { |
|
|
} else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { |
|
|
Timber.w("Failed to retreive shoutbox due to network error"); |
|
|
Timber.w("Failed to retreive shoutbox due to network error"); |
|
|
Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); |
|
|