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();
drawer.setSelection(SHOUTBOX_ID);
if (savedInstanceState == null) {
shoutboxFragment = ShoutboxFragment.newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, shoutboxFragment)
.commitNow();
}
}
@Override
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;
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();

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

@ -275,6 +275,13 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
} 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<BarEntry> valuesToCompare = new ArrayList<>();
int totalVotes = 0;
@ -320,12 +327,6 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
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());

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.OnNetworkTaskFinishedListener<Void> onSendShoutTaskFinished;
public void loadShoutbox() {
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) {
if (shoutboxMutableLiveData.getValue() == null) throw new IllegalStateException("Shoutbox task has not finished yet!");

Loading…
Cancel
Save