|
@ -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.app.Activity; |
|
|
import android.os.Bundle; |
|
|
import android.os.Bundle; |
|
|
import android.view.LayoutInflater; |
|
|
import android.view.LayoutInflater; |
|
@ -8,16 +7,14 @@ import android.view.View; |
|
|
import android.view.ViewGroup; |
|
|
import android.view.ViewGroup; |
|
|
import android.view.inputmethod.InputConnection; |
|
|
import android.view.inputmethod.InputConnection; |
|
|
import android.view.inputmethod.InputMethodManager; |
|
|
import android.view.inputmethod.InputMethodManager; |
|
|
import android.widget.TextView; |
|
|
|
|
|
import android.widget.Toast; |
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.annotation.Nullable; |
|
|
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.LinearLayoutManager; |
|
|
import androidx.recyclerview.widget.RecyclerView; |
|
|
|
|
|
import gr.thmmy.mthmmy.R; |
|
|
import gr.thmmy.mthmmy.R; |
|
|
import gr.thmmy.mthmmy.base.BaseFragment; |
|
|
|
|
|
import gr.thmmy.mthmmy.editorview.EditorView; |
|
|
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; |
|
@ -25,85 +22,30 @@ import gr.thmmy.mthmmy.model.Shoutbox; |
|
|
import gr.thmmy.mthmmy.session.SessionManager; |
|
|
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 me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; |
|
|
import timber.log.Timber; |
|
|
import timber.log.Timber; |
|
|
|
|
|
|
|
|
public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.EmojiKeyboardOwner { |
|
|
public class ShoutboxFragment extends Fragment implements EmojiKeyboard.EmojiKeyboardOwner { |
|
|
|
|
|
|
|
|
private static final String TAG = "ShoutboxFragment"; |
|
|
|
|
|
|
|
|
|
|
|
private MaterialProgressBar progressBar; |
|
|
private MaterialProgressBar progressBar; |
|
|
private TextView refreshLabel; |
|
|
|
|
|
private ShoutboxTask shoutboxTask; |
|
|
private ShoutboxTask shoutboxTask; |
|
|
private ShoutAdapter shoutAdapter; |
|
|
private ShoutAdapter shoutAdapter; |
|
|
private EmojiKeyboard emojiKeyboard; |
|
|
private EmojiKeyboard emojiKeyboard; |
|
|
private EditorView editorView; |
|
|
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) { |
|
|
private ShoutboxViewModel mViewModel; |
|
|
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 void onShoutboxTaskFinished(int resultCode, Shoutbox shoutbox) { |
|
|
public static ShoutboxFragment newInstance() { |
|
|
progressBar.setVisibility(View.INVISIBLE); |
|
|
return new ShoutboxFragment(); |
|
|
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(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Nullable |
|
|
@Nullable |
|
|
@Override |
|
|
@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); |
|
|
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); |
|
|
progressBar = rootView.findViewById(R.id.progressBar); |
|
|
CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview); |
|
|
CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview); |
|
|
shoutAdapter = new ShoutAdapter(getContext(), new Shout[0]); |
|
|
shoutAdapter = new ShoutAdapter(getContext(), new Shout[0]); |
|
@ -117,19 +59,6 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj |
|
|
imm.hideSoftInputFromWindow(editorView.getWindowToken(), 0); |
|
|
imm.hideSoftInputFromWindow(editorView.getWindowToken(), 0); |
|
|
return false; |
|
|
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); |
|
|
emojiKeyboard = rootView.findViewById(R.id.emoji_keyboard); |
|
|
editorView = rootView.findViewById(R.id.edior_view); |
|
|
editorView = rootView.findViewById(R.id.edior_view); |
|
@ -137,17 +66,12 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj |
|
|
InputConnection ic = editorView.getInputConnection(); |
|
|
InputConnection ic = editorView.getInputConnection(); |
|
|
setEmojiKeyboardInputConnection(ic); |
|
|
setEmojiKeyboardInputConnection(ic); |
|
|
editorView.setOnSubmitListener(view -> { |
|
|
editorView.setOnSubmitListener(view -> { |
|
|
if (shoutbox == null) return; |
|
|
if (mViewModel.getShoutboxMutableLiveData().getValue() == null) return; |
|
|
if (editorView.getText().toString().isEmpty()) { |
|
|
if (editorView.getText().toString().isEmpty()) { |
|
|
editorView.setError("Required"); |
|
|
editorView.setError("Required"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
editorView.setAlpha(0.5f); |
|
|
mViewModel.sendShout(editorView.getText().toString()); |
|
|
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()); |
|
|
|
|
|
}); |
|
|
}); |
|
|
editorView.hideMarkdown(); |
|
|
editorView.hideMarkdown(); |
|
|
editorView.setOnTouchListener((view, motionEvent) -> { |
|
|
editorView.setOnTouchListener((view, motionEvent) -> { |
|
@ -160,30 +84,64 @@ public class ShoutboxFragment extends BaseFragment implements EmojiKeyboard.Emoj |
|
|
return rootView; |
|
|
return rootView; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void hideRefreshLabel() { |
|
|
@Override |
|
|
if (refreshLabel.getVisibility() == View.GONE) return; |
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
|
|
if (animator != null) animator.cancel(); |
|
|
super.onActivityCreated(savedInstanceState); |
|
|
animator = getRefreshLabelAnimation(); |
|
|
mViewModel = ViewModelProviders.of(this).get(ShoutboxViewModel.class); |
|
|
animator.start(); |
|
|
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); |
|
|
|
|
|
|
|
|
private void showRefreshLabel() { |
|
|
mViewModel.loadShoutbox(); |
|
|
if (refreshLabel.getVisibility() == View.VISIBLE) return; |
|
|
|
|
|
if (animator != null) animator.cancel(); |
|
|
|
|
|
animator = getRefreshLabelAnimation(); |
|
|
|
|
|
animator.reverse(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private ValueAnimator getRefreshLabelAnimation() { |
|
|
private void onShoutboxTaskSarted() { |
|
|
ValueAnimator animator = ValueAnimator.ofFloat(-200, 0); |
|
|
Timber.i("Starting shoutbox task..."); |
|
|
animator.addUpdateListener(valueAnimator -> { |
|
|
progressBar.setVisibility(View.VISIBLE); |
|
|
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()); |
|
|
private void onSendShoutTaskStarted() { |
|
|
}); |
|
|
Timber.i("Start sending a shout..."); |
|
|
animator.setInterpolator(new FastOutSlowInInterpolator()); |
|
|
editorView.setAlpha(0.5f); |
|
|
animator.setDuration(200); |
|
|
editorView.setEnabled(false); |
|
|
return animator; |
|
|
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 |
|
|
@Override |