This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"userId": 1, | |
"id": 1, | |
"title": "delectus aut autem", | |
"completed": false | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
implementation 'com.android.support:appcompat-v7:27.1.1' | |
implementation 'androidx.constraintlayout:constraintlayout:1.1.2' | |
// RxJava library and RxAndroid for Android specific bindings | |
implementation 'io.reactivex.rxjava2:rxjava:2.2.0' | |
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' | |
// Retrofit and OkHttp | |
implementation 'com.squareup.retrofit2:retrofit:2.4.0' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseApplication extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
// Observer to detect if the app is in background or foreground. | |
AppLifeCycleObserver lifeCycleObserver | |
= new AppLifeCycleObserver(getApplicationContext()); | |
// Adding the above observer to process lifecycle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Closes the socket connection when app is in background and | |
* connects to socket when the app is in foreground. | |
*/ | |
public class AppLifeCycleObserver implements LifecycleObserver { | |
private Context mContext; | |
/** | |
* Use this constructor to create a new AppLifeCycleObserver |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is a contract between chat view and chat presenter. | |
* | |
*/ | |
public interface ChatContract { | |
interface View extends BaseView<Presenter>, EventListener { | |
void onMessageDelivered(ChatMessage chatMessage); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Listens to user actions and sends data to remote data source. | |
* <p> | |
* Presenter implements EventListener. Whenever the server sends events | |
* to the Repository, it passes those events to the Presenter via EventListener. | |
*/ | |
public class ChatPresenter implements ChatContract.Presenter { | |
@NonNull | |
private final BaseSchedulerProvider mSchedulerProvider; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Repository can send events to a remote data source and can listen | |
* for incoming events from remote data source as well. This bidirectional | |
* flow of events is what makes the app realtime. | |
* | |
* Repository implements {@link DataSource} which can send and receive events. | |
*/ | |
public class Repository implements DataSource { | |
private static Repository INSTANCE = null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Remote data source. | |
* | |
*/ | |
public class RemoteDataSource implements DataSource { | |
private static RemoteDataSource INSTANCE; | |
private static EventService mEventService = EventServiceImpl.getInstance(); | |
private EventListener mRepoEventListener; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Main interface for accessing data. It extends EventListener to receive | |
* incoming events from a remote data source. In this case, a chat server. | |
*/ | |
public interface DataSource extends EventListener { | |
void connect(String username) throws URISyntaxException; | |
void disconnect(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Main interface to listen to server events. | |
* | |
*/ | |
public interface EventListener { | |
void onConnect(Object... args); | |
void onDisconnect(Object... args); |