import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.cardview.widget.CardView; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.EditText; import android.widget.Toast; import com.dfoe.frimaster.DataBaseServices.MasterDB; import com.dfoe.frimaster.DataBaseServices.MasterMetaData; import com.dfoe.frimaster.RetrofitServices.RetrofitInterfaceTask; import com.google.firebase.iid.FirebaseInstanceId; import com.google.gson.JsonObject; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import okhttp3.MediaType; import okhttp3.RequestBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; import static android.Manifest.permission.READ_EXTERNAL_STORAGE; public class MainActivity extends AppCompatActivity implements View.OnClickListener,ShowListenerResponse { EditText edit_username,edit_password; CardView login_submit; MasterDB db; public JSONObject responseJsonObject = new JSONObject(); public LoginTaskHandler loginTaskHandler; private Basesalertdialog alertdialogs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title getSupportActionBar().hide(); // hide the title bar if (android.os.Build.VERSION.SDK_INT >= 23) { if (checkPermission()) { setUpUi(); } else { requestPermission(); } } else { setUpUi(); } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 5: if (grantResults.length > 0) { boolean ReadExternalStorage = grantResults[0] == PackageManager.PERMISSION_GRANTED; if (ReadExternalStorage) { setUpUi(); } } break; } } public void setUpUi() { db = MasterDB.getInstance(MainActivity.this); if(db != null) { Boolean isMasterLoggedIn = db.IsMasterLoggedIn(); if(isMasterLoggedIn) { Intent j = new Intent(MainActivity.this, DashBoard.class); startActivity(j); finish(); return; } } loginTaskHandler = new LoginTaskHandler(); setContentView(R.layout.activity_main); edit_username = (EditText)findViewById(R.id.edit_username); edit_password = (EditText)findViewById(R.id.edit_password); login_submit = (CardView)findViewById(R.id.login_submit); alertdialogs = new Basesalertdialog(MainActivity.this); ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); if (!isConnected) { //finish(); new AlertDialog.Builder(this) .setTitle("Not Connected to Internet") .setIcon(android.R.drawable.ic_dialog_alert) .setCancelable(false) .setMessage("Please Connect to Internet") .setPositiveButton("Close", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // finishAffinity(); } }).create().show(); } login_submit.setOnClickListener(this); } private void requestPermission() { ActivityCompat.requestPermissions(MainActivity.this, new String[] { READ_EXTERNAL_STORAGE, }, 5); } public boolean checkPermission() { int SecondPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.READ_EXTERNAL_STORAGE); return SecondPermissionResult == PackageManager.PERMISSION_GRANTED; } public void insertMasterDataInDatabase() { try { if(responseJsonObject.has("master_details")) { long sucess = db.insertUserDeatils(MasterMetaData.UserTable.TABLE_NAME,responseJsonObject.getJSONObject("master_details")); if(sucess > -1) { if(responseJsonObject.has("restaurent_detail")) { db.insertRestaurantDeatils(responseJsonObject.getJSONObject("restaurent_detail")); } if(responseJsonObject.has("menu_categories")) { db.insertMenuCategoryDetails(responseJsonObject.getJSONArray("menu_categories")); } if(responseJsonObject.has("restaurant_menu")) { db.insertMenuDetails(responseJsonObject.getJSONArray("restaurant_menu")); } if(responseJsonObject.has("menu_price")) { db.insertMenuPrice(responseJsonObject.getJSONArray("menu_price")); } if(responseJsonObject.has("menu_food_types")) { db.insertMenuFoodType(responseJsonObject.getJSONArray("menu_food_types")); } if(responseJsonObject.has("menu_image")) { db.insertMenuImage(responseJsonObject.getJSONArray("menu_image")); } if(responseJsonObject.has("menu_tags")) { db.insertMenuTag(responseJsonObject.getJSONArray("menu_tags")); } if(responseJsonObject.has("table_details")) { db.insertResTableDetails(responseJsonObject.getJSONArray("table_details")); } if(responseJsonObject.has("seat_details")) { db.insertResTableSeatDetails(responseJsonObject.getJSONArray("seat_details")); } if(responseJsonObject.has("currency_detail")) { db.insertResTableCurrencyDetails(responseJsonObject.getJSONObject("currency_detail")); } if(responseJsonObject.has("tax_detail")) { db.insertResTableTaxDetails(responseJsonObject.getJSONArray("tax_detail")); } if(responseJsonObject.has("order_details")) { db.insertRestaurantOrderForLogin(responseJsonObject.getJSONArray("order_details")); } if(responseJsonObject.has("payment_details")) { db.insertRestaurantPaymentFromLogin(responseJsonObject.getJSONArray("payment_details")); } if(responseJsonObject.has("foodType_detail")) { db.insertFoodType(responseJsonObject.getJSONArray("foodType_detail")); } if(responseJsonObject.has("messages")) { JSONArray arr = responseJsonObject.getJSONArray("messages"); for (int i=0;i<arr.length();i++) { // Log.e("Message",arr.getJSONObject(i).toString()); db.insertRestorentMessages(arr.getJSONObject(i)); } } Intent j = new Intent(MainActivity.this, DashBoard.class); startActivity(j); finish(); } } } catch (Exception ex) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); } } public void CallApiForDownlaodingRestaurantDetails(final int DownlaodingApi) { try { // display a progress dialog final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setCancelable(false); // set cancelable to false progressDialog.setMessage("Please Wait"); // set message progressDialog.show(); // show progress dialog Retrofit retrofit = new Retrofit.Builder() .baseUrl(CommonParams.bseURL) .addConverterFactory(GsonConverterFactory.create()) .build(); RetrofitInterfaceTask retrofitService = retrofit.create(RetrofitInterfaceTask.class); Call<JsonObject> call = null; JSONObject postJsopnData = new JSONObject(); if(DownlaodingApi == 0) { String username = edit_username.getText().toString().trim(); String password = edit_password.getText().toString().trim(); if(username.isEmpty() || username.equals("") || password.isEmpty() || password.equals("")) { if(username.isEmpty() || username.equals("")) { Toast.makeText(MainActivity.this, "Please enter valid username", Toast.LENGTH_LONG).show(); } else if(password.isEmpty() || password.equals("")) { Toast.makeText(MainActivity.this, "Please enter valid password", Toast.LENGTH_LONG).show(); } progressDialog.dismiss(); //dismis return; } password = CommonParams.encryptToMD5(password); try { postJsopnData.put("user_name", username); postJsopnData.put("user_password", password); } catch (JSONException e) { e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), postJsopnData.toString()); call = retrofitService.loginMaster(body); edit_password.setEnabled(false); edit_username.setEnabled(false); login_submit.setEnabled(false); login_submit.setClickable(false); } else if(LoginTaskHandler.Download_Menu == DownlaodingApi) { int restaurant_id = responseJsonObject.getJSONObject("master_details").getInt("restaurant_id"); try { postJsopnData.put("restaurant_id", restaurant_id); } catch (JSONException e) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), postJsopnData.toString()); call = retrofitService.getMenuDeatils(body); } else if(LoginTaskHandler.Download_Table == DownlaodingApi) { int restaurant_id = responseJsonObject.getJSONObject("master_details").getInt("restaurant_id"); try { postJsopnData.put("restaurant_id", restaurant_id); } catch (JSONException e) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), postJsopnData.toString()); call = retrofitService.getTableDeatils(body); } else if(LoginTaskHandler.Download_Restaurant == DownlaodingApi) { int restaurant_id = responseJsonObject.getJSONObject("master_details").getInt("restaurant_id"); try { postJsopnData.put("restaurant_id", restaurant_id); } catch (JSONException e) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), postJsopnData.toString()); call = retrofitService.getRestaurantDeatils(body); } else if(LoginTaskHandler.Download_Currency == DownlaodingApi) { int restaurant_id = responseJsonObject.getJSONObject("master_details").getInt("restaurant_id"); try { postJsopnData.put("restaurant_id", restaurant_id); } catch (JSONException e) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), postJsopnData.toString()); call = retrofitService.getRestaurantCurrency(body); } else if(LoginTaskHandler.Download_Taxs == DownlaodingApi) { int restaurant_id = responseJsonObject.getJSONObject("master_details").getInt("restaurant_id"); try { postJsopnData.put("restaurant_id", restaurant_id); } catch (JSONException e) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), postJsopnData.toString()); call = retrofitService.getRestaurantTax(body); } else if(LoginTaskHandler.Post_DeviceToken == DownlaodingApi) { String token = FirebaseInstanceId.getInstance().getToken(); if(token == null) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); return; } if ((token.equals("0"))) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); return; } try { int user_id = responseJsonObject.getJSONObject("master_details").getInt("user_id"); postJsopnData.put("token", token); postJsopnData.put("token_type", "fri_master"); postJsopnData.put("user_id", user_id); } catch (JSONException e) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), postJsopnData.toString()); call = retrofitService.postDeviceTosken(body); } else if(LoginTaskHandler.Download_Order == DownlaodingApi) { int restaurant_id = responseJsonObject.getJSONObject("master_details").getInt("restaurant_id"); try { postJsopnData.put("restaurant_id", restaurant_id); } catch (JSONException e) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), postJsopnData.toString()); call = retrofitService.getRestaurantOrder(body); } else if(LoginTaskHandler.Download_Payment == DownlaodingApi) { int restaurant_id = responseJsonObject.getJSONObject("master_details").getInt("restaurant_id"); try { postJsopnData.put("restaurant_id", restaurant_id); } catch (JSONException e) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), postJsopnData.toString()); call = retrofitService.getRestaurantPayment(body); } else if(LoginTaskHandler.Download_FoodType == DownlaodingApi) { int restaurant_id = responseJsonObject.getJSONObject("master_details").getInt("restaurant_id"); try { postJsopnData.put("restaurant_id", restaurant_id); } catch (JSONException e) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), postJsopnData.toString()); call = retrofitService.getRestaurantFoodType(body); } else if(LoginTaskHandler.Download_Message == DownlaodingApi) { int restaurant_id = responseJsonObject.getJSONObject("master_details").getInt("restaurant_id"); try { postJsopnData.put("restaurant_id", restaurant_id); } catch (JSONException e) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), postJsopnData.toString()); call = retrofitService.getRestaurantOrder(body); } if(call != null) { call.enqueue(new Callback<JsonObject>() { @Override public void onResponse(Call<JsonObject> call, Response<JsonObject> response) { if (response.isSuccessful()) { try { if (response.body().get("status").toString().equals("true")) { if(DownlaodingApi == 0) { progressDialog.dismiss(); //dismiss progress dialog String responseString = response.body().getAsJsonObject("master_details").toString(); JSONObject master_details = new JSONObject(responseString); responseJsonObject.put("master_details", master_details); if(loginTaskHandler != null) { loginTaskHandler.sendEmptyMessage(LoginTaskHandler.Download_Menu); } } else if(DownlaodingApi == LoginTaskHandler.Download_Menu ) { if(response.body().has("menu_categories")) { JSONArray menu_categories = new JSONArray(response.body().getAsJsonArray("menu_categories").toString()); responseJsonObject.put("menu_categories", menu_categories); JSONArray restaurant_menu = new JSONArray(response.body().getAsJsonArray("restaurant_menu").toString()); responseJsonObject.put("restaurant_menu", restaurant_menu); JSONArray menu_price = new JSONArray(response.body().getAsJsonArray("menu_price").toString()); responseJsonObject.put("menu_price", menu_price); JSONArray menu_image = new JSONArray(response.body().getAsJsonArray("menu_image").toString()); responseJsonObject.put("menu_image", menu_image); JSONArray menu_food_types = new JSONArray(response.body().getAsJsonArray("menu_food_types").toString()); responseJsonObject.put("menu_food_types", menu_food_types); JSONArray menu_tags = new JSONArray(response.body().getAsJsonArray("menu_tags").toString()); responseJsonObject.put("menu_tags", menu_tags); if(loginTaskHandler != null) { loginTaskHandler.sendEmptyMessage(LoginTaskHandler.Download_Table); } } } else if(DownlaodingApi == LoginTaskHandler.Download_Table ) { if(response.body().has("table_details")) { JSONArray table_details = new JSONArray(response.body().getAsJsonArray("table_details").toString()); responseJsonObject.put("table_details", table_details); JSONArray seat_details = new JSONArray(response.body().getAsJsonArray("seat_details").toString()); responseJsonObject.put("seat_details", seat_details); if(loginTaskHandler != null) { loginTaskHandler.sendEmptyMessage(LoginTaskHandler.Download_Restaurant); } } } else if(DownlaodingApi == LoginTaskHandler.Download_Restaurant ) { if(response.body().has("restaurent_detail")) { JSONObject restaurent_detail = new JSONObject(response.body().getAsJsonObject("restaurent_detail").toString()); responseJsonObject.put("restaurent_detail", restaurent_detail); if(loginTaskHandler != null) { loginTaskHandler.sendEmptyMessage(LoginTaskHandler.Download_Currency); } } } else if(DownlaodingApi == LoginTaskHandler.Download_Currency ) { if(response.body().has("currency_detail")) { JSONObject restaurent_detail = new JSONObject(response.body().getAsJsonObject("currency_detail").toString()); responseJsonObject.put("currency_detail", restaurent_detail); if(loginTaskHandler != null) { loginTaskHandler.sendEmptyMessage(LoginTaskHandler.Download_Taxs); } } } else if(DownlaodingApi == LoginTaskHandler.Download_Taxs ) { if(response.body().has("tax_detail")) { JSONArray restaurent_detail = new JSONArray(response.body().getAsJsonArray("tax_detail").toString()); responseJsonObject.put("tax_detail", restaurent_detail); if(loginTaskHandler != null) { loginTaskHandler.sendEmptyMessage(LoginTaskHandler.Post_DeviceToken); } } } else if(DownlaodingApi == LoginTaskHandler.Post_DeviceToken ) { if(response.body().has("status")) { if(response.body().get("status").toString().equals("true")) { if(loginTaskHandler != null) { loginTaskHandler.sendEmptyMessage(LoginTaskHandler.Download_Order); } } } } else if(DownlaodingApi == LoginTaskHandler.Download_Order ) { if(response.body().has("order_details")) { JSONArray restaurent_detail = new JSONArray(response.body().getAsJsonArray("order_details").toString()); responseJsonObject.put("order_details", restaurent_detail); JSONArray restaurent_detail1 = new JSONArray(response.body().getAsJsonArray("messages").toString()); responseJsonObject.put("messages", restaurent_detail1); if(loginTaskHandler != null) { loginTaskHandler.sendEmptyMessage(LoginTaskHandler.Download_Payment); } } } else if(DownlaodingApi == LoginTaskHandler.Download_Payment) { if(response.body().has("payment_details")) { JSONArray restaurent_detail = new JSONArray(response.body().getAsJsonArray("payment_details").toString()); responseJsonObject.put("payment_details", restaurent_detail); if(loginTaskHandler != null) { loginTaskHandler.sendEmptyMessage(LoginTaskHandler.Download_FoodType); } } } else if(DownlaodingApi == LoginTaskHandler.Download_FoodType) { if(response.body().has("foodType_detail")) { JSONArray restaurent_detail = new JSONArray(response.body().getAsJsonArray("foodType_detail").toString()); responseJsonObject.put("foodType_detail", restaurent_detail); if(loginTaskHandler != null) { loginTaskHandler.sendEmptyMessage(LoginTaskHandler.Insert_Data_In_Database); } } } else if(DownlaodingApi == LoginTaskHandler.Download_Message) { if(response.body().has("messages")) { JSONArray restaurent_messages = new JSONArray(response.body().getAsJsonArray("messages").toString()); responseJsonObject.put("messages", restaurent_messages); if(loginTaskHandler != null) { loginTaskHandler.sendEmptyMessage(LoginTaskHandler.Download_Message); } } } } else { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); // Toast.makeText(MainActivity.this, "Error while downlaoding.", Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, response.body().get("msg").toString(), Toast.LENGTH_LONG).show(); progressDialog.dismiss(); //dismiss progress dialog } } catch (Exception ex) { progressDialog.dismiss(); //dismiss progress dialog edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); // Toast.makeText(MainActivity.this, "Error while downlaoding.", Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, response.body().get("msg").toString(), Toast.LENGTH_LONG).show(); } } else { String message = response.message(); if(message.equals("Not Found")) Toast.makeText(MainActivity.this, "Server is down. Try it later", Toast.LENGTH_LONG).show(); else Toast.makeText(MainActivity.this, "Server is down. Try it later", Toast.LENGTH_LONG).show(); progressDialog.dismiss(); //dismiss progress dialog } } @Override public void onFailure(Call<JsonObject> call, Throwable t) { progressDialog.dismiss(); //dismiss progress dialog edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); if (alertdialogs.isNetworkAvailable(MainActivity.this)) { alertdialogs.serverconnectionerrorshow(getApplicationContext(),1); } else { alertdialogs.internetconnectionerrorshow(MainActivity.this,1); } } }); } } catch (Exception ex) { edit_password.setEnabled(true); edit_username.setEnabled(true); login_submit.setEnabled(true); login_submit.setClickable(true); String message = ex.getMessage().toString(); } } @Override public void callback(String value) { } public class LoginTaskHandler extends Handler { public static final int Download_Menu = 1; public static final int Download_Table = 2; public static final int Download_Restaurant = 3; public static final int Insert_Data_In_Database = 4; public static final int Download_Currency = 5; public static final int Download_Taxs = 6; public static final int Post_DeviceToken = 7; public static final int Download_Order = 8; public static final int Download_Payment = 9; public static final int Download_FoodType = 10; public static final int Download_Message = 11; @Override public void handleMessage(@NonNull Message msg) { switch (msg.what) { case Download_Menu: CallApiForDownlaodingRestaurantDetails(Download_Menu); break; case Download_Table: CallApiForDownlaodingRestaurantDetails(Download_Table); break; case Download_Restaurant: CallApiForDownlaodingRestaurantDetails(Download_Restaurant); break; case Insert_Data_In_Database: insertMasterDataInDatabase(); break; case Download_Currency: CallApiForDownlaodingRestaurantDetails(Download_Currency); break; case Download_Taxs: CallApiForDownlaodingRestaurantDetails(Download_Taxs); break; case Post_DeviceToken: CallApiForDownlaodingRestaurantDetails(Post_DeviceToken); break; case Download_Order: CallApiForDownlaodingRestaurantDetails(Download_Order); break; case Download_Payment: CallApiForDownlaodingRestaurantDetails(Download_Payment); break; case Download_FoodType: CallApiForDownlaodingRestaurantDetails(Download_FoodType); break; case Download_Message: CallApiForDownlaodingRestaurantDetails(Download_Message); break; } } } @Override public void onClick(View view) { switch (view.getId()) { case R.id.login_submit : responseJsonObject = new JSONObject(); CallApiForDownlaodingRestaurantDetails(0); break; } } }
Post Top Ad
Friday 3 January 2020
How to Called Login time multiple api called insert data to local Db
Subscribe to:
Post Comments (Atom)
Author Details
I’m an Engineer by Education. I am Software engineer,but I’ve been passionate about entrepreneurship and starting my own business.
No comments:
Post a Comment