Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. package com.pareandroid.note_mvppattern;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.support.annotation.NonNull;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.Menu;
  8. import android.view.MenuInflater;
  9. import android.view.MenuItem;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12.  
  13. import retrofit2.Call;
  14. import retrofit2.Callback;
  15. import retrofit2.Response;
  16.  
  17. public class EditorActivity extends AppCompatActivity {
  18.  
  19.     EditText et_tittle,et_note;
  20.     ProgressDialog progressDialog;
  21.     ApiInterface apiInterface;
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_editor);
  27.  
  28.         et_tittle = findViewById(R.id.tittle);
  29.         et_note = findViewById(R.id.note);
  30.         progressDialog  = new ProgressDialog(this);
  31.         progressDialog.setMessage("Mohon Tunggu...");
  32.  
  33.     }
  34.  
  35.     @Override
  36.     public boolean onCreateOptionsMenu(Menu menu) {
  37.         MenuInflater inflater = getMenuInflater();
  38.         inflater.inflate(R.menu.menu_editor,menu);
  39.         return  true;
  40.     }
  41.  
  42.     @Override
  43.     public boolean onOptionsItemSelected(MenuItem item) {
  44.         switch (item.getItemId()){
  45.             case R.id.save:
  46.                 //Save
  47.  
  48.                 String tittle = et_tittle.getText().toString().trim();
  49.                 String note = et_note.getText().toString().trim();
  50.                 int color = -2184710;
  51.  
  52.                 if (tittle.isEmpty()){
  53.                     et_tittle.setError("Mohon Isi kan Judul");
  54.                 }else if (note.isEmpty()) {
  55.                     et_note.setError("Masukkan Catatatan..");
  56.                 }else{
  57.                     SaveNote(tittle,note,color);
  58.                 }
  59.                 return  true;
  60.             default:
  61.  
  62.                 return super.onOptionsItemSelected(item);
  63.         }
  64.     }
  65.  
  66.     private void SaveNote(final String tittle, final String note, final int color) {
  67.         progressDialog.show();
  68.         apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
  69.         Call <Notes> call = apiInterface.savenote(tittle, note, color);
  70.  
  71.         call.enqueue(new Callback<Notes>() {
  72.             @Override
  73.             public void onResponse(@NonNull Call<Notes> call, @NonNull Response<Notes> response) {
  74.                 progressDialog.dismiss();
  75.  
  76.                 if (response.isSuccessful() && response.body() != null){
  77.  
  78.                     Boolean success = response.body().getSuccess();
  79.                     if (success){
  80.                         Toast.makeText(EditorActivity.this,
  81.                                 response.body().getMessage(),
  82.                                 Toast.LENGTH_SHORT).show();
  83.                             finish();
  84.                     }
  85.                     else{
  86.                         Toast.makeText(EditorActivity.this,
  87.                                 response.body().getMessage(),
  88.                                 Toast.LENGTH_SHORT).show();
  89.                     }
  90.                 }
  91.             }
  92.  
  93.             @Override
  94.             public void onFailure(@NonNull Call<Notes> call, @NonNull Throwable t) {
  95.                 progressDialog.dismiss();
  96.                 Toast.makeText(EditorActivity.this,
  97.                         t.getLocalizedMessage(),
  98.                         Toast.LENGTH_SHORT).show();
  99.             }
  100.         });
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement