ModifMedocActivity.java
7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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);
}
}