ModifMedocActivity.java 7.36 KB
package com.example.martin.projetv5;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by martin on 12/04/2017.
 */

public class ModifMedocActivity extends AppCompatActivity implements PriseAdapterMedoc.PriseAdapterListener {

    ArrayList<Prise2> listPrises = new ArrayList<>();
    PriseAdapterMedoc adapter = null;
    ListView list = null;
    StringBDD eventBDD = null;
    Medicament medicament = null;

    void supElementList(Prise2 p, ArrayList<Prise2> listP){
        listP.remove(p);
        adapter.notifyDataSetChanged();
    }

    void supList(ArrayList<Prise2> listP){
        listP.clear();
        adapter.notifyDataSetChanged();
    }

    void addElementList(Prise2 p, ArrayList<Prise2> listP){
        listP.add(p);
    }

    public void onClickDelete(final Prise2 item, int position){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        DialogInterface.OnClickListener onClickYes = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                supElementList(item, listPrises);
                eventBDD.removePriseWithID(item.getId());

            }
        };

        builder.setMessage(R.string.modif8);
        builder.setPositiveButton(R.string.oui,onClickYes);
        builder.setNegativeButton(R.string.non,null);
        builder.show();
    }

    void gestionPrises(){
        for(int w=0; w<eventBDD.getIndiceMaxPrise(); w++) {
            Prise2 p = eventBDD.getPriseWithId(w + 1);
            if (p != null) {
                if (p.getIdMedoc() == medicament.getIdM()) addElementList(p, listPrises);
            }
        }
        Collections.sort(listPrises);
        adapter.notifyDataSetChanged();
    }

    public void onClickInfos(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        String mes =
                getString(R.string.modif2) + "\n" + medicament.getNom1() +
                "\n\n" + getString(R.string.modif3) + "\n" + medicament.getNom2() +
                "\n\n" + getString(R.string.modif4) + "\n" + medicament.getPosologies() +
                "\n\n" + getString(R.string.modif5) + "\n" + medicament.getIndications()
                ;

        builder.setMessage(mes);
        builder.setNeutralButton(R.string.ok,null);
        builder.show();
    }

    public void onClickRetour(View view){
        Intent i = new Intent(this, MainActivity.class);
        startActivity(i);
    }

    public void onClickAjoutPrise(final View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.modif6);
        builder.setMessage(R.string.modif7);

        final EditText editText = new EditText(this);
        editText.setHint("ex : 12h30");
        editText.setTextSize(20);
        builder.setView(editText);

        DialogInterface.OnClickListener onClickYes = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    String[] decoupe = editText.getText().toString().split("h");
                    int size = decoupe.length;
                    Integer h, m;

                    if (size > 2 || decoupe[0].equals(""))
                        Toast.makeText(view.getContext(), R.string.modif9, Toast.LENGTH_SHORT).show();
                    else {
                        h = Integer.parseInt(decoupe[0]);
                        if (size == 1) m = 0;
                        else m = Integer.parseInt(decoupe[1]);

                        String s;
                        if (h >= 24 || h < 0)
                            Toast.makeText(view.getContext(), R.string.modif9, Toast.LENGTH_SHORT).show();
                        else {
                            if (m >= 60 || m < 0)
                                Toast.makeText(view.getContext(), R.string.modif9, Toast.LENGTH_SHORT).show();
                            else {
                                if (m == 0) s = h.toString() + "h" + "0" + m.toString();
                                else s = h.toString() + "h" + m.toString();

                                int w=1;
                                boolean out = false;
                                while(w <= eventBDD.getIndiceMaxPrise() && !out){
                                    Prise2 p = eventBDD.getPriseWithId(w);
                                    if(p == null) out = true;
                                    else w++;
                                }

                                Prise2 p = new Prise2(w, medicament.getIdM(), "false", s);
                                eventBDD.insertPrise(p);

                                Intent i = new Intent(ModifMedocActivity.this, ServiceInitAlarme.class);
                                i.putExtra("heure", p.getHeure());
                                startService(i);

                                supList(listPrises);
                                gestionPrises();
                            }
                        }
                    }
                }catch (Exception e){
                    Toast.makeText(ModifMedocActivity.this,R.string.modif9,Toast.LENGTH_SHORT).show();
                }
            }
        };

        builder.setPositiveButton(R.string.enregistrer,onClickYes);
        builder.setNegativeButton(R.string.annuler,null);
        builder.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modif_layout);

        eventBDD = new StringBDD(this);
        eventBDD.open();

        Intent intent = getIntent();
        Integer idMedoc = intent.getIntExtra("id",0);
        medicament = eventBDD.getMedocWithId(idMedoc);

        ImageView logo = (ImageView) findViewById(R.id.logo);
        Bitmap bitmap = BitmapFactory.decodeFile(medicament.getLogo());
        logo.setImageBitmap(bitmap);
        TextView nom1 = (TextView) findViewById(R.id.nom1);
        nom1.setText(medicament.getNom1());
        TextView nom2 = (TextView) findViewById(R.id.nom2);
        nom2.setText(medicament.getNom2());
    }

    @Override
    protected void onResume() {
        eventBDD.open();
        super.onResume();

        adapter = new PriseAdapterMedoc(this,listPrises);
        adapter.addListener(this);
        list = (ListView)findViewById(R.id.listePrises);
        list.setAdapter(adapter);

        gestionPrises();
    }

    @Override
    protected void onPause() {
        super.onPause();

        adapter = new PriseAdapterMedoc(this,listPrises);
        adapter.addListener(this);
        list = (ListView)findViewById(R.id.listePrises);
        list.setAdapter(adapter);

        eventBDD.close();
        supList(listPrises);
    }
}