Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit c0d8cab

Browse files
author
Evgeniy Kanivets
committed
Added date period and calculating first and last dauys of week
1 parent 680da76 commit c0d8cab

File tree

4 files changed

+87
-9
lines changed

4 files changed

+87
-9
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/MainActivity.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import android.widget.BaseAdapter;
1616
import android.widget.Button;
1717
import android.widget.ListView;
18+
import android.widget.TextView;
1819

1920
import com.blogspot.e_kanivets.moneytracker.R;
2021
import com.blogspot.e_kanivets.moneytracker.adapter.RecordAdapter;
@@ -40,6 +41,9 @@ public class MainActivity extends Activity implements Observer{
4041

4142
private Button btnAddIncome;
4243
private Button btnAddExpense;
44+
private Button btnReport;
45+
private TextView tvFromDate;
46+
private TextView tvToDate;
4347

4448
@Override
4549
protected void onCreate(Bundle savedInstanceState) {
@@ -52,11 +56,19 @@ protected void onCreate(Bundle savedInstanceState) {
5256
activity = this;
5357

5458
//Link views
55-
btnAddIncome = (Button) findViewById(R.id.b_add_income);
56-
btnAddExpense = (Button) findViewById(R.id.b_add_expense);
59+
btnAddIncome = (Button) findViewById(R.id.btn_add_income);
60+
btnAddExpense = (Button) findViewById(R.id.btn_add_expense);
61+
btnReport = (Button) findViewById(R.id.btn_report);
62+
63+
tvFromDate = (TextView) findViewById(R.id.tv_from_date);
64+
tvToDate = (TextView) findViewById(R.id.tv_to_date);
5765

5866
listView = (ListView) findViewById(R.id.listView);
5967

68+
//Set dates of current week
69+
tvFromDate.setText(MTHelper.getInstance().getFirstDayOfWeek());
70+
tvToDate.setText(MTHelper.getInstance().getLastDayOfWeek());
71+
6072
//Set listeners
6173
btnAddIncome.setOnClickListener(new View.OnClickListener() {
6274
@Override

app/src/main/java/com/blogspot/e_kanivets/moneytracker/helper/MTHelper.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
import com.blogspot.e_kanivets.moneytracker.util.Constants;
1212
import com.blogspot.e_kanivets.moneytracker.util.MTApp;
1313

14+
import java.text.SimpleDateFormat;
1415
import java.util.ArrayList;
16+
import java.util.Calendar;
17+
import java.util.Date;
1518
import java.util.List;
1619
import java.util.Observable;
1720

@@ -53,8 +56,8 @@ public void initialize() {
5356
//Read a record from DB
5457
Category category = new Category(cursor.getInt(idColIndex),
5558
cursor.getString(nameColIndex));
56-
Log.d(Constants.TAG, "id = " + cursor.getInt(idColIndex) +
57-
" name = " + cursor.getString(nameColIndex));
59+
/*Log.d(Constants.TAG, "id = " + cursor.getInt(idColIndex) +
60+
" name = " + cursor.getString(nameColIndex));*/
5861

5962
//Add record to list
6063
categories.add(category);
@@ -84,12 +87,12 @@ public void initialize() {
8487
cursor.getString(titleColIndex),
8588
cursor.getInt(categoryColIndex),
8689
cursor.getInt(priceColIndex));
87-
Log.d(Constants.TAG, "id = " + cursor.getInt(idColIndex) +
90+
/*Log.d(Constants.TAG, "id = " + cursor.getInt(idColIndex) +
8891
" time = " + cursor.getLong(timeColIndex) +
8992
" type = " + cursor.getInt(typeColIndex) +
9093
" title = " + cursor.getString(titleColIndex) +
9194
" category = " + cursor.getInt(categoryColIndex) +
92-
" price = " + cursor.getInt(priceColIndex));
95+
" price = " + cursor.getInt(priceColIndex));*/
9396

9497
//Add record to list
9598
records.add(record);
@@ -192,12 +195,45 @@ public String getCategoryById(int id) {
192195

193196
public int getCategoryIdByName(String name) {
194197
for(Category category : categories) {
195-
Log.d(Constants.TAG, name + " " + category.getName() + " " + category.getName().equals(name));
198+
//Log.d(Constants.TAG, name + " " + category.getName() + " " + category.getName().equals(name));
196199
if(category.getName().equals(name)) {
197200
return category.getId();
198201
}
199202
}
200203

201204
return -1;
202205
}
206+
207+
public String getFirstDayOfWeek() {
208+
// get today and clear time of day
209+
Calendar cal = Calendar.getInstance();
210+
cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
211+
cal.clear(Calendar.MINUTE);
212+
cal.clear(Calendar.SECOND);
213+
cal.clear(Calendar.MILLISECOND);
214+
215+
// set first day of week
216+
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
217+
218+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
219+
return dateFormat.format(cal.getTime());
220+
}
221+
222+
public String getLastDayOfWeek() {
223+
// get today and clear time of day
224+
Calendar cal = Calendar.getInstance();
225+
cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
226+
cal.clear(Calendar.MINUTE);
227+
cal.clear(Calendar.SECOND);
228+
cal.clear(Calendar.MILLISECOND);
229+
230+
// set first day of week
231+
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 6);
232+
cal.set(Calendar.HOUR_OF_DAY, 23);
233+
cal.set(Calendar.MINUTE, 59);
234+
cal.set(Calendar.SECOND, 59);
235+
236+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
237+
return dateFormat.format(cal.getTime());
238+
}
203239
}

app/src/main/res/layout/activity_main.xml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,35 @@
44
android:layout_height="fill_parent"
55
xmlns:android="http://schemas.android.com/apk/res/android">
66

7+
<LinearLayout
8+
android:orientation="horizontal"
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content"
11+
android:layout_gravity="center_horizontal">
12+
13+
<TextView
14+
android:layout_width="wrap_content"
15+
android:layout_height="match_parent"
16+
android:text="New Text"
17+
android:id="@+id/tv_from_date"
18+
android:layout_weight="1"
19+
android:gravity="center" />
20+
21+
<TextView
22+
android:layout_width="wrap_content"
23+
android:layout_height="match_parent"
24+
android:text="New Text"
25+
android:id="@+id/tv_to_date"
26+
android:layout_weight="1"
27+
android:gravity="center" />
28+
29+
<Button
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:text="@string/report"
33+
android:id="@+id/btn_report" />
34+
</LinearLayout>
35+
736
<ListView
837
android:layout_width="match_parent"
938
android:layout_height="match_parent"
@@ -21,7 +50,7 @@
2150
android:layout_width="match_parent"
2251
android:layout_height="wrap_content"
2352
android:text="@string/add_income"
24-
android:id="@+id/b_add_income"
53+
android:id="@+id/btn_add_income"
2554
android:layout_gravity="right"
2655
android:background="@drawable/selector_add_income"
2756
android:layout_weight="1"
@@ -31,7 +60,7 @@
3160
android:layout_width="match_parent"
3261
android:layout_height="wrap_content"
3362
android:text="@string/add_expense"
34-
android:id="@+id/b_add_expense"
63+
android:id="@+id/btn_add_expense"
3564
android:background="@drawable/selector_add_expense"
3665
android:layout_weight="1"
3766
android:padding="15dp" />

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
<string name="add">Add</string>
1717

1818
<string name="wrong_number_text">Sorry, but entered data is wrong!</string>
19+
<string name="report">Report</string>
1920

2021
</resources>

0 commit comments

Comments
 (0)