diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java index bdd026ad..9c78198c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java @@ -26,12 +26,10 @@ public class ShoutboxActivity extends BaseActivity { createDrawer(); drawer.setSelection(SHOUTBOX_ID); - if (savedInstanceState == null) { - shoutboxFragment = ShoutboxFragment.newInstance(); - getSupportFragmentManager().beginTransaction() - .replace(R.id.container, shoutboxFragment) - .commitNow(); - } + shoutboxFragment = ShoutboxFragment.newInstance(); + getSupportFragmentManager().beginTransaction() + .replace(R.id.container, shoutboxFragment) + .commitNow(); } @Override 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 0c2d8ae5..848baa9d 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 @@ -1,7 +1,6 @@ package gr.thmmy.mthmmy.activities.shoutbox; import android.app.Activity; -import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; @@ -22,7 +21,6 @@ import gr.thmmy.mthmmy.editorview.EditorView; import gr.thmmy.mthmmy.editorview.EmojiKeyboard; import gr.thmmy.mthmmy.model.Shout; import gr.thmmy.mthmmy.model.Shoutbox; -import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.viewmodel.ShoutboxViewModel; @@ -32,7 +30,6 @@ import timber.log.Timber; public class ShoutboxFragment extends Fragment { private MaterialProgressBar progressBar; - private ShoutboxTask shoutboxTask; private ShoutAdapter shoutAdapter; private EmojiKeyboard emojiKeyboard; private EditorView editorView; @@ -90,7 +87,7 @@ public class ShoutboxFragment extends Fragment { @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_refresh) { - shoutboxViewModel.loadShoutbox(); + shoutboxViewModel.loadShoutbox(true); return true; } else { return false; @@ -100,7 +97,7 @@ public class ShoutboxFragment extends Fragment { @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - shoutboxViewModel = ViewModelProviders.of(this).get(ShoutboxViewModel.class); + shoutboxViewModel = ViewModelProviders.of(getActivity()).get(ShoutboxViewModel.class); shoutboxViewModel.getShoutboxMutableLiveData().observe(this, shoutbox -> { if (shoutbox != null) { Timber.i("Shoutbox loaded successfully"); @@ -113,7 +110,7 @@ public class ShoutboxFragment extends Fragment { shoutboxViewModel.setOnSendShoutTaskStarted(this::onSendShoutTaskStarted); shoutboxViewModel.setOnSendShoutTaskFinished(this::onSendShoutTaskFinished); - shoutboxViewModel.loadShoutbox(); + shoutboxViewModel.loadShoutbox(false); } private void onShoutboxTaskSarted() { @@ -137,8 +134,7 @@ public class ShoutboxFragment extends Fragment { if (resultCode == NetworkResultCodes.SUCCESSFUL) { Timber.i("Shout was sent successfully"); editorView.getEditText().getText().clear(); - shoutboxTask = new ShoutboxTask(ShoutboxFragment.this::onShoutboxTaskSarted, ShoutboxFragment.this::onShoutboxTaskFinished); - shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); + shoutboxViewModel.loadShoutbox(true); } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { Timber.w("Failed to send shout"); Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 345fa9e2..dba15a45 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -275,6 +275,13 @@ class TopicAdapter extends RecyclerView.Adapter { } else { // Showing results holder.optionsLayout.setVisibility(View.GONE); + + if (poll.getSelectedEntryIndex() != -1) { + holder.selectedEntry.setText("You voted \"" + + poll.getEntries()[poll.getSelectedEntryIndex()].getEntryName() + "\""); + holder.selectedEntry.setVisibility(View.VISIBLE); + } + Arrays.sort(entries, (p1, p2) -> p1.getVotes() - p2.getVotes()); List valuesToCompare = new ArrayList<>(); int totalVotes = 0; @@ -320,12 +327,6 @@ class TopicAdapter extends RecyclerView.Adapter { holder.voteChart.setMinimumHeight((int) (chartHeightDp * (metrics.densityDpi / 160f))); holder.voteChart.invalidate(); holder.voteChart.setVisibility(View.VISIBLE); - - if (poll.getSelectedEntryIndex() != -1) { - holder.selectedEntry.setText("You voted \"" + - poll.getEntries()[poll.getSelectedEntryIndex()].getEntryName() + "\""); - holder.selectedEntry.setVisibility(View.VISIBLE); - } } if (poll.getRemoveVoteUrl() != null) { holder.removeVotesButton.setOnClickListener(v -> viewModel.removeVote()); diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/ShoutboxViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/ShoutboxViewModel.java index 5aa92f65..25092578 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/ShoutboxViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/ShoutboxViewModel.java @@ -17,11 +17,13 @@ public class ShoutboxViewModel extends ViewModel { private SendShoutTask.OnTaskStartedListener onSendShoutTaskStarted; private SendShoutTask.OnNetworkTaskFinishedListener onSendShoutTaskFinished; - public void loadShoutbox() { - if (shoutboxTask != null && shoutboxTask.getStatus() == AsyncTask.Status.RUNNING) - shoutboxTask.cancel(true); - shoutboxTask = new ShoutboxTask(onShoutboxTaskStarted, onShoutboxTaskFinished); - shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); + public void loadShoutbox(boolean force) { + if (shoutboxMutableLiveData.getValue() == null || force) { + if (shoutboxTask != null && shoutboxTask.getStatus() == AsyncTask.Status.RUNNING) + shoutboxTask.cancel(true); + shoutboxTask = new ShoutboxTask(onShoutboxTaskStarted, onShoutboxTaskFinished); + shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); + } } public void sendShout(String shout) {