«« More Music Having Stable and Development Branches »»
blog header image
Updating From File Changes Made Outside of AudioMan

On the home stretch with AudioMan 0.2.0 and I need to write this out to get it organised in my head. To get AudioMan to function like it did in 0.1.2, I need to get two things working that are kind of related: updating songs in AudioMan when they were edited by another application and automatic capitalization.

If you refresh your memory about how components in AudioMan communicate you'll see that step B2 involves getting a bunch of "records" from the repository that match what the user wants to see.

After it gets these records it has to check each one to make sure the file hasn't changed since the last time we read the information. The repository stores a time stamp for each file so AudioMan just has to compare it against the file's last modified time.

If the file has changed, AudioMan has to read the new data from the file (B3) and then update the repository (B4). After that the models can be safely updated (B5).

The old way used to do this one record at a time and then return the finished array but I want to be more flexible. If a file has changed AudioMan might have to do some long operation (like recompute the TRM) which could delay updating the model and affect UI responsiveness. Speaking of that, I have an idea of how to do those long TRM calculations in the background but I'll talk about that in another post.

So what I could do is add all of the records to the model and then update all of the stale ones via the TracksMutator afterwards. The models listen to the TracksMutator so they would be updated as well.

This also has the advantage of keeping all the write operations in the mutators and all of the read operations in the controller, which makes flow control nice and simple.

Automatic capitalization squeezes into this update procedure as well. I see the logic going something like:

//one record of many, inside a loop
AudioData ad = (AudioData)records.get(i);

//copy the original AudioData so we can compare later
AudioData newAd = new AudioData(ad);

Long timeStamp = (Long)ad.get(AudioDataKey.TIME_STAMP);
File f = (File)ad.get(AudioDataKey.FILE);

if (f.canRead() && (f.lastModified() > timeStamp.longValue()))
{
   try
   {
      //need to update the data from the file
      newAd = FileInformation.getInstance().getTag(f);

      //set the key on this new AudioData so
      //the repository recognizes the record on update
      newAd.set(AudioDataKey.KEY, ad.get(AudioDataKey.KEY));
   }
   catch (IOException ioe)
   {
      //file went missing after f.canRead()
      //skip update, don't change newAd
   }
}

//check the formatting
newAd = format(newAd);

if (false == newAd.equals(ad))
{
   //add to a list of records to update
   //after the model is loaded
   toUpdate.add(newAd);
}

The format() method does the capitalization and it returns the AudioData object unmodified if no formatting needed to be done.

After looping through all of the records the model is loaded with all of the records. Then the TracksMutator is used to update the records needing updating.

//update the stale tracks
for (int i = 0; i < needUpdating.size(); i++)
{
   AudioData ad = (AudioData)needUpdating.get(i);
   File f = (File)ad.get(AudioDataKey.FILE);

   if (f.canWrite())
   {
      try
      {
         TracksMutator.getInstance().updateTrack(ad);
      }
      catch (IOException ioe)
      {
         //file went wonky after f.canWrite()
         //could not update so do nothing
      }
   }
}

Explicity synchronizing with the file system like this will all go away once I figure out how to use file system listeners. You can attach listeners to directories to see if files are added/removed/modified, etc. In AudioMan in order to see a change in the file system you have to select another view and then come back. With file listeners I can update the data in the model and view as soon as the files are changed, just like Windows Explorer does.

I know this is possible because Eclipse does it since 3.0 M7.

Posted at February 23, 2004 at 03:58 AM EST
Last updated February 23, 2004 at 03:58 AM EST
Comments
Google
 
Search scope: Web ryanlowe.ca