|
|
@ -1,6 +1,5 @@ |
|
|
|
package gr.thmmy.mthmmy.activities.main.shoutbox; |
|
|
|
package gr.thmmy.mthmmy.activities.shoutbox; |
|
|
|
|
|
|
|
import android.animation.ValueAnimator; |
|
|
|
import android.app.Activity; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.view.LayoutInflater; |
|
|
@ -8,16 +7,14 @@ import android.view.View; |
|
|
|
import android.view.ViewGroup; |
|
|
|
import android.view.inputmethod.InputConnection; |
|
|
|
import android.view.inputmethod.InputMethodManager; |
|
|
|
import android.widget.TextView; |
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
import androidx.annotation.Nullable; |
|
|
|
import androidx.interpolator.view.animation.FastOutSlowInInterpolator; |
|
|
|
import androidx.fragment.app.Fragment; |
|
|
|
import androidx.lifecycle.ViewModelProviders; |
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager; |
|
|
|
import androidx.recyclerview.widget.RecyclerView; |
|
|
|
import gr.thmmy.mthmmy.R; |
|
|
|
import gr.thmmy.mthmmy.base.BaseFragment; |
|
|
|
import gr.thmmy.mthmmy.editorview.EditorView; |
|
|
|
import gr.thmmy.mthmmy.editorview.EmojiKeyboard; |
|
|
|
import gr.thmmy.mthmmy.model.Shout; |
|
|
@ -25,85 +22,30 @@ 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; |
|
|
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
|
import timber.log.Timber; |
|
|
|
|
|
|
|
public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.EmojiKeyboardOwner { |
|
|
|
|
|
|
|
private static final String TAG = "ShoutboxFragment"; |
|
|
|
public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKeyboardOwner { |
|
|
|
|
|
|
|
private MaterialProgressBar progressBar; |
|
|
|
private TextView refreshLabel; |
|
|
|
private ShoutboxTask shoutboxTask; |
|
|
|
private ShoutAdapter shoutAdapter; |
|
|
|
private EmojiKeyboard emojiKeyboard; |
|
|
|
private EditorView editorView; |
|
|
|
private Shoutbox shoutbox; |
|
|
|
private ValueAnimator animator; |
|
|
|
|
|
|
|
public static ShoutboxFragment newInstance(int sectionNumber) { |
|
|
|
ShoutboxFragment fragment = new ShoutboxFragment(); |
|
|
|
Bundle args = new Bundle(); |
|
|
|
args.putString(ARG_TAG, TAG); |
|
|
|
args.putInt(ARG_SECTION_NUMBER, sectionNumber); |
|
|
|
fragment.setArguments(args); |
|
|
|
return fragment; |
|
|
|
} |
|
|
|
|
|
|
|
private void onShoutboxTaskSarted() { |
|
|
|
Timber.i("Starting shoutbox task..."); |
|
|
|
hideRefreshLabel(); |
|
|
|
progressBar.setVisibility(View.VISIBLE); |
|
|
|
} |
|
|
|
|
|
|
|
private void onSendShoutTaskStarted() { |
|
|
|
Timber.i("Start sending a shout..."); |
|
|
|
progressBar.setVisibility(View.VISIBLE); |
|
|
|
} |
|
|
|
|
|
|
|
private void onSendShoutTaskFinished(int resultCode, Void ignored) { |
|
|
|
editorView.setAlpha(1f); |
|
|
|
editorView.setEnabled(true); |
|
|
|
progressBar.setVisibility(View.INVISIBLE); |
|
|
|
showRefreshLabel(); |
|
|
|
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()); |
|
|
|
} else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { |
|
|
|
Timber.w("Failed to send shout"); |
|
|
|
Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
private ShoutboxViewModel mViewModel; |
|
|
|
|
|
|
|
private void onShoutboxTaskFinished(int resultCode, Shoutbox shoutbox) { |
|
|
|
progressBar.setVisibility(View.INVISIBLE); |
|
|
|
if (resultCode == NetworkResultCodes.SUCCESSFUL) { |
|
|
|
Timber.i("Shoutbox loaded successfully"); |
|
|
|
shoutAdapter.setShouts(shoutbox.getShouts()); |
|
|
|
shoutAdapter.notifyDataSetChanged(); |
|
|
|
this.shoutbox = 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(); |
|
|
|
} else { |
|
|
|
Timber.wtf("Failed to retreive shoutbox due to unknown error"); |
|
|
|
Toast.makeText(getContext(), "Failed to retrieve shoutbox, please contact mthmmy developer team", Toast.LENGTH_LONG).show(); |
|
|
|
} |
|
|
|
public static ShoutboxFragment newInstance() { |
|
|
|
return new ShoutboxFragment(); |
|
|
|
} |
|
|
|
|
|
|
|
@Nullable |
|
|
|
@Override |
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, |
|
|
|
@Nullable Bundle savedInstanceState) { |
|
|
|
final View rootView = inflater.inflate(R.layout.fragment_shoutbox, container, false); |
|
|
|
|
|
|
|
refreshLabel = rootView.findViewById(R.id.refresh_label); |
|
|
|
refreshLabel.setOnClickListener(v -> { |
|
|
|
shoutboxTask = new ShoutboxTask(this::onShoutboxTaskSarted, this::onShoutboxTaskFinished); |
|
|
|
shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); |
|
|
|
}); |
|
|
|
|
|
|
|
progressBar = rootView.findViewById(R.id.progressBar); |
|
|
|
CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview); |
|
|
|
shoutAdapter = new ShoutAdapter(getContext(), new Shout[0]); |
|
|
@ -117,19 +59,6 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj |
|
|
|
imm.hideSoftInputFromWindow(editorView.getWindowToken(), 0); |
|
|
|
return false; |
|
|
|
}); |
|
|
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { |
|
|
|
@Override |
|
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { |
|
|
|
if (dy > 0) { |
|
|
|
showRefreshLabel(); |
|
|
|
} else { |
|
|
|
hideRefreshLabel(); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
shoutboxTask = new ShoutboxTask(this::onShoutboxTaskSarted, this::onShoutboxTaskFinished); |
|
|
|
shoutboxTask.execute(SessionManager.shoutboxUrl.toString()); |
|
|
|
|
|
|
|
emojiKeyboard = rootView.findViewById(R.id.emoji_keyboard); |
|
|
|
editorView = rootView.findViewById(R.id.edior_view); |
|
|
@ -137,17 +66,12 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj |
|
|
|
InputConnection ic = editorView.getInputConnection(); |
|
|
|
setEmojiKeyboardInputConnection(ic); |
|
|
|
editorView.setOnSubmitListener(view -> { |
|
|
|
if (shoutbox == null) return; |
|
|
|
if (mViewModel.getShoutboxMutableLiveData().getValue() == null) return; |
|
|
|
if (editorView.getText().toString().isEmpty()) { |
|
|
|
editorView.setError("Required"); |
|
|
|
return; |
|
|
|
} |
|
|
|
editorView.setAlpha(0.5f); |
|
|
|
editorView.setEnabled(false); |
|
|
|
setEmojiKeyboardVisible(false); |
|
|
|
new SendShoutTask(this::onSendShoutTaskStarted, this::onSendShoutTaskFinished) |
|
|
|
.execute(shoutbox.getSendShoutUrl(), editorView.getText().toString(), shoutbox.getSc(), |
|
|
|
shoutbox.getShoutName(), shoutbox.getShoutSend(), shoutbox.getShoutUrl()); |
|
|
|
mViewModel.sendShout(editorView.getText().toString()); |
|
|
|
}); |
|
|
|
editorView.hideMarkdown(); |
|
|
|
editorView.setOnTouchListener((view, motionEvent) -> { |
|
|
@ -160,30 +84,64 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj |
|
|
|
return rootView; |
|
|
|
} |
|
|
|
|
|
|
|
private void hideRefreshLabel() { |
|
|
|
if (refreshLabel.getVisibility() == View.GONE) return; |
|
|
|
if (animator != null) animator.cancel(); |
|
|
|
animator = getRefreshLabelAnimation(); |
|
|
|
animator.start(); |
|
|
|
@Override |
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
|
|
|
super.onActivityCreated(savedInstanceState); |
|
|
|
mViewModel = ViewModelProviders.of(this).get(ShoutboxViewModel.class); |
|
|
|
mViewModel.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); |
|
|
|
|
|
|
|
mViewModel.loadShoutbox(); |
|
|
|
} |
|
|
|
|
|
|
|
private void showRefreshLabel() { |
|
|
|
if (refreshLabel.getVisibility() == View.VISIBLE) return; |
|
|
|
if (animator != null) animator.cancel(); |
|
|
|
animator = getRefreshLabelAnimation(); |
|
|
|
animator.reverse(); |
|
|
|
private void onShoutboxTaskSarted() { |
|
|
|
Timber.i("Starting shoutbox task..."); |
|
|
|
progressBar.setVisibility(View.VISIBLE); |
|
|
|
} |
|
|
|
|
|
|
|
private ValueAnimator getRefreshLabelAnimation() { |
|
|
|
ValueAnimator animator = ValueAnimator.ofFloat(-200, 0); |
|
|
|
animator.addUpdateListener(valueAnimator -> { |
|
|
|
if (((Float) valueAnimator.getAnimatedValue()).intValue() == 1) refreshLabel.setVisibility(View.VISIBLE); |
|
|
|
if (((Float) valueAnimator.getAnimatedValue()).intValue() == -199) refreshLabel.setVisibility(View.GONE); |
|
|
|
refreshLabel.setTranslationY((float) valueAnimator.getAnimatedValue()); |
|
|
|
}); |
|
|
|
animator.setInterpolator(new FastOutSlowInInterpolator()); |
|
|
|
animator.setDuration(200); |
|
|
|
return animator; |
|
|
|
private void onSendShoutTaskStarted() { |
|
|
|
Timber.i("Start sending a shout..."); |
|
|
|
editorView.setAlpha(0.5f); |
|
|
|
editorView.setEnabled(false); |
|
|
|
setEmojiKeyboardVisible(false); |
|
|
|
progressBar.setVisibility(View.VISIBLE); |
|
|
|
} |
|
|
|
|
|
|
|
private void onSendShoutTaskFinished(int resultCode, Void ignored) { |
|
|
|
editorView.setAlpha(1f); |
|
|
|
editorView.setEnabled(true); |
|
|
|
progressBar.setVisibility(View.INVISIBLE); |
|
|
|
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()); |
|
|
|
} else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { |
|
|
|
Timber.w("Failed to send shout"); |
|
|
|
Toast.makeText(getContext(), "NetworkError", Toast.LENGTH_SHORT).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void onShoutboxTaskFinished(int resultCode, Shoutbox shoutbox) { |
|
|
|
progressBar.setVisibility(View.INVISIBLE); |
|
|
|
if (resultCode == NetworkResultCodes.SUCCESSFUL) { |
|
|
|
mViewModel.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(); |
|
|
|
} else { |
|
|
|
Timber.wtf("Failed to retreive shoutbox due to unknown error"); |
|
|
|
Toast.makeText(getContext(), "Failed to retrieve shoutbox, please contact mthmmy developer team", Toast.LENGTH_LONG).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |