SHARE
TWEET

Untitled

a guest Jul 21st, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. package com.iftalab.vista.factory; import android.content.Intent; import android.databinding.DataBindingUtil; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.text.TextUtils; import android.view.MenuItem; import android.view.View; import com.google.android.gms.common.GooglePlayServicesNotAvailableException; import com.google.android.gms.common.GooglePlayServicesRepairableException; import com.google.android.gms.location.places.Place; import com.google.android.gms.location.places.ui.PlacePicker; import com.google.android.gms.maps.model.LatLng; import com.iftalab.vista.R; import com.iftalab.vista.api.apiPojos.Factory; import com.iftalab.vista.api.apiPojos.Loc; import com.iftalab.vista.api.interfaces.FactoryDao; import com.iftalab.vista.dataSync.DataSyncService; import com.iftalab.vista.databinding.ActivityAddFactoryBinding; import com.iftalab.vista.toolbox.BaseActivity; import com.iftalab.vista.toolbox.I; import com.iftalab.vista.toolbox.ViewSetup; import java.util.ArrayList; import java.util.List; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class AddFactoryActivity extends BaseActivity {     ActivityAddFactoryBinding binding;     int PLACE_PICKER_REQUEST = 1;     LatLng latLng = null;     CharSequence address = "";     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         binding = DataBindingUtil.setContentView(this, R.layout.activity_add_factory);         ViewSetup.setupToolBar(this, binding.toolbar, getString(R.string.addFactory), true);         pickLocation(binding.getRoot());     }     public void done(View view) {         boolean hasError = false;         String name = binding.tiedFactoryName.getText().toString();         String address = binding.tvAddress.getText().toString();         if (TextUtils.isEmpty(name)) {             binding.tiedFactoryName.setError(getString(R.string.required));             hasError = true;         }         if (TextUtils.isEmpty(address)) {             binding.tvAddress.setError(getString(R.string.required));             hasError = true;         }         if (latLng == null) {             I.snack(binding.getRoot(), getString(R.string.youMustSelectFactoryLocation));             hasError = true;         }         if (!hasError) {             binding.fabDone.animate().scaleX(0f).scaleY(0f).start();             binding.avl.setVisibility(View.VISIBLE);             Loc loc = new Loc();             List<Double> coordinates = new ArrayList<>();             coordinates.add(latLng.latitude);             coordinates.add(latLng.longitude);             loc.setCoordinates(coordinates);             Factory factory = new Factory();             factory.setName(binding.tiedFactoryName.getText().toString());             factory.setLoc(loc);             factory.setAddress(address.toString());             FactoryDao.getAccess().add(factory).enqueue(new Callback<Factory>() {                 @Override                 public void onResponse(Call<Factory> call, Response<Factory> response) {                     binding.fabDone.animate().scaleX(1f).scaleY(1f).start();                     binding.avl.setVisibility(View.GONE);                     if (response.code() >= 200 && response.code() < 400) {                         I.snack(binding.getRoot(), getString(R.string.operationSuccessful));                         DataSyncService.startActionSyncFactory(AddFactoryActivity.this);                         finish();                     } else {                         I.snack(binding.getRoot(), getString(R.string.operationFailed) + response.message());                     }                 }                 @Override                 public void onFailure(Call<Factory> call, Throwable t) {                     I.networkError(binding.getRoot());                 }             });         }     }     public void pickLocation(View view) {         try {             PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();             startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);         } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException re) {             I.snack(binding.getRoot(), getString(R.string.googleServiceNotAvailable));         }     }     @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         if (requestCode == PLACE_PICKER_REQUEST) {             if (resultCode == RESULT_OK) {                 Place place = PlacePicker.getPlace(this, data);                 latLng = place.getLatLng();                 address = place.getAddress();                 if (!TextUtils.isEmpty(place.getName())) {                     if (binding.tiedFactoryName.getText().length() > 0) {                         showNameChangeDialog(place.getName());                     } else {                         binding.tiedFactoryName.setText(place.getName());                     }                 }                 binding.tvLocation.setText((getString(R.string.location) + latLng.latitude + ", " + latLng.longitude));                 binding.tvAddress.setText(place.getAddress());             }         }     }     private void showNameChangeDialog(CharSequence name) {         AlertDialog.Builder builder = new AlertDialog.Builder(AddFactoryActivity.this);         builder.setTitle(getString(R.string.useName));         builder.setMessage(getString(R.string.useNameMessage) + name);         builder.setPositiveButton(getString(R.string.yes), (dialog, which) -> {             binding.tiedFactoryName.setText(name);             dialog.dismiss();         });         builder.setNegativeButton(getString(R.string.no), (dialog, which) -> {             dialog.dismiss();         });         builder.create().show();     }     @Override     public boolean onOptionsItemSelected(MenuItem item) {         if (item.getItemId() == android.R.id.home) {             finish();         }         return true;     } }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top