check update and login
This commit is contained in:
@@ -2,6 +2,8 @@ package ch.pizzalink.android.activity;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
@@ -11,9 +13,11 @@ import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import ch.pizzalink.android.R;
|
||||
import ch.pizzalink.android.api.ApiConstants;
|
||||
import ch.pizzalink.android.api.ApiErrorUtils;
|
||||
import ch.pizzalink.android.api.ApiService;
|
||||
import ch.pizzalink.android.api.ResponseArray;
|
||||
@@ -23,6 +27,7 @@ import ch.pizzalink.android.helper.DisplayHelper;
|
||||
import ch.pizzalink.android.helper.NetworkHelper;
|
||||
import ch.pizzalink.android.helper.SessionHelper;
|
||||
import ch.pizzalink.android.helper.SharedPrefsHelper;
|
||||
import ch.pizzalink.android.model.AppVersionModel;
|
||||
import ch.pizzalink.android.model.CategoryModel;
|
||||
import ch.pizzalink.android.model.cart.CartInfoModel;
|
||||
import retrofit2.Call;
|
||||
@@ -38,11 +43,54 @@ public class SplashActivity extends BaseActivity {
|
||||
ButterKnife.bind(this);
|
||||
//DisplayHelper.changeStatusColor();
|
||||
if(NetworkHelper.isNetworkAvailable())
|
||||
getCategoryList();
|
||||
checkVersion();
|
||||
else
|
||||
DialogHelper.showNoNetworkDialog();
|
||||
}
|
||||
|
||||
private void checkVersion(){
|
||||
Call<ResponseObject<AppVersionModel>> call = ApiService.apiInterface.checkUpdate(getCheckUpdateParams());
|
||||
call.enqueue(new Callback<ResponseObject<AppVersionModel>>() {
|
||||
@Override
|
||||
public void onResponse(Call<ResponseObject<AppVersionModel>> call, Response<ResponseObject<AppVersionModel>> response) {
|
||||
if(response.isSuccessful()){
|
||||
if(response.body().isSuccess()){
|
||||
getCategoryList();
|
||||
}
|
||||
else{
|
||||
DialogHelper.showUpdateAppDialog(SplashActivity.this);
|
||||
}
|
||||
}
|
||||
else{
|
||||
ApiErrorUtils.parseError(response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<ResponseObject<AppVersionModel>> call, Throwable t) {
|
||||
DialogHelper.showFailedDialog();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private HashMap<String, Object> getCheckUpdateParams(){
|
||||
HashMap<String, Object> params = new HashMap<>();
|
||||
params.put("application", ApiConstants.APP_TYPE_ID_ANDROID);
|
||||
params.put("version", getAppVersionCode());
|
||||
return params;
|
||||
}
|
||||
|
||||
private int getAppVersionCode(){
|
||||
PackageInfo pInfo = null;
|
||||
try {
|
||||
pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
return pInfo.versionCode;
|
||||
}
|
||||
|
||||
private void getCategoryList(){
|
||||
Call<ResponseArray<CategoryModel>> call = ApiService.apiInterface.getAllCategories();
|
||||
call.enqueue(new Callback<ResponseArray<CategoryModel>>() {
|
||||
@@ -66,6 +114,7 @@ public class SplashActivity extends BaseActivity {
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
private void checkCustomerToken(){
|
||||
|
||||
if(SessionHelper.isCustomerLoggedIn()){
|
||||
@@ -77,6 +126,16 @@ public class SplashActivity extends BaseActivity {
|
||||
else
|
||||
openActivity(LoginActivity.class);
|
||||
}
|
||||
*/
|
||||
|
||||
private void checkCustomerToken(){
|
||||
if(SessionHelper.isCustomerLoggedIn()){
|
||||
getCartItemCount();
|
||||
}
|
||||
else{
|
||||
openActivity(LoginActivity.class);
|
||||
}
|
||||
}
|
||||
|
||||
private void openActivity(final Class<?> cls){
|
||||
Handler handler = new Handler();
|
||||
@@ -130,6 +189,7 @@ public class SplashActivity extends BaseActivity {
|
||||
|
||||
}
|
||||
else
|
||||
//response.body().getErrorCode()
|
||||
ApiErrorUtils.parseError(response);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,4 +8,6 @@ public class ApiConstants {
|
||||
public static final int CATEGORY_ID_EKSTRA_KEBAP = 9998;
|
||||
public static final int PRODUCT_ID_WUNSCHPIZZA = 56;
|
||||
public static final int PRODUCT_ID_EKSTRA_KEBAP = 91;
|
||||
public static final String APP_TYPE_ID_ANDROID = "2";
|
||||
public static final int APP_ERROR_CODE_AUTHORIZATION = 1;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ public class ApiEndPoints {
|
||||
public static final String API_GET_CUSTOMER_ADDRESSES = PREFIX + "getAddresses" + SUFFIX;
|
||||
public static final String API_GET_PAYMENT_METHODS = PREFIX + "getPaymentMethodsArray" + SUFFIX;
|
||||
public static final String API_CREATE_ORDER = PREFIX + "addOrder" + SUFFIX + "&token=";
|
||||
public static final String API_CHECK_UPDATE = PREFIX + "checkUpdate" + SUFFIX;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package ch.pizzalink.android.api;
|
||||
|
||||
import android.content.Intent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
import ch.pizzalink.android.R;
|
||||
import ch.pizzalink.android.activity.BaseActivity;
|
||||
import ch.pizzalink.android.activity.LoginActivity;
|
||||
import ch.pizzalink.android.helper.DialogHelper;
|
||||
import ch.pizzalink.android.helper.SharedPrefsHelper;
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Converter;
|
||||
import retrofit2.Response;
|
||||
@@ -15,6 +19,18 @@ public class ApiErrorUtils {
|
||||
Converter<ResponseBody, ApiError> converter = ApiService.retrofit.responseBodyConverter(ApiError.class, new Annotation[0]);
|
||||
ApiError error = new ApiError();
|
||||
error.setStatusCode(response.code());
|
||||
|
||||
BaseResponse baseResponse = (BaseResponse) response.body();
|
||||
|
||||
if(baseResponse.getErrorCode() == ApiConstants.APP_ERROR_CODE_AUTHORIZATION){
|
||||
SharedPrefsHelper.clearCustomerInfo();
|
||||
SharedPrefsHelper.clearCustomerToken();
|
||||
SharedPrefsHelper.setCustomerLoggedIn(false);
|
||||
BaseActivity.currentActivity.startActivity(new Intent(BaseActivity.currentActivity, LoginActivity.class));
|
||||
BaseActivity.currentActivity.finishAffinity();
|
||||
return error;
|
||||
}
|
||||
|
||||
try {
|
||||
error = converter.convert(response.errorBody());
|
||||
DialogHelper.showDialogWithPositiveButton(BaseActivity.currentActivity, error.getMessage());
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.HashMap;
|
||||
|
||||
import ch.pizzalink.android.model.AddProductToBasketResponseModel;
|
||||
import ch.pizzalink.android.model.AddressModel;
|
||||
import ch.pizzalink.android.model.AppVersionModel;
|
||||
import ch.pizzalink.android.model.PaymentMethodModel;
|
||||
import ch.pizzalink.android.model.PaymentMethodsResponseModel;
|
||||
import ch.pizzalink.android.model.ShippingMethodModel;
|
||||
@@ -95,4 +96,9 @@ public interface ApiInterface {
|
||||
@POST
|
||||
Call<ResponseObject<Integer>> createOrder(@Url String url, @FieldMap HashMap<String, Object> body);
|
||||
|
||||
@FormUrlEncoded
|
||||
@POST(ApiEndPoints.API_CHECK_UPDATE)
|
||||
Call<ResponseObject<AppVersionModel>> checkUpdate(@FieldMap HashMap<String, Object> body);
|
||||
|
||||
|
||||
}
|
||||
|
||||
28
app/src/main/java/ch/pizzalink/android/api/BaseResponse.java
Normal file
28
app/src/main/java/ch/pizzalink/android/api/BaseResponse.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package ch.pizzalink.android.api;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by cimenmus on 22.10.2017.
|
||||
*/
|
||||
|
||||
public class BaseResponse implements Serializable {
|
||||
|
||||
@SerializedName("success") private boolean success;
|
||||
@SerializedName("message") private String message;
|
||||
@SerializedName("error_code") private int errorCode;
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public int getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
}
|
||||
@@ -10,20 +10,10 @@ import java.util.List;
|
||||
* Created by cimenmus on 05/10/2017.
|
||||
*/
|
||||
|
||||
public class ResponseArray<T> implements Serializable {
|
||||
public class ResponseArray<T> extends BaseResponse implements Serializable {
|
||||
|
||||
@SerializedName("success") private boolean success;
|
||||
@SerializedName("message") private String message;
|
||||
@SerializedName("data") private ArrayList<T> data;
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public ArrayList<T> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -8,20 +8,10 @@ import java.io.Serializable;
|
||||
* Created by cimenmus on 05/10/2017.
|
||||
*/
|
||||
|
||||
public class ResponseObject<T> implements Serializable {
|
||||
public class ResponseObject<T> extends BaseResponse implements Serializable {
|
||||
|
||||
@SerializedName("success") private boolean success;
|
||||
@SerializedName("message") private String message;
|
||||
@SerializedName("data") private T data;
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package ch.pizzalink.android.helper;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import com.afollestad.materialdialogs.DialogAction;
|
||||
@@ -69,6 +71,12 @@ public class DialogHelper {
|
||||
.content(BaseActivity.currentActivity.getString(R.string.no_network_message))
|
||||
.positiveText(R.string.ok)
|
||||
.positiveColor(ContextCompat.getColor(BaseActivity.currentActivity, R.color.colorPrimary))
|
||||
.onPositive(new MaterialDialog.SingleButtonCallback() {
|
||||
@Override
|
||||
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
|
||||
BaseActivity.currentActivity.finishAffinity();
|
||||
}
|
||||
})
|
||||
.show();
|
||||
}
|
||||
|
||||
@@ -103,4 +111,25 @@ public class DialogHelper {
|
||||
.show();
|
||||
}
|
||||
|
||||
public static void showUpdateAppDialog(Context context) {
|
||||
|
||||
new MaterialDialog.Builder(context)
|
||||
.title(R.string.alert)
|
||||
.content(R.string.alert_update_app)
|
||||
.positiveText(R.string.update_app)
|
||||
.positiveColor(ContextCompat.getColor(context, R.color.colorPrimary))
|
||||
.cancelable(false)
|
||||
.onPositive(new MaterialDialog.SingleButtonCallback() {
|
||||
@Override
|
||||
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setData(Uri.parse("market://details?id=ch.pizzalink.android"));
|
||||
BaseActivity.currentActivity.startActivity(intent);
|
||||
BaseActivity.currentActivity.finishAffinity();
|
||||
}
|
||||
})
|
||||
.show();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package ch.pizzalink.android.model;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
/**
|
||||
* Created by cimenmus on 22.10.2017.
|
||||
*/
|
||||
|
||||
public class AppVersionModel {
|
||||
|
||||
@Expose
|
||||
@SerializedName("application")
|
||||
private String appType;
|
||||
|
||||
private int version;
|
||||
|
||||
public String getAppType() {
|
||||
return appType;
|
||||
}
|
||||
|
||||
public void setAppType(String appType) {
|
||||
this.appType = appType;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -99,6 +99,8 @@
|
||||
<string name="cancel">İptal</string>
|
||||
<string name="loading">Lütfen bekleyiniz...</string>
|
||||
<string name="alert_logout">Çıkış yapmak istediğinize emin misiniz?</string>
|
||||
<string name="alert_update_app">Pizzalink uygulamasını kullanabilmek için uygulamanızı güncellemeniz gerekmektedir.</string>
|
||||
<string name="update_app">GÜNCELLE</string>
|
||||
|
||||
<string name="month_name_january">Jan.</string>
|
||||
<string name="month_name_february">Feb.</string>
|
||||
|
||||
Reference in New Issue
Block a user