89 lines
3.2 KiB
Java
89 lines
3.2 KiB
Java
package com.thebrokenrail.mtudining.widget;
|
|
|
|
import android.content.Context;
|
|
import android.util.AttributeSet;
|
|
import android.view.Gravity;
|
|
import android.widget.Button;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import com.google.android.material.button.MaterialButton;
|
|
import com.google.android.material.progressindicator.CircularProgressIndicator;
|
|
import com.thebrokenrail.mtudining.R;
|
|
|
|
/**
|
|
* Widget that shows a progress indicator for the loading of data.
|
|
*/
|
|
public class LoaderView extends LinearLayout {
|
|
private final LinearLayout inner;
|
|
|
|
public LoaderView(Context context, @Nullable AttributeSet attrs) {
|
|
super(context, attrs);
|
|
inner = new LinearLayout(context);
|
|
inner.setGravity(Gravity.CENTER_HORIZONTAL);
|
|
inner.setOrientation(LinearLayout.VERTICAL);
|
|
addView(inner);
|
|
setup(false, null);
|
|
// Set Margin
|
|
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
|
|
int margin = getResources().getDimensionPixelSize(R.dimen.margin);
|
|
layoutParams.setMargins(margin, margin, margin, margin);
|
|
inner.setLayoutParams(layoutParams);
|
|
}
|
|
|
|
/**
|
|
* Setup widget.
|
|
* @param isError If the error message should be shown
|
|
* @param retry Callback that is executed when the retry button is clicked
|
|
*/
|
|
public void setup(boolean isError, Runnable retry) {
|
|
if (!isError) {
|
|
setupProgress();
|
|
} else {
|
|
setupError(retry);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display progress widget.
|
|
*/
|
|
private void setupProgress() {
|
|
inner.removeAllViews();
|
|
|
|
// Create Progress Indicator
|
|
CircularProgressIndicator progressIndicator = new CircularProgressIndicator(getContext());
|
|
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
|
progressIndicator.setLayoutParams(layoutParams);
|
|
progressIndicator.setIndeterminate(true);
|
|
inner.addView(progressIndicator);
|
|
}
|
|
|
|
/**
|
|
* Display error.
|
|
* @param retry Callback that is executed when the retry button is clicked
|
|
*/
|
|
private void setupError(Runnable retry) {
|
|
inner.removeAllViews();
|
|
|
|
// Create TextView
|
|
TextView text = new TextView(getContext());
|
|
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
|
text.setLayoutParams(layoutParams);
|
|
text.setTextAlignment(TEXT_ALIGNMENT_CENTER);
|
|
text.setText(R.string.load_error);
|
|
inner.addView(text);
|
|
|
|
// Create Retry Button
|
|
Button button = new MaterialButton(getContext());
|
|
int margin = getResources().getDimensionPixelSize(R.dimen.margin);
|
|
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
|
layoutParams.setMargins(0, margin, 0, 0);
|
|
button.setLayoutParams(layoutParams);
|
|
button.setText(R.string.retry);
|
|
button.setOnClickListener(v -> retry.run());
|
|
inner.addView(button);
|
|
}
|
|
}
|