11package com .blogspot .e_kanivets .moneytracker .activity ;
22
3- import android .util .Pair ;
4- import android .view .View ;
5- import android .view .ViewGroup ;
6- import android .view .ViewTreeObserver ;
3+ import android .content .DialogInterface ;
4+ import android .support .v7 .app .AlertDialog ;
75import android .widget .ExpandableListView ;
8- import android .widget .ListView ;
96
107import com .blogspot .e_kanivets .moneytracker .R ;
118import com .blogspot .e_kanivets .moneytracker .activity .base .BaseActivity ;
129import com .blogspot .e_kanivets .moneytracker .adapter .ExpandableListReportAdapter ;
13- import com .blogspot .e_kanivets .moneytracker .adapter .ReportItemAdapter ;
1410import com .blogspot .e_kanivets .moneytracker .controller .AccountController ;
15- import com .blogspot .e_kanivets .moneytracker .controller .CategoryController ;
1611import com .blogspot .e_kanivets .moneytracker .controller .ExchangeRateController ;
17- import com .blogspot .e_kanivets .moneytracker .controller .RecordController ;
1812import com .blogspot .e_kanivets .moneytracker .DbHelper ;
19- import com .blogspot .e_kanivets .moneytracker .model .Category ;
2013import com .blogspot .e_kanivets .moneytracker .model .Period ;
2114import com .blogspot .e_kanivets .moneytracker .model .Record ;
22- import com .blogspot .e_kanivets .moneytracker .model .Report ;
2315import com .blogspot .e_kanivets .moneytracker .repo .AccountRepo ;
24- import com .blogspot .e_kanivets .moneytracker .repo .CategoryRepo ;
2516import com .blogspot .e_kanivets .moneytracker .repo .ExchangeRateRepo ;
26- import com .blogspot .e_kanivets .moneytracker .repo .base .IRepo ;
27- import com .blogspot .e_kanivets .moneytracker .repo .RecordRepo ;
28- import com .blogspot .e_kanivets .moneytracker .util .Constants ;
17+ import com .blogspot .e_kanivets .moneytracker .report .ReportConverter ;
18+ import com .blogspot .e_kanivets .moneytracker .report .ReportMaker ;
19+ import com .blogspot .e_kanivets .moneytracker .report .base .IReport ;
20+ import com .blogspot .e_kanivets .moneytracker .ui .TotalReportViewCreator ;
2921
30- import java .util .ArrayList ;
31- import java .util .HashMap ;
3222import java .util .List ;
33- import java .util .Map ;
3423
3524import butterknife .Bind ;
3625
@@ -39,17 +28,11 @@ public class ReportActivity extends BaseActivity {
3928 private static final String TAG = "ReportActivity" ;
4029
4130 public static final String KEY_PERIOD = "key_period" ;
31+ public static final String KEY_RECORD_LIST = "key_record_list" ;
4232
43- private Report report ;
44- private String currency ;
45-
46- @ Bind (R .id .list_view )
47- ListView listView ;
4833 @ Bind (R .id .exp_list_view )
4934 ExpandableListView expandableListView ;
50-
51- private RecordController recordController ;
52- private Period period ;
35+ private IReport report ;
5336
5437 @ Override
5538 protected int getContentViewId () {
@@ -60,129 +43,62 @@ protected int getContentViewId() {
6043 protected boolean initData () {
6144 super .initData ();
6245
46+ List <Record > recordList = getIntent ().getParcelableArrayListExtra (KEY_RECORD_LIST );
47+ if (recordList == null ) return false ;
48+
49+ Period period = getIntent ().getParcelableExtra (KEY_PERIOD );
50+ if (period == null ) return false ;
51+
6352 DbHelper dbHelper = new DbHelper (ReportActivity .this );
64- IRepo <Category > categoryRepo = new CategoryRepo (dbHelper );
65- CategoryController categoryController = new CategoryController (categoryRepo );
6653 AccountController accountController = new AccountController (new AccountRepo (dbHelper ));
6754 ExchangeRateController rateController = new ExchangeRateController (new ExchangeRateRepo (dbHelper ));
6855
69- recordController = new RecordController (new RecordRepo (dbHelper ), categoryController ,
70- accountController );
71-
72- currency = DbHelper .DEFAULT_ACCOUNT_CURRENCY ;
56+ String currency = DbHelper .DEFAULT_ACCOUNT_CURRENCY ;
7357 if (accountController .readAll ().size () > 0 )
7458 currency = accountController .readAll ().get (0 ).getCurrency ();
7559
76- period = getIntent ().getParcelableExtra (KEY_PERIOD );
77- report = new Report (recordController .getRecordsForPeriod (period ), currency , rateController );
60+ ReportMaker reportMaker = new ReportMaker (rateController );
61+ report = reportMaker .getReport (currency , period , recordList );
62+
63+ if (report == null ) {
64+ List <String > ratesNeeded = reportMaker .currencyNeeded (currency , recordList );
65+ showExchangeRatesNeededDialog (currency , ratesNeeded );
66+ }
7867
79- return period != null ;
68+ return true ;
8069 }
8170
8271 @ Override
8372 protected void initViews () {
8473 super .initViews ();
8574
86- listView .setAdapter (new ReportItemAdapter (ReportActivity .this , report .getReportList ()));
75+ if (report == null ) return ;
76+ ReportConverter reportConverter = new ReportConverter (report );
8777
88- /* Scroll list to bottom only once at start */
89- listView .getViewTreeObserver ().addOnGlobalLayoutListener (new ViewTreeObserver .OnGlobalLayoutListener () {
90- private boolean isFirst = true ;
91-
92- @ Override
93- public void onGlobalLayout () {
94- if (isFirst ) {
95- isFirst = false ;
96- listView .setSelection (listView .getCount () - 1 );
97- }
98- }
99- });
100-
101- initExpandableListView ();
78+ expandableListView .addFooterView (new TotalReportViewCreator (ReportActivity .this , report ).create ());
79+ expandableListView .setAdapter (new ExpandableListReportAdapter (ReportActivity .this , reportConverter ));
10280 }
10381
104- private void initExpandableListView () {
105- /* List for groups */
106- List <Map <String , String >> groupData ;
107-
108- /* List for child items */
109- List <Map <String , String >> childDataItem ;
82+ private void showExchangeRatesNeededDialog (String currency , List <String > ratesNeeded ) {
83+ AlertDialog .Builder builder = new AlertDialog .Builder (ReportActivity .this );
84+ builder .setTitle (getString (R .string .cant_make_report ));
11085
111- /* List for childDataItems */
112- List <List <Map <String , String >>> childData ;
113-
114- /* Buffer map for names of values */
115- Map <String , String > m ;
116-
117- /* Fill the group list */
118- groupData = new ArrayList <>();
119- for (Pair <String , Integer > item : report .getReportList ()) {
120- /* Fill up attribute names for each group */
121- m = new HashMap <>();
122- m .put (Constants .TITLE_PARAM_NAME , item .first );
123- m .put (Constants .PRICE_PARAM_NAME , Integer .toString (item .second ));
124-
125- groupData .add (m );
86+ StringBuilder sb = new StringBuilder (getString (R .string .rates_needed ));
87+ for (String str : ratesNeeded ) {
88+ sb .append ("\n " ).append (str ).append (getString (R .string .arrow )).append (currency );
12689 }
12790
128- /* List of attributes of groups for reading */
129- String groupFrom [] = new String []{Constants .TITLE_PARAM_NAME , Constants .PRICE_PARAM_NAME };
130- /* List of view IDs for information insertion */
131- int groupTo [] = new int []{R .id .tv_category , R .id .tv_total };
132-
133- /* Create list for childDataItems */
134- childData = new ArrayList <>();
135-
136- for (Map <String , String > group : groupData ) {
137- childDataItem = new ArrayList <>();
138- /* Fill up attribute names for each child item */
139- for (Record record : report .getSummaryRecordList ()) {
140- if (record .getCategory ().equals (group .get (Constants .TITLE_PARAM_NAME ))) {
141- int price = record .getPrice ();
142- if (!record .isIncome ()) {
143- price *= -1 ;
144- }
145-
146- m = new HashMap <>();
147- m .put (Constants .TITLE_PARAM_NAME , record .getTitle ());
148- m .put (Constants .PRICE_PARAM_NAME , Integer .toString (price ));
149-
150- childDataItem .add (m );
151- }
152- }
91+ builder .setMessage (sb .toString ());
15392
154- /* Add childDataItem to common childData */
155- childData .add (childDataItem );
156- }
93+ builder .setPositiveButton (android .R .string .ok , null );
15794
158- /* List of attributes of childItems for reading */
159- String childFrom [] = new String []{Constants .TITLE_PARAM_NAME , Constants .PRICE_PARAM_NAME };
160- /* List of view IDs for information insertion */
161- int childTo [] = new int []{R .id .tv_category , R .id .tv_total };
162-
163- expandableListView .addFooterView (getSummaryReportView (report .getSummaryReportList ()));
164- expandableListView .setAdapter (new ExpandableListReportAdapter (
165- ReportActivity .this ,
166- groupData ,
167- R .layout .view_report_item_exp ,
168- groupFrom ,
169- groupTo ,
170- childData ,
171- R .layout .view_report_item ,
172- childFrom ,
173- childTo ) {
95+ builder .setOnDismissListener (new DialogInterface .OnDismissListener () {
96+ @ Override
97+ public void onDismiss (DialogInterface dialog ) {
98+ finish ();
99+ }
174100 });
175- }
176-
177- private View getSummaryReportView (List <Pair <String , Integer >> summaryReportList ) {
178- ViewGroup viewGroup = (ViewGroup ) getLayoutInflater ().inflate (R .layout .view_summary_report , null );
179-
180- ReportItemAdapter adapter = new ReportItemAdapter (ReportActivity .this , summaryReportList );
181-
182- for (int i = 0 ; i < adapter .getCount (); i ++) {
183- viewGroup .addView (adapter .getView (i , null , null ));
184- }
185101
186- return viewGroup ;
102+ builder . show () ;
187103 }
188104}
0 commit comments