Browse Source

add refresh shoutbox button

pull/61/merge
Thodoris1999 6 years ago
parent
commit
e47b045754
  1. 41
      app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java
  2. 1
      app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java
  3. 5
      app/src/main/res/drawable/ic_refresh_white_24dp.xml
  4. 3
      app/src/main/res/layout/activity_shoutbox.xml
  5. 10
      app/src/main/res/menu/shoutbox_menu.xml
  6. 2
      app/src/main/res/values/strings.xml

41
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();

1
app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java

@ -45,6 +45,7 @@ public class ShoutboxTask extends NewParseTask<Shoutbox> {
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);

5
app/src/main/res/drawable/ic_refresh_white_24dp.xml

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
</vector>

3
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">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:gravity="center"
app:popupTheme="@style/ToolbarTheme" />

10
app/src/main/res/menu/shoutbox_menu.xml

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_refresh"
android:icon="@drawable/ic_refresh_white_24dp"
app:showAsAction="ifRoom"
android:title="@string/refresh">
</item>
</menu>

2
app/src/main/res/values/strings.xml

@ -20,7 +20,7 @@
<string name="forum">Forum</string>
<string name="unread">Unread</string>
<string name="shoutbox">Shoutbox</string>
<string name="refresh_button">REFRESH</string>
<string name="refresh">Refresh</string>
<!--Login Activity-->
<string name="thmmy_img_description">thmmy.gr</string>

Loading…
Cancel
Save