Skip to content

Commit

Permalink
#7 Feat: Add "Loop on Measure" settings to side drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
ditek committed Jul 20, 2019
1 parent 3861cbd commit 3818340
Show file tree
Hide file tree
Showing 14 changed files with 439 additions and 77 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

repositories {
maven {
url 'https://github.com/joytunes/USB-MIDI-Driver/raw/master/MIDIDriver/snapshots'
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/midisheetmusic/MidiPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ void init() {
stopButton.setOnClickListener(v -> Stop());
playButton.setOnClickListener(v -> Play());
fastFwdButton.setOnClickListener(v -> FastForward());
settingsButton.setOnClickListener(v -> drawer.openDrawer());
settingsButton.setOnClickListener(v -> {
drawer.deselect();
drawer.openDrawer();
});
midiButton.setOnClickListener(v -> toggleMidi());
leftHandButton.setOnClickListener(v -> toggleTrack(LEFT_TRACK));
rightHandButton.setOnClickListener(v -> toggleTrack(RIGHT_TRACK));
Expand Down
74 changes: 12 additions & 62 deletions app/src/main/java/com/midisheetmusic/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ public static class SettingsFragment extends PreferenceFragmentCompat
private Preference setAllToPiano; /** Set all instruments to piano */
private SwitchPreferenceCompat scrollVertically; /** Scroll vertically/horizontally */
private SwitchPreferenceCompat showPiano; /** Show the piano */
private SwitchPreferenceCompat showMeasures; /** Show the measure numbers */
private SwitchPreferenceCompat showLyrics; /** Show the lyrics */
private SwitchPreferenceCompat twoStaffs; /** Combine tracks into two staffs */
private ListPreference showNoteLetters; /** Show the note letters */
Expand All @@ -135,11 +134,6 @@ public static class SettingsFragment extends PreferenceFragmentCompat
private ColorPreference shade1Color; /** Right-hand color */
private ColorPreference shade2Color; /** Left-hand color */

/** Play the measures from start to end in a loop */
private SwitchPreferenceCompat playMeasuresInLoop;
private ListPreference loopStart;
private ListPreference loopEnd;

private Context context;

@Override
Expand Down Expand Up @@ -178,7 +172,6 @@ private void createView() {
createTimeSignaturePrefs(root);
createCombineIntervalPrefs(root);
createColorPrefs(root);
createPlayMeasuresInLoopPrefs(root);
setPreferenceScreen(root);
}

Expand Down Expand Up @@ -408,10 +401,6 @@ private void createColorPrefs(PreferenceScreen root) {
PreferenceCategory localPreferenceCategory = new PreferenceCategory(context);
localPreferenceCategory.setTitle("Select Colors");
root.addPreference(localPreferenceCategory);
useColors = new SwitchPreferenceCompat(context);
useColors.setTitle("Use Note Colors");
useColors.setChecked(options.useColors);
root.addPreference(useColors);

shade1Color = new ColorPreference(context);
shade1Color.setColor(options.shade1Color);
Expand All @@ -423,63 +412,28 @@ private void createColorPrefs(PreferenceScreen root) {
shade2Color.setTitle(R.string.left_hand_color);
root.addPreference(shade2Color);

useColors = new SwitchPreferenceCompat(context);
useColors.setTitle("Use Note Colors");
useColors.setChecked(options.useColors);
useColors.setOnPreferenceChangeListener((preference, isChecked) -> {
for (ColorPreference noteColorPref : noteColors) {
noteColorPref.setVisible((boolean)isChecked);
}
return true;
});
root.addPreference(useColors);

noteColors = new ColorPreference[options.noteColors.length];
for (int i = 0; i < 12; i++) {
noteColors[i] = new ColorPreference(context);
noteColors[i].setColor(options.noteColors[i]);
noteColors[i].setTitle(new String[]
{"A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"}[i]);
noteColors[i].setVisible(options.useColors);
root.addPreference(noteColors[i]);
}
}


/** Create the "Play Measures in a Loop" preference.
*
* Note that we display the measure numbers starting at 1,
* but the actual playMeasuresInLoopStart field starts at 0.
*/
private void createPlayMeasuresInLoopPrefs(PreferenceScreen root) {
String[] values = new String[options.lastMeasure + 1];
for (int measure = 0; measure < values.length; measure++) {
values[measure] = "" + (measure+1);
}

PreferenceCategory playLoopTitle = new PreferenceCategory(context);
playLoopTitle.setTitle(R.string.play_measures_in_loop_title);
root.addPreference(playLoopTitle);

showMeasures = new SwitchPreferenceCompat(context);
showMeasures.setTitle(R.string.show_measures);
showMeasures.setChecked(options.showMeasures);
root.addPreference(showMeasures);

playMeasuresInLoop = new SwitchPreferenceCompat(context);
playMeasuresInLoop.setTitle(R.string.play_measures_in_loop);
playMeasuresInLoop.setChecked(options.playMeasuresInLoop);
root.addPreference(playMeasuresInLoop);

loopStart = new ListPreference(context);
loopStart.setKey("loop_start");
loopStart.setOnPreferenceChangeListener(this);
loopStart.setTitle(R.string.play_measures_in_loop_start);
loopStart.setEntries(values);
loopStart.setEntryValues(values);
loopStart.setValueIndex(options.playMeasuresInLoopStart);
loopStart.setSummary(loopStart.getEntry() );
root.addPreference(loopStart);

loopEnd = new ListPreference(context);
loopEnd.setKey("loop_end");
loopEnd.setOnPreferenceChangeListener(this);
loopEnd.setTitle(R.string.play_measures_in_loop_end);
loopEnd.setEntries(values);
loopEnd.setEntryValues(values);
loopEnd.setValueIndex(options.playMeasuresInLoopEnd);
loopEnd.setSummary(loopEnd.getEntry() );
root.addPreference(loopEnd);
}

/** Create the "Restore Default Settings" preference */
private void createRestoreDefaultPrefs(PreferenceScreen root) {
restoreDefaults = new Preference(context);
Expand Down Expand Up @@ -532,10 +486,6 @@ private void updateOptions() {
options.shade1Color = shade1Color.getColor();
options.shade2Color = shade2Color.getColor();
options.useColors = useColors.isChecked();
options.showMeasures = showMeasures.isChecked();
options.playMeasuresInLoop = playMeasuresInLoop.isChecked();
options.playMeasuresInLoopStart = Integer.parseInt(loopStart.getValue()) - 1;
options.playMeasuresInLoopEnd = Integer.parseInt(loopEnd.getValue()) - 1;
}

@Override
Expand Down
98 changes: 95 additions & 3 deletions app/src/main/java/com/midisheetmusic/SheetMusicActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@

import androidx.drawerlayout.widget.DrawerLayout;

import com.midisheetmusic.drawerItems.ExpandableSwitchDrawerItem;
import com.midisheetmusic.sheets.ClefSymbol;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.holder.StringHolder;
import com.mikepenz.materialdrawer.model.DividerDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.SecondarySwitchDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;

import java.io.File;
Expand All @@ -55,7 +60,10 @@ public class SheetMusicActivity extends MidiHandlingActivity {

public static final String MidiTitleID = "MidiTitleID";
public static final int settingsRequestCode = 1;

public static final int ID_LOOP_ENABLE = 10;
public static final int ID_LOOP_START = 11;
public static final int ID_LOOP_END = 12;

private MidiPlayer player; /* The play/stop/rewind toolbar */
private Piano piano; /* The piano at the top */
private SheetMusic sheet; /* The sheet music */
Expand Down Expand Up @@ -128,13 +136,50 @@ public void onCreate(Bundle state) {
void createViews() {
layout = findViewById(R.id.sheet_content);

SecondarySwitchDrawerItem showMeasures = new SecondarySwitchDrawerItem()
.withName(R.string.show_measures)
.withLevel(2)
.withChecked(options.showMeasures)
.withOnCheckedChangeListener((iDrawerItem, compoundButton, isChecked) -> {
options.showMeasures = isChecked;
createSheetMusic(options);
});

SecondaryDrawerItem loopStart = new SecondaryDrawerItem()
.withIdentifier(ID_LOOP_START)
.withBadge(Integer.toString(options.playMeasuresInLoopStart + 1))
.withName(R.string.play_measures_in_loop_start)
.withLevel(2);

SecondaryDrawerItem loopEnd = new SecondaryDrawerItem()
.withIdentifier(ID_LOOP_END)
.withBadge(Integer.toString(options.playMeasuresInLoopEnd + 1))
.withName(R.string.play_measures_in_loop_end)
.withLevel(2);


ExpandableSwitchDrawerItem loopSettings = new ExpandableSwitchDrawerItem()
.withIdentifier(ID_LOOP_ENABLE)
.withName("Loop on Measures")
.withChecked(options.playMeasuresInLoop)
.withOnCheckedChangeListener((iDrawerItem, compoundButton, isChecked) -> {
options.playMeasuresInLoop = isChecked;
})
.withSubItems(showMeasures, loopStart, loopEnd);

// Drawer
drawer = new DrawerBuilder()
.withActivity(this)
.withInnerShadow(true)
.addDrawerItems(
loopSettings,
new DividerDrawerItem()
)
.inflateMenu(R.menu.sheet_menu)
.withOnDrawerItemClickListener((view, i, item) -> drawerItemClickListener(item))
.withDrawerGravity(Gravity.RIGHT)
.build();

// Make sure that the view extends over the navigation buttons area
drawer.getDrawerLayout().setFitsSystemWindows(false);
// Lock the drawer so swiping doesn't open it
Expand Down Expand Up @@ -181,18 +226,65 @@ public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}


/** Create a string list of the numbers between listStart and listEnd (inclusive) */
private String[] makeStringList(int listStart, int listEnd) {
String[] list = new String[listEnd];
for (int i = 0; i < list.length; i++) {
list[i] = Integer.toString(i + listStart);
}
return list;
}


/** Handle clicks on the drawer menu */
public boolean drawerItemClickListener(IDrawerItem item) {
switch ((int)item.getIdentifier()) {
case R.id.song_settings:
changeSettings();
drawer.closeDrawer();
break;
case R.id.save_images:
showSaveImagesDialog();
drawer.closeDrawer();
break;
case ID_LOOP_START:
// Note that we display the measure numbers starting at 1,
// but the actual playMeasuresInLoopStart field starts at 0.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.play_measures_in_loop_start);
String[] items = makeStringList(1, options.lastMeasure + 1);
builder.setItems(items, (dialog, i) -> {
options.playMeasuresInLoopStart = Integer.parseInt(items[i]) - 1;
// Make sure End is not smaller than Start
if (options.playMeasuresInLoopStart > options.playMeasuresInLoopEnd) {
options.playMeasuresInLoopEnd = options.playMeasuresInLoopStart;
drawer.updateBadge(ID_LOOP_END, new StringHolder(items[i]));
}
((SecondaryDrawerItem) item).withBadge(items[i]);
drawer.updateItem(item);
});
builder.create().show();
break;
case ID_LOOP_END:
// Note that we display the measure numbers starting at 1,
// but the actual playMeasuresInLoopEnd field starts at 0.
builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.play_measures_in_loop_end);
items = makeStringList(1, options.lastMeasure + 1);
builder.setItems(items, (dialog, i) -> {
options.playMeasuresInLoopEnd = Integer.parseInt(items[i]) - 1;
// Make sure End is not smaller than Start
if (options.playMeasuresInLoopStart > options.playMeasuresInLoopEnd) {
options.playMeasuresInLoopStart = options.playMeasuresInLoopEnd;
drawer.updateBadge(ID_LOOP_START, new StringHolder(items[i]));
}
((SecondaryDrawerItem) item).withBadge(items[i]);
drawer.updateItem(item);
});
builder.create().show();
break;
}
item.setSelected(false);
drawer.closeDrawer();
return true;
}

Expand Down
Loading

0 comments on commit 3818340

Please sign in to comment.