Skip to content

Instantly share code, notes, and snippets.

View mayuroks's full-sized avatar

Mayur Rokade mayuroks

View GitHub Profile
/**
* This is a contract between chat view and chat presenter.
*
*/
public interface ChatContract {
interface View extends BaseView<Presenter>, EventListener {
void onMessageDelivered(ChatMessage chatMessage);
}
/**
* 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
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
@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'
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
public class Todo {
String userId;
int id;
String title;
boolean completed;
public String getFormattedInfo() {
StringBuilder builder = new StringBuilder();
builder.append("Title: " + title + "\n\n");
public interface ApiService {
@GET("/todos/1")
Flowable<Todo> getFirstTodo();
}
public class HttpClient {
private static final String API_BASE_URL = "https://jsonplaceholder.typicode.com/";
private static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
private static ApiService apiService = retrofit.create(ApiService.class);
public class SchedulerProvider {
// UI thread
public static Scheduler ui() {
return AndroidSchedulers.mainThread();
}
// IO thread
public static Scheduler io() {
return Schedulers.io();
public class MainActivity extends AppCompatActivity {
private ApiService apiService = HttpClient.getApiService();
private TextView tvStatus, tvInfo;
private Button btnHttpRequest, btnReset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);