Skip to content

Instantly share code, notes, and snippets.

View mayuroks's full-sized avatar

Mayur Rokade mayuroks

View GitHub Profile
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
@mayuroks
mayuroks / build.gradle
Created August 15, 2018 14:27
build.gradle
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'
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
/**
* 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 is a contract between chat view and chat presenter.
*
*/
public interface ChatContract {
interface View extends BaseView<Presenter>, EventListener {
void onMessageDelivered(ChatMessage chatMessage);
}
/**
* 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;
/**
* 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;
/**
* Remote data source.
*
*/
public class RemoteDataSource implements DataSource {
private static RemoteDataSource INSTANCE;
private static EventService mEventService = EventServiceImpl.getInstance();
private EventListener mRepoEventListener;
/**
* 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();
/**
* Main interface to listen to server events.
*
*/
public interface EventListener {
void onConnect(Object... args);
void onDisconnect(Object... args);