Commit cfe60c48 authored by Nadun K Bandara's avatar Nadun K Bandara

Code refactor

parent 4036c418
package com.jachdev.consumerprotection;
import android.app.Application;
import com.jachdev.consumerprotection.network.AppService;
/**
* Created by Tharushaa on 9/20/2021.
*/
public class AppApplication extends Application {
public static volatile AppApplication mApplication;
private AppService mAppService;
@Override
public void onCreate() {
super.onCreate();
mApplication = this;
mAppService = AppService.getInstance();
}
/**
* this is Singleton class
* @return : application class
*/
public static AppApplication getInstance() {
//Double check locking pattern
if (mApplication == null) { //Check for the first time
synchronized (AppApplication.class) { //Check for the second time.
//if there is no instance available... create new one
if (mApplication == null)
mApplication = new AppApplication();
}
}
return mApplication;
}
public AppService getAppService() {
return mAppService;
}
}
package com.jachdev.consumerprotection;
/**
* Created by Tharushaa on 9/20/2021.
*/
public class AppConstant {
public static final String BASE_URL = "http://192.168.1.4:5000/";
public static final String PATH_SIGN_UP = "register";
public static final String PATH_LOGIN = "login";
public static final String PATH_GET_CATEGORIES = "getCategories";
public static final String PATH_GET_ORGANIZATION = "getOrganization";
public static final String PATH_ADD_ORGANIZATION = "addOrganization";
public static final String PATH_ADD_SHOP = "addShop";
public static final String PATH_GET_SHOP = "getShops";
public static final String PATH_GET_ESSENTIALS = "getEssentials";
public static final String PATH_COMPLAINTS = "complaints";
public static final String PATH_GET_ALL_SHOPS = "getAllShops";
public static final String PATH_GET_NOTIFICATIONS = "getNotifications";
public static final String ORG_LOGO_KEY = "logo";
public static final int IMAGE_LIMIT = 1;
public static final String IMAGE_PATH = "img/";
}
package com.jachdev.consumerprotection.data;
import com.google.gson.Gson;
/**
* Created by Tharushaa on 9/20/2021.
*/
public class AppResponse {
private int code;
private String message;
private Object data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public <T> T getObjectToType(Class<T> t) {
try{
Gson gson = new Gson();
String json = gson.toJson(data);
Object value = new Gson().fromJson(json, t);
return (T)value;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public <T> T getObjectToType(String stringJson, Class<T> t) {
try{
Gson gson = new Gson();
Object value = new Gson().fromJson(stringJson, t);
return (T)value;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public void setData(Object data) {
this.data = data;
}
}
package com.jachdev.consumerprotection.network;
import com.jachdev.consumerprotection.AppApplication;
import com.jachdev.consumerprotection.AppConstant;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by Tharushaa on 9/20/2021.
*/
public class AppService {
private static final String BASE_URL = AppConstant.BASE_URL;
public static volatile AppService service;
private Retrofit server;
public AppService() {
server = provideRetrofit();
}
/**
* this is Singleton class
* @return : application class
*/
public static AppService getInstance() {
//Double check locking pattern
if (service == null) { //Check for the first time
synchronized (AppService.class) { //Check for the second time.
//if there is no instance available... create new one
if (service == null)
service = new AppService();
}
}
return service;
}
private Retrofit provideRetrofit(){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.level(HttpLoggingInterceptor.Level.BODY);
Interceptor queryInterceptor1 = new Interceptor() {
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request request = chain.request();
HttpUrl url = request.url().newBuilder().build();
request = request.newBuilder().url(url).build();
return chain.proceed(request);
}
};
OkHttpClient client = new OkHttpClient.Builder()
// .connectTimeout(120, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.addInterceptor(queryInterceptor1)
.build();
return new Retrofit.Builder().baseUrl(BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
private ApiInterface provideRetrofitService(Retrofit retrofit){
return retrofit.create(ApiInterface.class);
}
public ApiInterface getServer() {
return provideRetrofitService(server);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment