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

Commit f4d7286

Browse files
author
Evgenii Kanivets
committed
#43[1h 30m]. Remove PeriodController. Add PeriodSpinner. Refactor Period logic.
1 parent 25909ae commit f4d7286

File tree

17 files changed

+239
-181
lines changed

17 files changed

+239
-181
lines changed

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

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
import android.support.v4.widget.DrawerLayout;
77
import android.support.v7.app.ActionBarDrawerToggle;
88
import android.support.v7.app.AppCompatActivity;
9-
import android.support.v7.widget.AppCompatSpinner;
109
import android.support.v7.widget.Toolbar;
1110
import android.view.ContextMenu;
1211
import android.view.MenuItem;
1312
import android.view.View;
1413
import android.widget.AdapterView;
15-
import android.widget.ArrayAdapter;
1614
import android.widget.ListView;
1715

1816
import com.blogspot.e_kanivets.moneytracker.DbHelper;
@@ -26,7 +24,6 @@
2624
import com.blogspot.e_kanivets.moneytracker.controller.AccountController;
2725
import com.blogspot.e_kanivets.moneytracker.controller.CategoryController;
2826
import com.blogspot.e_kanivets.moneytracker.controller.ExchangeRateController;
29-
import com.blogspot.e_kanivets.moneytracker.controller.PeriodController;
3027
import com.blogspot.e_kanivets.moneytracker.controller.RecordController;
3128
import com.blogspot.e_kanivets.moneytracker.entity.Category;
3229
import com.blogspot.e_kanivets.moneytracker.entity.Record;
@@ -39,11 +36,11 @@
3936
import com.blogspot.e_kanivets.moneytracker.report.ReportMaker;
4037
import com.blogspot.e_kanivets.moneytracker.report.base.IReport;
4138
import com.blogspot.e_kanivets.moneytracker.ui.AppRateDialog;
39+
import com.blogspot.e_kanivets.moneytracker.ui.PeriodSpinner;
4240
import com.blogspot.e_kanivets.moneytracker.ui.SummaryRecordsPresenter;
4341
import com.blogspot.e_kanivets.moneytracker.util.PrefUtils;
4442

4543
import java.util.ArrayList;
46-
import java.util.Calendar;
4744
import java.util.Collections;
4845
import java.util.List;
4946

@@ -57,22 +54,20 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
5754
private static final int REQUEST_ACTION_RECORD = 1;
5855

5956
private List<Record> recordList;
57+
private Period period;
6058

6159
private RecordController recordController;
62-
private PeriodController periodController;
6360
private ExchangeRateController rateController;
6461
private AccountController accountController;
65-
66-
private IReport report;
62+
private SummaryRecordsPresenter summaryPresenter;
6763

6864
@Bind(R.id.drawer_layout)
6965
DrawerLayout drawer;
7066

7167
@Bind(R.id.list_view)
7268
ListView listView;
7369
@Bind(R.id.spinner_period)
74-
AppCompatSpinner spinner;
75-
private SummaryRecordsPresenter summaryPresenter;
70+
PeriodSpinner spinner;
7671

7772
@Override
7873
protected int getContentViewId() {
@@ -83,8 +78,6 @@ protected int getContentViewId() {
8378
protected boolean initData() {
8479
PrefUtils.addLaunchCount();
8580

86-
periodController = new PeriodController();
87-
8881
DbHelper dbHelper = new DbHelper(MainActivity.this);
8982
IRepo<Category> categoryRepo = new CategoryRepo(dbHelper);
9083
CategoryController categoryController = new CategoryController(categoryRepo);
@@ -111,49 +104,6 @@ protected void initViews() {
111104

112105
registerForContextMenu(listView);
113106

114-
spinner.setAdapter(new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1,
115-
getResources().getStringArray(R.array.array_periods)));
116-
spinner.setSelection(PrefUtils.readUsedPeriod());
117-
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
118-
@Override
119-
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
120-
Calendar calendar = Calendar.getInstance();
121-
calendar.clear(Calendar.MINUTE);
122-
calendar.clear(Calendar.SECOND);
123-
calendar.clear(Calendar.MILLISECOND);
124-
125-
switch (position) {
126-
case 0:
127-
periodController.setPeriod(Period.dayPeriod());
128-
break;
129-
130-
case 1:
131-
periodController.setPeriod(Period.weekPeriod());
132-
break;
133-
134-
case 2:
135-
periodController.setPeriod(Period.monthPeriod());
136-
break;
137-
138-
case 3:
139-
periodController.setPeriod(Period.yearPeriod());
140-
break;
141-
142-
default:
143-
break;
144-
}
145-
146-
PrefUtils.writeUsedPeriod(position);
147-
148-
update();
149-
}
150-
151-
@Override
152-
public void onNothingSelected(AdapterView<?> parent) {
153-
154-
}
155-
});
156-
157107
View summaryView = summaryPresenter.create();
158108
listView.addHeaderView(summaryView);
159109
summaryView.setOnClickListener(new View.OnClickListener() {
@@ -163,7 +113,15 @@ public void onClick(View v) {
163113
}
164114
});
165115

166-
update();
116+
spinner.setPeriodSelectedListener(new PeriodSpinner.OnPeriodSelectedListener() {
117+
@Override
118+
public void onPeriodSelected(Period period) {
119+
MainActivity.this.period = period;
120+
PrefUtils.writePeriod(period);
121+
update();
122+
}
123+
});
124+
spinner.setPeriod(PrefUtils.readPeriod());
167125
}
168126

