1

I run into a quite common case of handling Video files with different quality of a video, when the app (on android in this case) should save and use one or another video file depending on a user choice (global app's setting). I can’t find some best practices and I think that I'm just googling it wrong. Anyway, both particular solutions for my example or just links to related posts will be appreciated.

Here is how it works now.

There is a Media object that is stored in db and it contains a link to MediaUrls object that has 2 urls: to HD videos and SD videos. This object comes from a server. For the backward compatibility, Media object also has a mediaUrl link which is basically the old way to store the url. In short:

public class Media {

  @Deprecated
  private String mediaUrl;
  private MediaUrls mediaUrls;

  // getters, setters, other fields: likes, views, etc
}

public class MediaUrls {

  private String hd;
  private String sd;

  // getters, setters
}

As a result, we need to have next logic somewhere to get the actual link:

  1. Return mediaUrl in case if there is no MediaUrls object

  2. Check the setting in PreferenceModel class and return sd or hd version basing on it

We can’t get the url having only the Media object because we need to know what is the current setting and we need PreferenceModel for this. This logic is put into a MediaRepository class that has getVideoUrl(Media media) object. We need that url in many places, as a result, we have to provide the MediaRepository into many classes just to get the url for a media.

In other words, if I have playMedia(Media media) method somewhere, it will have to call mdeiaRepository.getVideoUrl(media) inside to know which of urls to play. The same happens with caching and other stuff.

In the same moment, we still want to have both sd/hd urls to be present in the Media objects, as we can simply use one or another url without need to update all the other fields of the Media object.

The question is what is the way to do it without need to constantly use some third class to get a url actual at this moment.

One of the solutions that I see is to add smith like actualUrl (or reuse existing mediaUrl) field that will be cached for this purpose and rewritten in the background each time when the setting is changed (not so often, that’s a global one in the app’s settings).

Another one might be to add the PreferenceModel as a dependency to Media object, but I’m afraid that it will make the latter one too «thick».

1 Answer 1

1

Pass MediaURLs out

When asking for a URL do not resolve at the bottom of the hierarchy. Resolve it at the point you need a real URL.

This will avoid having to pass the PreferenceModel all the way down through classes that just do not care.

Unfortunately you will need PreferenceModel in the network access class/presentation classes. But considering that the preference is a UI concern that should not be to hard.

IoC

Sometimes you do need to pass context around that most classes just do not care for. Some form of IoC container that holds the current context and can be used to obtain the current setting could reduce your overheads.

Unfortunately your tests will now need to be aware of this global state.

Only retrieve preferred URLs

If the user rarely changes their preference on the SD/HD you might be able to get away with storing only a single URL for the current mode.

This will cause a full reload of affected data should the setting be changed, but no global state, or complex decision procedures are required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.