Browse Source

fixes: selected vote indicator, shoutbox crash on back press with changed

orientation, reloading data when changing orientation in shoutbox
pull/61/merge
oogee 6 years ago
parent
commit
0aae9fca16
  1. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java
  2. 12
      app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java
  3. 13
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  4. 4
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/ShoutboxViewModel.java

2
app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxActivity.java

@ -26,13 +26,11 @@ public class ShoutboxActivity extends BaseActivity {
createDrawer(); createDrawer();
drawer.setSelection(SHOUTBOX_ID); drawer.setSelection(SHOUTBOX_ID);
if (savedInstanceState == null) {
shoutboxFragment = ShoutboxFragment.newInstance(); shoutboxFragment = ShoutboxFragment.newInstance();
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(R.id.container, shoutboxFragment) .replace(R.id.container, shoutboxFragment)
.commitNow(); .commitNow();
} }
}
@Override @Override
protected void onResume() { protected void onResume() {

12
app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java

@ -1,7 +1,6 @@
package gr.thmmy.mthmmy.activities.shoutbox; package gr.thmmy.mthmmy.activities.shoutbox;
import android.app.Activity; import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -22,7 +21,6 @@ import gr.thmmy.mthmmy.editorview.EditorView;
import gr.thmmy.mthmmy.editorview.EmojiKeyboard; import gr.thmmy.mthmmy.editorview.EmojiKeyboard;
import gr.thmmy.mthmmy.model.Shout; import gr.thmmy.mthmmy.model.Shout;
import gr.thmmy.mthmmy.model.Shoutbox; import gr.thmmy.mthmmy.model.Shoutbox;
import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.CustomRecyclerView; import gr.thmmy.mthmmy.utils.CustomRecyclerView;
import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.viewmodel.ShoutboxViewModel; import gr.thmmy.mthmmy.viewmodel.ShoutboxViewModel;
@ -32,7 +30,6 @@ import timber.log.Timber;
public class ShoutboxFragment extends Fragment { public class ShoutboxFragment extends Fragment {
private MaterialProgressBar progressBar; private MaterialProgressBar progressBar;
private ShoutboxTask shoutboxTask;
private ShoutAdapter shoutAdapter; private ShoutAdapter shoutAdapter;
private EmojiKeyboard emojiKeyboard; private EmojiKeyboard emojiKeyboard;
private EditorView editorView; private EditorView editorView;
@ -90,7 +87,7 @@ public class ShoutboxFragment extends Fragment {
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_refresh) { if (item.getItemId() == R.id.menu_refresh) {
shoutboxViewModel.loadShoutbox(); shoutboxViewModel.loadShoutbox(true);
return true; return true;
} else { } else {
return false; return false;
@ -100,7 +97,7 @@ public class ShoutboxFragment extends Fragment {
@Override @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) { public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
shoutboxViewModel = ViewModelProviders.of(this).get(ShoutboxViewModel.class); shoutboxViewModel = ViewModelProviders.of(getActivity()).get(ShoutboxViewModel.class);
shoutboxViewModel.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");
@ -113,7 +110,7 @@ public class ShoutboxFragment extends Fragment {
shoutboxViewModel.setOnSendShoutTaskStarted(this::onSendShoutTaskStarted); shoutboxViewModel.setOnSendShoutTaskStarted(this::onSendShoutTaskStarted);
shoutboxViewModel.setOnSendShoutTaskFinished(this::onSendShoutTaskFinished); shoutboxViewModel.setOnSendShoutTaskFinished(this::onSendShoutTaskFinished);
shoutboxViewModel.loadShoutbox(); shoutboxViewModel.loadShoutbox(false);
} }
private void onShoutboxTaskSarted() { private void onShoutboxTaskSarted() {
@ -137,8 +134,7 @@ public class ShoutboxFragment extends Fragment {
if (resultCode == NetworkResultCodes.SUCCESSFUL) { if (resultCode == NetworkResultCodes.SUCCESSFUL) {
Timber.i("Shout was sent successfully"); Timber.i("Shout was sent successfully");
editorView.getEditText().getText().clear(); editorView.getEditText().getText().clear();
shoutboxTask = new ShoutboxTask(ShoutboxFragment.this::onShoutboxTaskSarted, ShoutboxFragment.this::onShoutboxTaskFinished); shoutboxViewModel.loadShoutbox(true);
shoutboxTask.execute(SessionManager.shoutboxUrl.toString());
} else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) {
Timber.w("Failed to send shout"); Timber.w("Failed to send shout");
Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show();

13
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -275,6 +275,13 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
} else { } else {
// Showing results // Showing results
holder.optionsLayout.setVisibility(View.GONE); 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()); Arrays.sort(entries, (p1, p2) -> p1.getVotes() - p2.getVotes());
List<BarEntry> valuesToCompare = new ArrayList<>(); List<BarEntry> valuesToCompare = new ArrayList<>();
int totalVotes = 0; int totalVotes = 0;
@ -320,12 +327,6 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.voteChart.setMinimumHeight((int) (chartHeightDp * (metrics.densityDpi / 160f))); holder.voteChart.setMinimumHeight((int) (chartHeightDp * (metrics.densityDpi / 160f)));
holder.voteChart.invalidate(); holder.voteChart.invalidate();
holder.voteChart.setVisibility(View.VISIBLE); 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) { if (poll.getRemoveVoteUrl() != null) {
holder.removeVotesButton.setOnClickListener(v -> viewModel.removeVote()); holder.removeVotesButton.setOnClickListener(v -> viewModel.removeVote());

4
app/src/main/java/gr/thmmy/mthmmy/viewmodel/ShoutboxViewModel.java

@ -17,12 +17,14 @@ public class ShoutboxViewModel extends ViewModel {
private SendShoutTask.OnTaskStartedListener onSendShoutTaskStarted; private SendShoutTask.OnTaskStartedListener onSendShoutTaskStarted;
private SendShoutTask.OnNetworkTaskFinishedListener<Void> onSendShoutTaskFinished; private SendShoutTask.OnNetworkTaskFinishedListener<Void> onSendShoutTaskFinished;
public void loadShoutbox() { public void loadShoutbox(boolean force) {
if (shoutboxMutableLiveData.getValue() == null || force) {
if (shoutboxTask != null && shoutboxTask.getStatus() == AsyncTask.Status.RUNNING) if (shoutboxTask != null && shoutboxTask.getStatus() == AsyncTask.Status.RUNNING)
shoutboxTask.cancel(true); shoutboxTask.cancel(true);
shoutboxTask = new ShoutboxTask(onShoutboxTaskStarted, onShoutboxTaskFinished); shoutboxTask = new ShoutboxTask(onShoutboxTaskStarted, onShoutboxTaskFinished);
shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); shoutboxTask.execute(SessionManager.shoutboxUrl.toString());
} }
}
public void sendShout(String shout) { public void sendShout(String shout) {
if (shoutboxMutableLiveData.getValue() == null) throw new IllegalStateException("Shoutbox task has not finished yet!"); if (shoutboxMutableLiveData.getValue() == null) throw new IllegalStateException("Shoutbox task has not finished yet!");

Loading…
Cancel
Save