1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
public class SettingsActivity extends AppCompatActivity {
private Toolbar toolbar;
private ListView listView;
private SharedPref sharedPref;
@Override
protected void onCreate (Bundle savedInstanceState) {
sharedPref = new SharedPref(this);
if(sharedPref.loadDarkModeState()) {
setTheme(R.style.AppThemeDark);
} else {
setTheme(R.style.AppTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Action Bar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
List<Item> itemList = new ArrayList<>();
itemList.add(new SectionItem("PERSONNALISER"));
itemList.add(new SwitchItem(R.drawable.item_nightmode_icon, "", "Mode sombre"));
itemList.add(new ColorItem(R.drawable.item_themecolor_icon, "Thème de l'application", "Mode color"));
itemList.add(new SectionItem("COMPTE"));
itemList.add(new EntryItem(R.drawable.item_profil_icon, "Profil"));
itemList.add(new EntryItem(R.drawable.item_logout_icon, "Se déconnecter"));
ListView listView = findViewById(R.id.listSettings);
listView.setAdapter(new ItemAdapter(this, itemList));
listView.setDivider(null);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
if(position == 2) {
}
if(position == 4) {
Toast.makeText(getApplicationContext(), "Profil", Toast.LENGTH_SHORT).show();
}
if(position == 5) {
Toast.makeText(getApplicationContext(), "Se déconnecter", Toast.LENGTH_SHORT).show();
}
}
});
}
public interface Item {
public boolean isColor();
public boolean isSwitch();
public boolean isSection();
public String getTitle();
}
public class SectionItem implements Item {
private String title;
public SectionItem (String title) {
this.title = title;
}
public String getTitle () {
return title;
}
@Override
public boolean isColor() {
return false;
}
@Override
public boolean isSwitch() {
return false;
}
@Override
public boolean isSection() {
return true;
}
}
public class SwitchItem implements Item {
private int switchIconLeft;
private String title;
private String switchIconRight;
public SwitchItem (int switchIconLeft, String title, String switchIconRight) {
this.switchIconLeft = switchIconLeft;
this.title = title;
this.switchIconRight = switchIconRight;
}
public int getSwitchIconLeft () {
return switchIconLeft;
}
public String getTitle () {
return title;
}
public String getSwitchIconRight () {
return switchIconRight;
}
@Override
public boolean isColor() {
return false;
}
@Override
public boolean isSwitch() {
return true;
}
@Override
public boolean isSection() {
return false;
}
}
public class EntryItem implements Item {
private int iconleft;
private String title;
public EntryItem (int iconleft, String title) {
this.iconleft = iconleft;
this.title = title;
}
public int getIconleft () {
return iconleft;
}
public String getTitle () {
return title;
}
@Override
public boolean isColor() {
return false;
}
@Override
public boolean isSwitch() {
return false;
}
@Override
public boolean isSection() {
return false;
}
}
public class ColorItem implements Item {
private int colorIconLeft;
private String title;
private String colorIconRight;
public ColorItem (int colorIconLeft, String title, String colorIconRight) {
this.colorIconLeft = colorIconLeft;
this.title = title;
this.colorIconRight = colorIconRight;
}
public int getColorIconLeft () {
return colorIconLeft;
}
public String getTitle () {
return title;
}
public String getColorIconRight () {
return colorIconRight;
}
@Override
public boolean isColor() {
return true;
}
@Override
public boolean isSwitch() {
return false;
}
@Override
public boolean isSection() {
return false;
}
}
public class ItemAdapter extends BaseAdapter {
private Context context;
private List<Item> itemList;
private LayoutInflater inflater;
public ItemAdapter(Context context, List<Item> itemList) {
this.context = context;
this.itemList = itemList;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount () {
return itemList.size();
}
@Override
public Object getItem (int position) {
return itemList.get(position);
}
@Override
public long getItemId (int i) {
return 0;
}
@Override
public View getView (int i, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(itemList.get(i).isSection()) {
// if header
convertView = inflater.inflate(R.layout.header_item, null);
TextView itemHeaderTitle = convertView.findViewById(R.id.headerSectionTitle);
itemHeaderTitle.setText(((SectionItem) itemList.get(i)).getTitle());
convertView.setEnabled(false);
} else if(itemList.get(i).isSwitch()) {
convertView = inflater.inflate(R.layout.switch_item, null);
// Switch Icon left
ImageView itemSwitchIconLeftView = convertView.findViewById(R.id.switchIconLeftSettings);
itemSwitchIconLeftView.setImageResource(((SwitchItem) itemList.get(i)).getSwitchIconLeft());
TextView itemSwitchNameView = convertView.findViewById(R.id.switchNameSettings);
itemSwitchNameView.setText(((SwitchItem) itemList.get(i)).getSwitchIconRight());
Switch itemSwitchIconRightView = convertView.findViewById(R.id.switchIconRightSettings);
itemSwitchIconRightView.setText(((SwitchItem) itemList.get(i)).getTitle());
itemSwitchIconRightView.setClickable(false);
if(sharedPref.loadDarkModeState()) {
itemSwitchIconRightView.setChecked(true);
}
RelativeLayout relativeLayoutSettings = convertView.findViewById(R.id.relativeLayoutSettings);
relativeLayoutSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
if(!itemSwitchIconRightView.isChecked()) {
itemSwitchIconRightView.setChecked(true);
sharedPref.setDarkModeState(true);
Intent intent = new Intent(SettingsActivity.this, SettingsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Animatoo.animateFade(SettingsActivity.this);
finish();
} else {
itemSwitchIconRightView.setChecked(false);
sharedPref.setDarkModeState(false);
Intent intent = new Intent(SettingsActivity.this, SettingsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Animatoo.animateFade(SettingsActivity.this);
finish();
}
}
});
} else if(itemList.get(i).isColor()) {
convertView = inflater.inflate(R.layout.choose_color_item, null);
// Switch Icon left
ImageView itemColorIconLeftView = convertView.findViewById(R.id.colorIconLeftSettings);
itemColorIconLeftView.setImageResource(((ColorItem) itemList.get(i)).getColorIconLeft());
TextView itemColorNameView = convertView.findViewById(R.id.colorNameSettings);
itemColorNameView.setText(((ColorItem) itemList.get(i)).getTitle());
Button itemButtonIconRightView = convertView.findViewById(R.id.colorIconRightSettings);
itemButtonIconRightView.setText(((ColorItem) itemList.get(i)).getColorIconRight());
itemButtonIconRightView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
ColorChooserDialog dialog = new ColorChooserDialog(SettingsActivity.this);
dialog.setTitle("Couleurs du thème");
dialog.setColorListener(new ColorListener() {
@Override
public void OnColorClick (View v, int color) {
ShapeDrawable d = new ShapeDrawable(new OvalShape());
d.getPaint().setStyle(Paint.Style.FILL);
itemButtonIconRightView.setBackground(d);
Intent intent = new Intent(SettingsActivity.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
overridePendingTransition(R.anim.no_animation, R.anim.no_animation);
finish();
}
});
dialog.show();
}
});
} else {
// if item
convertView = inflater.inflate(R.layout.adapter_item, null);
// Icon left
ImageView itemIconView = convertView.findViewById(R.id.item_iconleft_settings);
itemIconView.setImageResource(((EntryItem) itemList.get(i)).getIconleft());
// Title
TextView itemNameView = convertView.findViewById(R.id.item_name_settings);
itemNameView.setText(((EntryItem) itemList.get(i)).getTitle());
}
return convertView;
}
}
@Override
public void onBackPressed () {
super.onBackPressed();
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
finish();
}
@Override
public boolean onSupportNavigateUp () {
onBackPressed();
finish();
return super.onSupportNavigateUp();
}
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.no_animation, R.anim.no_animation);
}
} |
Partager