66import android .support .v4 .widget .DrawerLayout ;
77import android .support .v7 .app .ActionBarDrawerToggle ;
88import android .support .v7 .app .AppCompatActivity ;
9- import android .support .v7 .widget .AppCompatSpinner ;
109import android .support .v7 .widget .Toolbar ;
1110import android .view .ContextMenu ;
1211import android .view .MenuItem ;
1312import android .view .View ;
1413import android .widget .AdapterView ;
15- import android .widget .ArrayAdapter ;
1614import android .widget .ListView ;
1715
1816import com .blogspot .e_kanivets .moneytracker .DbHelper ;
2624import com .blogspot .e_kanivets .moneytracker .controller .AccountController ;
2725import com .blogspot .e_kanivets .moneytracker .controller .CategoryController ;
2826import com .blogspot .e_kanivets .moneytracker .controller .ExchangeRateController ;
29- import com .blogspot .e_kanivets .moneytracker .controller .PeriodController ;
3027import com .blogspot .e_kanivets .moneytracker .controller .RecordController ;
3128import com .blogspot .e_kanivets .moneytracker .entity .Category ;
3229import com .blogspot .e_kanivets .moneytracker .entity .Record ;
3936import com .blogspot .e_kanivets .moneytracker .report .ReportMaker ;
4037import com .blogspot .e_kanivets .moneytracker .report .base .IReport ;
4138import com .blogspot .e_kanivets .moneytracker .ui .AppRateDialog ;
39+ import com .blogspot .e_kanivets .moneytracker .ui .PeriodSpinner ;
4240import com .blogspot .e_kanivets .moneytracker .ui .SummaryRecordsPresenter ;
4341import com .blogspot .e_kanivets .moneytracker .util .PrefUtils ;
4442
4543import java .util .ArrayList ;
46- import java .util .Calendar ;
4744import java .util .Collections ;
4845import 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
0 commit comments