How to better structure your Android application
Edoardo Tognoni
292

Hi, Edoardo! Great article! I’m now trying to implement my own architecture compiling different approaches from the web together. The problem I’m facing right now is how to implement subscription to data layer events. My goal is to make subscription independent of concrete implementation, so I can easily swap old-school callbacks with event bus or RxJava subscriptions. So in my domain layer I’m declaring interfaces like this

public interface PlaybackStateObservable
{
void subscribe(@NotNull PlaybackStateObserver observer);

void unsubscribe(@NotNull PlaybackStateObserver observer);
}
public interface PlaybackStateObserver
{
void onPlaybackStateChanged(@NotNull PlaybackState playbackState);

void onCurrentAudioChanged(int position);

void onPlaybackQueueChanged(@NotNull List<AudioMediaInfo> playbackQueue);
}

and then implementing them im my presentation and data layer and subscribe/unsubscribe from them with use cases (SubscribeToPlaybackEventsUseCase, UnsubscribeToPlaybackEventsUseCase). For now I’m using callbacks. The problem is this “thread divider” layer. Because all code on data layer runs on worker thread and data layer basically should not care about threads, all my callbacks calls gets called in worker thread too. So I need somehow to handle this im my “thread divider“ layer but I don’t know how. Do you have any suggestions?

Show your support

Clapping shows how much you appreciated Stanislav Maksimov’s story.