Skip to content

Commit

Permalink
refactor: #13 keeping codes clean.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroxpepe committed Mar 12, 2022
1 parent aaed5a2 commit fd2da28
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
2 changes: 2 additions & 0 deletions MidiPlayer.Droid/ListViewAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ListTitle {
/// an Adapter class for ListView.
/// </summary>
public class ListTitleAdapter : ArrayAdapter<ListTitle> {
#nullable enable

///////////////////////////////////////////////////////////////////////////////////////////////
// Constructor
Expand Down Expand Up @@ -72,6 +73,7 @@ public class ListItem {
/// an Adapter class for ListView.
/// </summary>
public class ListItemAdapter : ArrayAdapter<ListItem> {
#nullable enable

///////////////////////////////////////////////////////////////////////////////////////////////
// Constructor
Expand Down
63 changes: 39 additions & 24 deletions MidiPlayer.Droid/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Essentials;

Expand All @@ -24,6 +23,11 @@ namespace MidiPlayer.Droid {
public partial class MainActivity : AppCompatActivity {
#nullable enable

///////////////////////////////////////////////////////////////////////////////////////////////
// Const [nouns]

const int VIEW_REFRESH_TIME = 2000; // msec.

///////////////////////////////////////////////////////////////////////////////////////////////
// Fields [nouns, noun phrases]

Expand All @@ -35,14 +39,15 @@ public partial class MainActivity : AppCompatActivity {

List<ListItem> _truckList;

Task _timer;
Task _refreshTimer;

///////////////////////////////////////////////////////////////////////////////////////////////
// Constructor

public MainActivity() {
_playList = new();
_truckList = new();
_refreshTimer = createRefreshTask();
}

///////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -68,15 +73,7 @@ protected override void OnCreate(Bundle? savedInstanceState) {

initializeComponent();
Conf.Load();

// load previous setting.
if (Env.ExistsSoundFont && Env.ExistsMidiFile) {
Synth.SoundFontPath = Env.SoundFontPath;
Synth.MidiFilePath = Env.MidiFilePath;
Title = $"MidiPlayer: {Synth.MidiFilePath.ToFileName()} {Synth.SoundFontPath.ToFileName()}";
_soundFontPath = Env.SoundFontPath;
_midiFilePath = Env.MidiFilePath;
}
loadPreviousSetting();

/// <summary>
/// add a callback function to be called when the synth is playback.
Expand All @@ -93,25 +90,14 @@ protected override void OnCreate(Bundle? savedInstanceState) {
MainThread.BeginInvokeOnMainThread(() => {
Title = $"MidiPlayer: {Synth.MidiFilePath.ToFileName()} {Synth.SoundFontPath.ToFileName()}";
});
// refresh the viewlist in a few seconds.
_timer = new Task(async () => {
while (true) {
var truckListView = FindViewById<ListView>(Resource.Id.list_view_truck);
var listItemAdapter = (ListItemAdapter) truckListView.Adapter;
RunOnUiThread(() => {
listItemAdapter.NotifyDataSetChanged();
});
await Task.Delay(2000);
}
});
_timer.Start();
_refreshTimer.Start();
};

/// <summary>
/// add a callback function to be called when the synth ended.
/// </summary>
Synth.Ended += () => {
Log.Info("Ended called.");
Log.Info("Ended called.");
if (!_playList.Ready) {
Synth.Stop();
Synth.Start();
Expand Down Expand Up @@ -253,6 +239,19 @@ static string getActualPathBy(Intent data) {
return path;
}

/// <summary>
/// load previous setting.
/// </summary>
void loadPreviousSetting() {
if (Env.ExistsSoundFont && Env.ExistsMidiFile) {
Synth.SoundFontPath = Env.SoundFontPath;
Synth.MidiFilePath = Env.MidiFilePath;
Title = $"MidiPlayer: {Synth.MidiFilePath.ToFileName()} {Synth.SoundFontPath.ToFileName()}";
_soundFontPath = Env.SoundFontPath;
_midiFilePath = Env.MidiFilePath;
}
}

/// <summary>
/// play the song.
/// </summary>
Expand Down Expand Up @@ -298,6 +297,22 @@ void updateList(Synth.Track track) {
listItem.Instrument = Synth.GetVoice(track.Index);
}

/// <summary>
/// refresh the view in a few seconds.
/// </summary>
Task createRefreshTask() {
return new(async () => {
var truckListView = FindViewById<ListView>(Resource.Id.list_view_truck);
var listItemAdapter = (ListItemAdapter) truckListView.Adapter;
while (true) {
RunOnUiThread(() => {
listItemAdapter.NotifyDataSetChanged();
});
await Task.Delay(VIEW_REFRESH_TIME);
}
});
}

/// <summary>
/// show memory information to log.
/// FIXME: delete
Expand Down

0 comments on commit fd2da28

Please sign in to comment.