1 package org.coolreader.crengine;
2 
3 import android.view.LayoutInflater;
4 import android.view.View;
5 import android.view.ViewGroup;
6 import android.widget.ListView;
7 import android.widget.RadioButton;
8 import android.widget.TextView;
9 
10 import org.coolreader.CoolReader;
11 import org.coolreader.R;
12 
13 public class SwitchProfileDialog extends BaseDialog {
14 	CoolReader mCoolReader;
15 	ReaderView mReaderView;
16 	ListView mListView;
17 	int currentProfile;
SwitchProfileDialog(CoolReader coolReader, ReaderView readerView)18 	public SwitchProfileDialog(CoolReader coolReader, ReaderView readerView)
19 	{
20 		super(coolReader, coolReader.getResources().getString(R.string.action_switch_settings_profile), false, false);
21         setCancelable(true);
22 		this.mCoolReader = coolReader;
23 		this.mReaderView = readerView;
24 		this.mListView = new BaseListView(getContext(), false);
25 		currentProfile = this.mCoolReader.getCurrentProfile(); // TODO: get from settings
26 		mListView.setOnItemClickListener((listview, view, position, id) -> {
27 			mReaderView.setCurrentProfile(position + 1);
28 			SwitchProfileDialog.this.dismiss();
29 		});
30 		mListView.setOnItemLongClickListener((listview, view, position, id) -> {
31 			// TODO: rename?
32 			SwitchProfileDialog.this.dismiss();
33 			return true;
34 		});
35 		mListView.setLongClickable(true);
36 		mListView.setClickable(true);
37 		mListView.setFocusable(true);
38 		mListView.setFocusableInTouchMode(true);
39 		mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
40 		setView(mListView);
41 		//
42 		// cancel
43 		setFlingHandlers(mListView, SwitchProfileDialog.this::dismiss, SwitchProfileDialog.this::dismiss);
44 		mListView.setAdapter(new ProfileListAdapter());
45 	}
46 
47 	private String[] profileNames = {
48 		"Profile 1",
49 		"Profile 2",
50 		"Profile 3",
51 		"Profile 4",
52 		"Profile 5",
53 		"Profile 6",
54 		"Profile 7",
55 	};
56 
57 	class ProfileListAdapter extends BaseListAdapter {
areAllItemsEnabled()58 		public boolean areAllItemsEnabled() {
59 			return true;
60 		}
61 
isEnabled(int arg0)62 		public boolean isEnabled(int arg0) {
63 			return true;
64 		}
65 
getCount()66 		public int getCount() {
67 			return Settings.MAX_PROFILES;
68 		}
69 
getItem(int position)70 		public Object getItem(int position) {
71 			return profileNames[position];
72 		}
73 
getItemId(int position)74 		public long getItemId(int position) {
75 			return position;
76 		}
77 
getItemViewType(int position)78 		public int getItemViewType(int position) {
79 			return 0;
80 		}
81 
82 
getView(final int position, View convertView, ViewGroup parent)83 		public View getView(final int position, View convertView, ViewGroup parent) {
84 			View view;
85 			boolean isCurrentItem = position == currentProfile - 1;
86 			if ( convertView==null ) {
87 				//view = new TextView(getContext());
88 				LayoutInflater inflater = LayoutInflater.from(getContext());
89 				view = inflater.inflate(R.layout.profile_item, null);
90 			} else {
91 				view = convertView;
92 			}
93 			RadioButton cb = view.findViewById(R.id.option_value_check);
94 			TextView title = view.findViewById(R.id.option_value_text);
95 			cb.setChecked(isCurrentItem);
96 			cb.setFocusable(false);
97 			cb.setFocusableInTouchMode(false);
98 			title.setText(profileNames[position]);
99 			cb.setOnClickListener(v -> {
100 				mReaderView.setCurrentProfile(position + 1);
101 				SwitchProfileDialog.this.dismiss();
102 			});
103 			return view;
104 		}
105 
getViewTypeCount()106 		public int getViewTypeCount() {
107 			return 1;
108 		}
109 
hasStableIds()110 		public boolean hasStableIds() {
111 			return true;
112 		}
113 
isEmpty()114 		public boolean isEmpty() {
115 			return false;
116 		}
117 	}
118 
119 }
120 
121