From e47b0457542ecca724d095bc594300894483b0ff Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Fri, 12 Oct 2018 14:39:31 +0300 Subject: [PATCH] add refresh shoutbox button --- .../activities/shoutbox/ShoutboxFragment.java | 41 ++++++++++++++----- .../activities/shoutbox/ShoutboxTask.java | 1 + .../res/drawable/ic_refresh_white_24dp.xml | 5 +++ app/src/main/res/layout/activity_shoutbox.xml | 3 +- app/src/main/res/menu/shoutbox_menu.xml | 10 +++++ app/src/main/res/values/strings.xml | 2 +- 6 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 app/src/main/res/drawable/ic_refresh_white_24dp.xml create mode 100644 app/src/main/res/menu/shoutbox_menu.xml diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java index 85c20615..11e2d2d4 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java @@ -3,6 +3,9 @@ package gr.thmmy.mthmmy.activities.shoutbox; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputConnection; @@ -34,7 +37,7 @@ public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKey private EmojiKeyboard emojiKeyboard; private EditorView editorView; - private ShoutboxViewModel mViewModel; + private ShoutboxViewModel shoutboxViewModel; public static ShoutboxFragment newInstance() { return new ShoutboxFragment(); @@ -45,6 +48,7 @@ public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKey public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.fragment_shoutbox, container, false); + setHasOptionsMenu(true); progressBar = rootView.findViewById(R.id.progressBar); CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview); @@ -66,12 +70,12 @@ public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKey InputConnection ic = editorView.getInputConnection(); setEmojiKeyboardInputConnection(ic); editorView.setOnSubmitListener(view -> { - if (mViewModel.getShoutboxMutableLiveData().getValue() == null) return; + if (shoutboxViewModel.getShoutboxMutableLiveData().getValue() == null) return; if (editorView.getText().toString().isEmpty()) { editorView.setError("Required"); return; } - mViewModel.sendShout(editorView.getText().toString()); + shoutboxViewModel.sendShout(editorView.getText().toString()); }); editorView.hideMarkdown(); editorView.setOnTouchListener((view, motionEvent) -> { @@ -84,23 +88,38 @@ public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKey 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 public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - mViewModel = ViewModelProviders.of(this).get(ShoutboxViewModel.class); - mViewModel.getShoutboxMutableLiveData().observe(this, shoutbox -> { + shoutboxViewModel = ViewModelProviders.of(this).get(ShoutboxViewModel.class); + shoutboxViewModel.getShoutboxMutableLiveData().observe(this, shoutbox -> { if (shoutbox != null) { Timber.i("Shoutbox loaded successfully"); shoutAdapter.setShouts(shoutbox.getShouts()); shoutAdapter.notifyDataSetChanged(); } }); - mViewModel.setOnShoutboxTaskStarted(this::onShoutboxTaskSarted); - mViewModel.setOnShoutboxTaskFinished(this::onShoutboxTaskFinished); - mViewModel.setOnSendShoutTaskStarted(this::onSendShoutTaskStarted); - mViewModel.setOnSendShoutTaskFinished(this::onSendShoutTaskFinished); + shoutboxViewModel.setOnShoutboxTaskStarted(this::onShoutboxTaskSarted); + shoutboxViewModel.setOnShoutboxTaskFinished(this::onShoutboxTaskFinished); + shoutboxViewModel.setOnSendShoutTaskStarted(this::onSendShoutTaskStarted); + shoutboxViewModel.setOnSendShoutTaskFinished(this::onSendShoutTaskFinished); - mViewModel.loadShoutbox(); + shoutboxViewModel.loadShoutbox(); } private void onShoutboxTaskSarted() { @@ -134,7 +153,7 @@ public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKey private void onShoutboxTaskFinished(int resultCode, Shoutbox shoutbox) { progressBar.setVisibility(View.INVISIBLE); if (resultCode == NetworkResultCodes.SUCCESSFUL) { - mViewModel.setShoutbox(shoutbox); + shoutboxViewModel.setShoutbox(shoutbox); } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { Timber.w("Failed to retreive shoutbox due to network error"); Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java index 5368248c..23181d35 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java @@ -45,6 +45,7 @@ public class ShoutboxTask extends NewParseTask { String formUrl = shoutboxForm.attr("action"); String sc = shoutboxForm.select("input[name=sc]").first().attr("value"); String shoutName = shoutboxForm.select("input[name=tp-shout-name]").first().attr("value"); + // TODO: make shout send nullable and disable shouting String shoutSend = shoutboxForm.select("input[name=shout_send]").first().attr("value"); String shoutUrl = shoutboxForm.select("input[name=tp-shout-url]").first().attr("value"); return new Shoutbox(shouts.toArray(new Shout[0]), sc, formUrl, shoutName, shoutSend, shoutUrl); diff --git a/app/src/main/res/drawable/ic_refresh_white_24dp.xml b/app/src/main/res/drawable/ic_refresh_white_24dp.xml new file mode 100644 index 00000000..cc2d1e04 --- /dev/null +++ b/app/src/main/res/drawable/ic_refresh_white_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_shoutbox.xml b/app/src/main/res/layout/activity_shoutbox.xml index e341b84d..e98e8fd6 100644 --- a/app/src/main/res/layout/activity_shoutbox.xml +++ b/app/src/main/res/layout/activity_shoutbox.xml @@ -10,13 +10,12 @@ android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" - android:paddingTop="@dimen/appbar_padding_top" android:theme="@style/ToolbarTheme"> diff --git a/app/src/main/res/menu/shoutbox_menu.xml b/app/src/main/res/menu/shoutbox_menu.xml new file mode 100644 index 00000000..8f3a16ca --- /dev/null +++ b/app/src/main/res/menu/shoutbox_menu.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7f2a5619..bc0a8552 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,7 +20,7 @@ Forum Unread Shoutbox - REFRESH + Refresh thmmy.gr