|
|
@@ -0,0 +1,349 @@
|
|
|
+package com.usai.redant.apexdrivers.setting;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+import android.widget.BaseExpandableListAdapter;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.ProgressBar;
|
|
|
+import android.widget.Switch;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import com.usai.redant.apexdrivers.R;
|
|
|
+import com.usai.redant.apexdrivers.setting.model.AboutModel;
|
|
|
+import com.usai.redant.apexdrivers.setting.model.ActionModel;
|
|
|
+import com.usai.redant.apexdrivers.setting.model.BaseModel;
|
|
|
+import com.usai.redant.apexdrivers.setting.model.OptionModel;
|
|
|
+import com.usai.redant.apexdrivers.setting.model.SectionModel;
|
|
|
+import com.usai.redant.apexdrivers.setting.model.SwitchModel;
|
|
|
+
|
|
|
+import java.lang.ref.WeakReference;
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
+public class SettingAdapter extends BaseExpandableListAdapter {
|
|
|
+
|
|
|
+ public interface SettingAdapterDelegate {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private WeakReference<SettingAdapterDelegate> weakDelegate;
|
|
|
+ private ArrayList<SectionModel> sections;
|
|
|
+ private Context mCtx;
|
|
|
+ SettingAdapter(Context context,SettingAdapterDelegate delegate,ArrayList<SectionModel> sections) {
|
|
|
+ mCtx = context;
|
|
|
+ if (delegate != null) {
|
|
|
+ weakDelegate = new WeakReference<>(delegate);
|
|
|
+ }
|
|
|
+ this.sections = sections;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getGroupCount() {
|
|
|
+ if (sections == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return sections.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getChildrenCount(int groupPosition) {
|
|
|
+
|
|
|
+ SectionModel sectionModel = sections.get(groupPosition);
|
|
|
+ if (sectionModel.settings == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ return sectionModel.settings.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getGroup(int groupPosition) {
|
|
|
+ SectionModel sectionModel = sections.get(groupPosition);
|
|
|
+ return sectionModel;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getChild(int groupPosition, int childPosition) {
|
|
|
+ SectionModel sectionModel = sections.get(groupPosition);
|
|
|
+ return sectionModel.settings.get(childPosition);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long getGroupId(int groupPosition) {
|
|
|
+ return groupPosition;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long getChildId(int groupPosition, int childPosition) {
|
|
|
+ return childPosition;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getChildTypeCount() {
|
|
|
+ return BaseModel.settingTypeCount();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getChildType(int groupPosition, int childPosition) {
|
|
|
+ SectionModel sectionModel = sections.get(groupPosition);
|
|
|
+ return sectionModel.settings.get(childPosition).type;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean hasStableIds() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
|
|
|
+
|
|
|
+ SectionHolder holder;
|
|
|
+ if (convertView == null) {
|
|
|
+
|
|
|
+ convertView = LayoutInflater.from(mCtx).inflate(R.layout.setting_section_header,null);
|
|
|
+ holder = new SectionHolder(convertView);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ holder = (SectionHolder)convertView.getTag();
|
|
|
+ }
|
|
|
+
|
|
|
+ return convertView;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
|
|
|
+
|
|
|
+ SectionModel sectionModel = sections.get(groupPosition);
|
|
|
+ BaseModel model = sectionModel.settings.get(childPosition);
|
|
|
+ switch (model.type) {
|
|
|
+ case BaseModel.SettingTypeOption: {
|
|
|
+
|
|
|
+ OptionHolder holder;
|
|
|
+ if (convertView == null) {
|
|
|
+
|
|
|
+ convertView = LayoutInflater.from(mCtx).inflate(R.layout.setting_option_cell,null);
|
|
|
+ holder = new OptionHolder(convertView);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ holder = (OptionHolder)convertView.getTag();
|
|
|
+ }
|
|
|
+ holder.setModel(model);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case BaseModel.SettingTypeSwitch: {
|
|
|
+
|
|
|
+ SwitchHolder holder;
|
|
|
+ if (convertView == null) {
|
|
|
+
|
|
|
+ convertView = LayoutInflater.from(mCtx).inflate(R.layout.setting_switch_cell,null);
|
|
|
+ holder = new SwitchHolder(convertView);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ holder = (SwitchHolder)convertView.getTag();
|
|
|
+ }
|
|
|
+ holder.setModel(model);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case BaseModel.SettingTypeAction: {
|
|
|
+
|
|
|
+ ActionHolder holder;
|
|
|
+ if (convertView == null) {
|
|
|
+
|
|
|
+ convertView = LayoutInflater.from(mCtx).inflate(R.layout.setting_action_cell,null);
|
|
|
+ holder = new ActionHolder(convertView);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ holder = (ActionHolder)convertView.getTag();
|
|
|
+ }
|
|
|
+ holder.setModel(model);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case BaseModel.SettingTypeAbout: {
|
|
|
+
|
|
|
+ AboutHolder holder;
|
|
|
+ if (convertView == null) {
|
|
|
+
|
|
|
+ convertView = LayoutInflater.from(mCtx).inflate(R.layout.setting_about_cell,null);
|
|
|
+ holder = new AboutHolder(convertView);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ holder = (AboutHolder)convertView.getTag();
|
|
|
+ }
|
|
|
+ holder.setModel(model);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return convertView;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isChildSelectable(int groupPosition, int childPosition) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private class SectionHolder {
|
|
|
+
|
|
|
+ SectionHolder(View view) {
|
|
|
+ view.setTag(this);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private class BaseHolder implements BaseModel.ModelDelegate {
|
|
|
+
|
|
|
+ private BaseModel model;
|
|
|
+
|
|
|
+ public void setModel(BaseModel model) {
|
|
|
+ if (this.model != null) {
|
|
|
+ this.model.setDelegate(null);
|
|
|
+ }
|
|
|
+ this.model = model;
|
|
|
+ if (this.model != null) {
|
|
|
+ this.model.setDelegate(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ refreshUI();
|
|
|
+ }
|
|
|
+
|
|
|
+ public BaseModel getModel() {
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void refreshUI() {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private class OptionHolder extends BaseHolder {
|
|
|
+
|
|
|
+ private TextView titleTv;
|
|
|
+ private TextView valueTv;
|
|
|
+
|
|
|
+ OptionHolder(View view) {
|
|
|
+ view.setTag(this);
|
|
|
+
|
|
|
+ titleTv = view.findViewById(R.id.setting_option_title_tv);
|
|
|
+ valueTv = view.findViewById(R.id.setting_option_value_tv);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void refreshUI() {
|
|
|
+ super.refreshUI();
|
|
|
+
|
|
|
+ OptionModel model = (OptionModel) getModel();
|
|
|
+ if (model != null) {
|
|
|
+ titleTv.setText(model.title);
|
|
|
+ if (model.option != null) {
|
|
|
+ valueTv.setText(model.option.title);
|
|
|
+ } else {
|
|
|
+ valueTv.setText(null);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ valueTv.setText(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private class SwitchHolder extends BaseHolder {
|
|
|
+
|
|
|
+ private TextView titleTv;
|
|
|
+ private Switch aSwitch;
|
|
|
+
|
|
|
+ SwitchHolder(View view) {
|
|
|
+ view.setTag(this);
|
|
|
+
|
|
|
+ titleTv = view.findViewById(R.id.setting_switch_title_tv);
|
|
|
+ aSwitch = view.findViewById(R.id.setting_action_switch_view);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void refreshUI() {
|
|
|
+ super.refreshUI();
|
|
|
+
|
|
|
+ SwitchModel model = (SwitchModel)getModel();
|
|
|
+ if (model != null) {
|
|
|
+
|
|
|
+ titleTv.setText(model.title);
|
|
|
+ aSwitch.setChecked(model.switchValue);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ titleTv.setText(null);
|
|
|
+ aSwitch.setChecked(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private class ActionHolder extends BaseHolder {
|
|
|
+
|
|
|
+ private TextView titleTv;
|
|
|
+ private ProgressBar indicator;
|
|
|
+
|
|
|
+ ActionHolder(View view) {
|
|
|
+ view.setTag(this);
|
|
|
+
|
|
|
+ titleTv = view.findViewById(R.id.setting_action_title_tv);
|
|
|
+ indicator = view.findViewById(R.id.setting_action_indicator_view);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void refreshUI() {
|
|
|
+ super.refreshUI();
|
|
|
+
|
|
|
+ ActionModel model = (ActionModel)getModel();
|
|
|
+ if (model != null) {
|
|
|
+
|
|
|
+ titleTv.setText(model.title);
|
|
|
+ indicator.setVisibility(model.active ? View.VISIBLE : View.GONE);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ titleTv.setText(null);
|
|
|
+ indicator.setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private class AboutHolder extends BaseHolder {
|
|
|
+
|
|
|
+ private ImageView iconView,redantLogoView;
|
|
|
+ private TextView appNameTv,appVerTv,appSupportTv;
|
|
|
+
|
|
|
+ AboutHolder(View view) {
|
|
|
+ view.setTag(this);
|
|
|
+
|
|
|
+ iconView = view.findViewById(R.id.setting_about_icon_view);
|
|
|
+ redantLogoView = view.findViewById(R.id.setting_about_redant_view);
|
|
|
+ appNameTv = view.findViewById(R.id.setting_about_name_tv);
|
|
|
+ appVerTv = view.findViewById(R.id.setting_about_ver_tv);
|
|
|
+ appSupportTv = view.findViewById(R.id.setting_about_surpport_value_tv);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void refreshUI() {
|
|
|
+ super.refreshUI();
|
|
|
+
|
|
|
+ AboutModel model = (AboutModel)getModel();
|
|
|
+ if (model != null) {
|
|
|
+
|
|
|
+ iconView.setImageResource(model.appIcon());
|
|
|
+ redantLogoView.setImageResource(model.redantLogo());
|
|
|
+ appNameTv.setText(model.appName(mCtx));
|
|
|
+ appVerTv.setText(model.appVer(mCtx));
|
|
|
+ appSupportTv.setText(model.appSupport());
|
|
|
+
|
|
|
+ } else {
|
|
|
+ iconView.setImageBitmap(null);
|
|
|
+ redantLogoView.setImageBitmap(null);
|
|
|
+ appNameTv.setText(null);
|
|
|
+ appVerTv.setText(null);
|
|
|
+ appSupportTv.setText(null);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|