169127
@Override
@@ -229,10 +187,12 @@ public boolean onContextItemSelected(MenuItem item) {
229187
startAddIncomeActivity(record, AddRecordActivity.Mode.MODE_EDIT);
230188
else startAddExpenseActivity(record, AddRecordActivity.Mode.MODE_EDIT);
231189
return true;
190+
232191
case R.id.delete:
233192
recordController.delete(recordList.get(info.position));
234193
update();
235194
return true;
195+
236196
default:
237197
return super.onContextItemSelected(item);
238198
}
@@ -250,7 +210,7 @@ public void addIncome() {
250210

251211
public void showReport() {
252212
Intent intent = new Intent(MainActivity.this, ReportActivity.class);
253-
intent.putExtra(ReportActivity.KEY_PERIOD, periodController.getPeriod());
213+
intent.putExtra(ReportActivity.KEY_PERIOD, period);
254214
intent.putExtra(ReportActivity.KEY_RECORD_LIST, (ArrayList<Record>) recordList);
255215
startActivity(intent);
256216
}
@@ -272,7 +232,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
272232
}
273233

274234
private void update() {
275-
recordList = recordController.getRecordsForPeriod(periodController.getPeriod());
235+
recordList = recordController.getRecordsForPeriod(period);
276236
Collections.reverse(recordList);
277237

278238
listView.setAdapter(new RecordAdapter(MainActivity.this, recordList));
@@ -282,7 +242,7 @@ private void update() {
282242
currency = accountController.readAll().get(0).getCurrency();
283243

284244
ReportMaker reportMaker = new ReportMaker(rateController);
285-
report = reportMaker.getReport(currency, periodController.getPeriod(), recordList);
245+
IReport report = reportMaker.getReport(currency, period, recordList);
286246
summaryPresenter.update(report);
287247
}
288248

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/PeriodController.java

Lines changed: 0 additions & 68 deletions
This file was deleted.

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/Period.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
package com.blogspot.e_kanivets.moneytracker.model;
22

3+
import android.annotation.SuppressLint;
34
import android.os.Parcel;
45
import android.os.Parcelable;
56

7+
import java.text.SimpleDateFormat;
68
import java.util.Calendar;
79
import java.util.Date;
810

911
/**
10-
* Entity class for Period which consists from two dates.
12+
* Entity class for Period which consists from two dates. Immutable.
1113
* Created on 10/09/14.
1214
*
1315
* @author Evgenii Kanivets
1416
*/
1517
public class Period implements Parcelable {
18+
19+
public static final String TYPE_DAY = "day";
20+
public static final String TYPE_WEEK = "week";
21+
public static final String TYPE_MONTH = "month";
22+
public static final String TYPE_YEAR = "year";
23+
public static final String TYPE_CUSTOM = "custom";
24+
25+
@SuppressLint("SimpleDateFormat")
26+
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM, yyyy");
27+
1628
private Date first;
1729
private Date last;
30+
private String type;
1831

19-
public Period(Date first, Date last) {
20-
this.first = first;
21-
this.last = last;
32+
public Period(Date first, Date last, String type) {
33+
this.first = new Date(first.getTime());
34+
this.last = new Date(last.getTime());
35+
this.type = type;
2236
}
2337

2438
protected Period(Parcel in) {
2539
first = new Date(in.readLong());
2640
last = new Date(in.readLong());
41+
type = in.readString();
2742
}
2843

2944
public static final Creator<Period> CREATOR = new Creator<Period>() {
@@ -46,26 +61,16 @@ public Date getLast() {
4661
return last;
4762
}
4863

49-
public void setFirst(Date first) {
50-
Calendar cal = Calendar.getInstance();
51-
cal.setTime(first);
52-
53-
cal.set(Calendar.HOUR_OF_DAY, 0);
54-
cal.set(Calendar.MINUTE, 0);
55-
cal.set(Calendar.SECOND, 0);
56-
57-
this.first = cal.getTime();
64+
public String getType() {
65+
return type;
5866
}
5967

60-
public void setLast(Date last) {
61-
Calendar cal = Calendar.getInstance();
62-
cal.setTime(last);
63-
64-
cal.set(Calendar.HOUR_OF_DAY, 23);
65-
cal.set(Calendar.MINUTE, 59);
66-
cal.set(Calendar.SECOND, 59);
68+
public String getFirstDay() {
69+
return dateFormat.format(getFirst());
70+
}
6771

68-
this.last = cal.getTime();
72+
public String getLastDay() {
73+
return dateFormat.format(getLast());
6974
}
7075

7176
@Override
@@ -77,6 +82,7 @@ public int describeContents() {
7782
public void writeToParcel(Parcel dest, int flags) {
7883
dest.writeLong(first.getTime());
7984
dest.writeLong(last.getTime());
85+
dest.writeString(type);
8086
}
8187

8288
public static Period dayPeriod() {
@@ -97,7 +103,7 @@ public static Period dayPeriod() {
97103

98104
Date last = cal.getTime();
99105

100-
return new Period(first, last);
106+
return new Period(first, last, TYPE_DAY);
101107
}
102108

103109
public static Period weekPeriod() {
@@ -122,7 +128,7 @@ public static Period weekPeriod() {
122128

123129
Date last = cal.getTime();
124130

125-
return new Period(first, last);
131+
return new Period(first, last, TYPE_WEEK);
126132
}
127133

128134
public static Period monthPeriod() {
@@ -147,7 +153,7 @@ public static Period monthPeriod() {
147153

148154
Date last = cal.getTime();
149155

150-
return new Period(first, last);
156+
return new Period(first, last, TYPE_MONTH);
151157
}
152158

153159
public static Period yearPeriod() {
@@ -174,7 +180,7 @@ public static Period yearPeriod() {
174180

175181
Date last = cal.getTime();
176182

177-
return new Period(first, last);
183+
return new Period(first, last, TYPE_YEAR);
178184
}
179185

180186
@Override

0 commit comments

Comments
 (0)