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:
Return
mediaUrl
in case if there is noMediaUrls
objectCheck 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».