public void saveChanges() { AutoPlaylist.Rule[] modifiedRules = new AutoPlaylist.Rule[editedRules.size()]; editedReference.setRules(editedRules.toArray(modifiedRules)); if (reference.getPlaylistId() == AutoPlaylist.EMPTY.getPlaylistId()) { Library.createAutoPlaylist(this, editedReference); } else { Library.editAutoPlaylist(this, editedReference); } }
private boolean rulesChanged() { if (editedRules.size() != reference.getRules().length) { return true; } for (int i = 0; i < editedRules.size(); i++) { if (!reference.getRules()[i].equals(editedRules.get(i))) { return true; } } return false; }
private boolean validateName() { boolean valid = editedReference .getPlaylistName() .trim() .equalsIgnoreCase(reference.getPlaylistName().trim()) || Library.verifyPlaylistName(this, editedReference.getPlaylistName()) == null; if (!valid) { RecyclerView list = (RecyclerView) findViewById(R.id.list); list.scrollToPosition(0); } return valid; }
@Override public void onBackPressed() { if (!editedReference.isEqual(reference) || rulesChanged()) { AlertDialog dialog = new AlertDialog.Builder(this) .setMessage("Save changes?") .setPositiveButton( "Save", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (validateName()) { saveChanges(); Navigate.back(AutoPlaylistEditActivity.this); } } }) .setNegativeButton( "Discard", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Navigate.back(AutoPlaylistEditActivity.this); } }) .setNeutralButton("Cancel", null) .show(); Themes.themeAlertDialog(dialog); } else { super.onBackPressed(); } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_instance_no_miniplayer); reference = getIntent().getParcelableExtra(PLAYLIST_EXTRA); if (reference == null) { reference = AutoPlaylist.EMPTY; } if (savedInstanceState != null) { editedReference = savedInstanceState.getParcelable(EDITED_HEADER); editedRules = savedInstanceState.getParcelableArrayList(EDITED_RULES); } if (editedReference == null || editedRules == null) { editedReference = new AutoPlaylist(reference); editedRules = new ArrayList<>(reference.getRules().length); Collections.addAll(editedRules, editedReference.getRules()); } if (getSupportActionBar() != null) { if (reference == null) { getSupportActionBar().setTitle(R.string.playlist_auto_new); } else { getSupportActionBar().setTitle(reference.getPlaylistName()); } getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_done_24dp); } adapter = new HeterogeneousAdapter() .addSection(new RuleHeaderSingleton(editedReference)) .addSection(new RuleSection(editedRules, this)); RecyclerView list = (RecyclerView) findViewById(R.id.list); list.setAdapter(adapter); list.setLayoutManager(new LinearLayoutManager(this)); list.addItemDecoration(new BackgroundDecoration(Themes.getBackgroundElevated())); list.addItemDecoration(new DividerDecoration(this)); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.add: editedRules.add(new AutoPlaylist.Rule(AutoPlaylist.Rule.EMPTY)); adapter.notifyItemInserted(editedRules.size()); return true; case R.id.discard: if (rulesChanged()) { AlertDialog dialog = new AlertDialog.Builder(this) .setMessage("Discard changes?") .setPositiveButton( "Discard", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setNegativeButton(R.string.action_cancel, null) .show(); Themes.themeAlertDialog(dialog); } else { finish(); } return true; case android.R.id.home: if (validateName()) { if (!editedReference.isEqual(reference) || rulesChanged()) { saveChanges(); } } else { return true; } break; } return super.onOptionsItemSelected(item); }