-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJourneysActivity.java
More file actions
223 lines (186 loc) · 8.12 KB
/
JourneysActivity.java
File metadata and controls
223 lines (186 loc) · 8.12 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.sfu.aqua.carbontracker;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.sfu.aqua.carbontracker.models.Journey;
import com.sfu.aqua.carbontracker.models.Singleton;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class JourneysActivity extends AppCompatActivity {
private List<Journey> myJourneys = new ArrayList<Journey>();
public static final int REQUEST_CODE_EDITING_JOURNEY = 222;
public static final double laptopHours = .012;
int flag;
public static Intent makeIntent(Context context) {
return new Intent(context, JourneysActivity.class);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_journeys);
changeFont((TextView) findViewById(R.id.textForJourney));
extractFlag();
populateListView();
registerClickCallback();
setupGraphBtn();
setupDeleteBtn();
}
private void extractFlag() {
Intent intent = getIntent();
flag = intent.getIntExtra("unitFlag", 0);
Log.i("IN SELECT", " " + flag);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.footprint_graph, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
super.onBackPressed();
break;
case R.id.home:
Intent intent = new Intent(JourneysActivity.this, MainActivity.class);
startActivity(intent);
finish();
break;
}
return super.onOptionsItemSelected(item);
}
private void populateJourneyList() {
myJourneys.clear();
for (Journey journey : Singleton.journeyList) {
myJourneys.add(journey);
//myJourneys.add(new Journey(journey.getDate(), journey.getRoute(), journey.getEmission(), journey.getTransportation()));
}
}
private void changeFont(TextView tv) {
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/bold.ttf");
tv.setTypeface(typeface);
}
private void setupGraphBtn() {
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.btn_graph);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(JourneysActivity.this, FootprintGraph.class);
intent.putExtra("flag", flag);
startActivity(intent);
}
});
}
private void setupDeleteBtn() {
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.deleteJourney);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Singleton.journeyList.size() == 0) {
Toast.makeText(JourneysActivity.this, R.string.nothing_to_delete, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(JourneysActivity.this, R.string.tap_journey_to_delete, Toast.LENGTH_SHORT)
.show();
ListView list = (ListView) findViewById(R.id.Journey);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Singleton.journeyList.remove(position);
populateListView();
Toast.makeText(JourneysActivity.this, R.string.click_on_delete_to_finish, Toast.LENGTH_SHORT)
.show();
Button btn = (Button) findViewById(R.id.deleteJourney);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerClickCallback();
}
});
}
});
}
}
});
}
private void populateListView() {
populateJourneyList();
ArrayAdapter<Journey> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.Journey);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Journey> {
public MyListAdapter() {
super(JourneysActivity.this, R.layout.journey_view, myJourneys);
//System.out.println("=======" + myCars.size());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Make sure we have a view to work with (may have been given null)
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.journey_view, parent, false);
}
Journey currentJourney = myJourneys.get(position);
ImageView imageView = (ImageView) itemView.findViewById(R.id.img_journey);
imageView.setImageResource(currentJourney.getIcon());
// Condition:
TextView condtionText = (TextView) itemView.findViewById(R.id.txt_transportation);
condtionText.setText(currentJourney.getName());
// Make:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
TextView makeText = (TextView) itemView.findViewById(R.id.txt_date);
makeText.setText(dateFormat.format(currentJourney.getDate().getTime()));
// Year:
TextView yearText = (TextView) itemView.findViewById(R.id.txt_emission);
if (flag == 0) {
yearText.setText(String.format("%.2f", currentJourney.getEmission()) + "g ");
} else {
yearText.setText(String.format("%.2f", currentJourney.getEmission() / laptopHours) + " laptop hours ");
}
TextView modelText = (TextView) itemView.findViewById(R.id.txt_distance);
modelText.setText(currentJourney.getDistance() + "km ");
return itemView;
}
}
private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.Journey);
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(JourneysActivity.this, "rrrr", Toast.LENGTH_SHORT).show();
Intent intent = EditJourneyActivity.makeIntent(JourneysActivity.this);
intent.putExtra("TRANS", Singleton.journeyList.get(position).getTransportation());
intent.putExtra("EDIT_INDEX", position);
startActivityForResult(intent, REQUEST_CODE_EDITING_JOURNEY);
return true;
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_EDITING_JOURNEY:
if (resultCode == Activity.RESULT_OK) {
populateListView();
}
}
}
}