You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.4 KiB

package com.community.pocket.ui.main.ui.visitor;
import android.os.Build;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.core.widget.NestedScrollView;
import androidx.gridlayout.widget.GridLayout;
import com.community.pocket.R;
import com.community.pocket.data.model.Page;
import com.community.pocket.data.model.Visitor;
import com.community.pocket.ui.BaseFragment;
import com.community.pocket.util.PropertiesUtil;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import java.util.Objects;
/**
* 访客列表通用布局
*/
public abstract class VisitorMyVisitor extends BaseFragment {
//表格布局
@ViewInject(R.id.table)
private GridLayout gridLayout;
//查询开始时间
@ViewInject(R.id.start_time)
protected EditText startTime;
//查询结束时间
@ViewInject(R.id.end_time)
protected EditText endTime;
//表格列
@ViewInject(R.id.name)
protected TextView name;
@ViewInject(R.id.time)
protected TextView time;
@ViewInject(R.id.tip)
private TextView tip;
/**
* 显示备注最大长度
*/
private static final int maxLength = 5;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.initTip();
this.loadMore();
}
/**
* 初始化数据
*/
protected <T extends Visitor> void loadData(Page<T> visitors) {
if (visitors.getCurrentPage() == 1 && !visitors.isEmpty()) {
gridLayout.removeViews(4, gridLayout.getChildCount() - 4);
}
for (final Visitor visitor : visitors.getList()) {
createTextView(visitor.getName());
createTextView(DateFormat.format(getString(R.string.dateformat_visitor), visitor.getTime()));
createTextView(visitor.getNotes().length() <= maxLength ? visitor.getNotes() : visitor.getNotes().substring(0, maxLength));
switch (visitor.getStatus()) {
case ok:
Button button = new Button(getContext());
button.setText(R.string.show_notes);
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
layoutParams.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f);
button.setLayoutParams(layoutParams);
button.setOnClickListener(v -> createAlertNotes(visitor));
gridLayout.addView(button);
break;
case uncheck:
createTextView(R.string.visitor_uncheck);
break;
case fail:
createTextView(R.string.visitor_check_fail);
break;
}
}
}
/**
* 加载更多数据
*/
protected abstract Long getCurrentPage();
protected abstract VisitorLoad getViewModel();
protected void load(Long page) {
getViewModel().loadMy(startTime.getText().toString(), endTime.getText().toString(), page);
}
private void loadMore() {
VisitorFragment visitorFragment = getParentFragment(VisitorFragment.class);
visitorFragment.getScrollView().setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
load(this.getCurrentPage() + 1);
}
});
}
/**
* 创建单元格
*/
private void createTextView(CharSequence text) {
TextView textView = new TextView(getContext());
textView.setText(text);
createTextView(textView);
}
private void createTextView(@StringRes int resId) {
TextView textView = new TextView(getContext());
textView.setText(resId);
createTextView(textView);
}
private void createTextView(TextView textView) {
textView.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
textView.setBackground(getResources().getDrawable(R.drawable.border));
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
layoutParams.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f);
textView.setLayoutParams(layoutParams);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
gridLayout.addView(textView);
}
/**
* 创建显示备注信息弹窗
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void createAlertNotes(Visitor visitor) {
View view = View.inflate(getContext(), R.layout.visitor_info, null);
TextView name = view.findViewById(R.id.name);
name.setText(getString(R.string.visitor_show_name, visitor.getName()));
TextView time = view.findViewById(R.id.time);
time.setText(getString(R.string.visitor_show_time, DateFormat.format(getString(R.string.dateformat_visitor), visitor.getTime())));
TextView notes = view.findViewById(R.id.notes);
notes.setText(getString(R.string.visitor_show_notes, visitor.getNotes()));
TextView admin = view.findViewById(R.id.admin);
admin.setText(getString(R.string.visitor_show_admin, visitor.getManagerName()));
Button button = view.findViewById(R.id.close);
AlertDialog.Builder alert = new AlertDialog.Builder(Objects.requireNonNull(getContext()));
final AlertDialog alertDialog = alert.setTitle(R.string.visitor_traffic_permit).setView(view).create();
button.setOnClickListener(v -> alertDialog.dismiss());
alertDialog.show();
}
//初始化日期提示
private void initTip() {
String format = PropertiesUtil.getValue("date.pattern");
tip.setText(getString(R.string.dateformat_tip, format, DateFormat.format(format, System.currentTimeMillis())));
}
/**
* 查询访客数据
*/
@Event(R.id.query)
private void query(View view) {
load(1L);
}
}