Created
June 7, 2017 14:15
-
-
Save kalvian1060/dd7997d6f2c9bf5f60bcf5890cce43bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.app.Dialog; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.graphics.Bitmap; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Looper; | |
import android.provider.MediaStore; | |
import android.support.annotation.Nullable; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.Window; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.activeandroid.query.Select; | |
import com.pos.noota.Addons.Configuration.BaseFragment; | |
import com.pos.noota.Model.res_partner; | |
import com.pos.noota.R; | |
import com.pos.noota.Untils.BitmapUtil; | |
import com.pos.noota.Untils.FIleUtility; | |
import com.pos.noota.Untils.ImageCustomize; | |
import com.pos.noota.Untils.OdooUtility; | |
import com.pos.noota.Untils.SharedData; | |
import com.pos.noota.Untils.until; | |
import java.io.File; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
public class InputCostumer extends BaseFragment { | |
private Button btnKembali, btnSimpan; | |
private TextView tvNama, tvPhone, tvMobile, tvEmail, tvAlamat, tvAlamat2, tvWebsite, tvKodepos, tvKota; | |
private ImageView ivCamera, ivPreview; | |
private static final int TAKE_FOTO = 234; | |
private static final int SELECT_PHOTO = 321; | |
private boolean isImage = false; | |
private String path = ""; | |
private FIleUtility fileUtility; | |
private long id; | |
private boolean edit; | |
Bitmap bitMapImg; | |
private long createCostumer; | |
private OdooUtility odoo; | |
private String server,database,uid,password; | |
private SharedData sharedData; | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.input_costumer, container, false); | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
setupView(view); | |
//get data | |
server=SharedData.getKey(getActivity(),"serverAddress"); | |
database=SharedData.getKey(getActivity(),"serverDB"); | |
uid=SharedData.getKey(getActivity(),"uid"); | |
password=SharedData.getKey(getActivity(),"password"); | |
odoo=new OdooUtility(server,"object"); | |
Bundle bundle=getArguments(); | |
if(!bundle.isEmpty()){ | |
id=bundle.getLong("id"); | |
edit=bundle.getBoolean("edit"); | |
if(edit){ | |
List<res_partner> dataCostumer=new Select().from(res_partner.class).where("Id = "+id).execute(); | |
loadDataEdit(dataCostumer); | |
}else{ | |
} | |
} | |
} | |
private void loadDataEdit(List<res_partner> p){ | |
for (int pos=0;pos<p.size();pos++) { | |
tvNama.setText(p.get(pos).getName()); | |
tvPhone.setText(p.get(pos).getPhone()); | |
tvMobile.setText(p.get(pos).getMobile()); | |
tvAlamat.setText(p.get(pos).getStreet()); | |
tvKota.setText(p.get(pos).getCity()); | |
tvKodepos.setText(p.get(pos).getZip()); | |
tvEmail.setText(p.get(pos).getName()); | |
tvWebsite.setText(p.get(pos).getWebsite()); | |
tvAlamat2.setText(p.get(pos).getStreet2()); | |
} | |
btnSimpan.setText("Update Data"); | |
} | |
private void editData() { | |
clearForm(); | |
} | |
private void saveData() { | |
final String name = tvNama.getText().toString(); | |
final String phone=tvPhone.getText().toString(); | |
final String mobile=tvMobile.getText().toString(); | |
final String street = tvAlamat.getText().toString(); | |
final String city = tvKota.getText().toString(); | |
String display_name = tvNama.getText().toString(); | |
final String zip = tvKodepos.getText().toString(); | |
final String email = tvEmail.getText().toString(); | |
final String website = tvWebsite.getText().toString(); | |
final String street2 = tvAlamat2.getText().toString(); | |
String company_type = "person"; | |
String company="oke"; | |
final int country_id = 101; | |
final int company_id = 1; | |
boolean costumer = true; | |
String status = "LOCAL"; | |
//proses bikin bitmap | |
BitmapDrawable drawable = (BitmapDrawable) ivPreview.getDrawable(); | |
Bitmap bitmap = drawable.getBitmap(); | |
// bitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true); | |
final String image=BitmapUtil.encode(bitmap); | |
/* List data = Arrays. asList (new HashMap() {{ | |
put("name", name); | |
put("company_id", company_id); | |
put("street", street); | |
put("street2", street2); | |
put("city", city); | |
put("zip", zip); | |
put("country_id", country_id); | |
put("email", email); | |
put("website", website); | |
put("phone", phone); | |
put("mobile", mobile); | |
put("image",image); | |
put("create_id",uid); | |
}}); | |
if(until.statusConection(getActivity())){ | |
showProgress(); | |
createCostumer = odoo.create(listenerOdoo, database, uid,password, "res.partner", data); | |
} | |
*/ | |
res_partner rs = new res_partner(name,phone,mobile,company_id,company, | |
company_type, street, city, display_name, | |
zip, country_id, website,email, street2, costumer, status, image,Integer.parseInt(uid)); | |
//save data | |
rs.save(); | |
clearForm(); | |
until.replaceFragment(getActivity(),R.id.activity_costumer,new DataCostumer(),DataCostumer.class.getSimpleName(),"DATA_COSTUMER"); | |
} | |
private void clearForm() { | |
tvNama.setText(""); | |
tvPhone.setText(""); | |
tvMobile.setText(""); | |
tvEmail.setText(""); | |
tvAlamat.setText(""); | |
tvAlamat2.setText(""); | |
tvWebsite.setText(""); | |
tvKodepos.setText(""); | |
tvKota.setText(""); | |
} | |
private void setupView(View view) { | |
tvNama = (TextView) view.findViewById(R.id.tvNama); | |
tvPhone = (TextView) view.findViewById(R.id.tvPhone); | |
tvMobile = (TextView) view.findViewById(R.id.tvMobile); | |
tvEmail = (TextView) view.findViewById(R.id.tvEmail); | |
tvAlamat = (TextView) view.findViewById(R.id.tvAlamat); | |
tvAlamat2 = (TextView) view.findViewById(R.id.tvAlamat2); | |
tvWebsite = (TextView) view.findViewById(R.id.tvWebsite); | |
tvKodepos = (TextView) view.findViewById(R.id.tvKodepos); | |
tvKota = (TextView) view.findViewById(R.id.tvKota); | |
ivCamera = (ImageView) view.findViewById(R.id.ivCamera); | |
ivCamera.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
showDialogPilih(); | |
} | |
}); | |
ivPreview = (ImageView) view.findViewById(R.id.ivPreview); | |
ivPreview.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Dialog settingsDialog = new Dialog(getActivity()); | |
View v = getActivity().getLayoutInflater().inflate(R.layout.image_layout, null); | |
ImageView iv = (ImageView) v.findViewById(R.id.profile_img_popup); | |
Bitmap bm = ((BitmapDrawable) ivPreview.getDrawable()).getBitmap(); | |
iv.setImageBitmap(bm); | |
settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); | |
settingsDialog.setContentView(v); | |
settingsDialog.show(); | |
} | |
}); | |
btnSimpan = (Button) view.findViewById(R.id.btnSimpan); | |
btnSimpan.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if(btnSimpan.getText().equals("Update Data")){ | |
res_partner rs=res_partner.load(res_partner.class,id); | |
rs.name=tvNama.getText().toString(); | |
rs.phone=tvPhone.getText().toString();; | |
rs.mobile=tvMobile.getText().toString(); | |
rs.email=tvEmail.getText().toString(); | |
rs.street=tvAlamat.getText().toString(); | |
rs.street2=tvAlamat2.getText().toString(); | |
rs.website=tvWebsite.getText().toString(); | |
rs.zip=tvKodepos.getText().toString(); | |
rs.city=tvKota.getText().toString(); | |
rs.save(); | |
until.replaceFragment(getActivity(),R.id.fLProduk,new DataCostumer(),DataCostumer.class.getSimpleName(),"DATA_COSTUMER"); | |
Toast.makeText(getActivity(),"Update Data berhasil",Toast.LENGTH_LONG).show(); | |
}else { | |
saveData(); | |
Toast.makeText(getActivity(), "Pelanggan Baru Berhasil di Tambahkan", Toast.LENGTH_LONG).show(); | |
} | |
} | |
}); | |
btnKembali = (Button) view.findViewById(R.id.btnKembali); | |
btnKembali.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if(edit){ | |
getActivity().getSupportFragmentManager().popBackStack("DATA_EDIT",1); | |
}else { | |
getActivity().getSupportFragmentManager().popBackStack("DATA_INPUT",1); | |
} | |
} | |
}); | |
} | |
private void showDialogPilih() { | |
String[] items = {"Kamera", "Galeri"}; | |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
builder.setTitle("Pilih Gambar"); | |
builder.setItems(items, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
switch (i) { | |
case 0: | |
callKamera(); | |
break; | |
case 1: | |
callGalery(); | |
break; | |
} | |
} | |
}); | |
AlertDialog alertDialog = builder.create(); | |
alertDialog.show(); | |
} | |
//untuk handle gambar yang dipilih | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
switch (requestCode){ | |
case TAKE_FOTO: | |
//jika request berhasil | |
if(resultCode == Activity.RESULT_OK){ | |
File imageFile=fileUtility.getTempJpgImageFile(); | |
String path=imageFile.getAbsolutePath(); | |
//mengabil lokasi file | |
File file=new File(path); | |
File filetmp= ImageCustomize.resizeImage(file); | |
Bitmap bmp=ImageCustomize.decodeFile(filetmp,300); | |
ivPreview.setImageBitmap(bmp); | |
path=imageFile.getAbsolutePath().toString(); | |
isImage=true; | |
}else{ | |
isImage=false; | |
} | |
break; | |
case SELECT_PHOTO: | |
//jika request berhasil | |
if(resultCode== Activity.RESULT_OK) { | |
Uri selectedImage = data.getData(); | |
String[] filePathColumn = {MediaStore.Images.Media.DATA}; | |
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null); | |
cursor.moveToFirst(); | |
int index = cursor.getColumnIndex(filePathColumn[0]); | |
String pictPath = cursor.getString(index); | |
cursor.close(); | |
File f = new File(pictPath); | |
File file1 = ImageCustomize.resizeImage(f); | |
Bitmap bmp = ImageCustomize.decodeFile(file1, 300); | |
ivPreview.setImageBitmap(bmp); | |
path = file1.getAbsoluteFile().toString(); | |
isImage = true; | |
}else{ | |
isImage=false; | |
} | |
break; | |
} | |
super.onActivityResult(requestCode, resultCode, data); | |
} | |
private void callKamera(){ | |
fileUtility=new FIleUtility(getActivity()); | |
Uri uriSavedImage=Uri.fromFile(fileUtility.getTempJpgImageFile()); | |
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
intent.putExtra(MediaStore.EXTRA_OUTPUT,uriSavedImage); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); | |
startActivityForResult(intent,TAKE_FOTO); | |
} | |
private void callGalery(){ | |
Intent intent=new Intent(Intent.ACTION_PICK, | |
MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); | |
startActivityForResult(intent,SELECT_PHOTO); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment