3636import com .nightonke .saver .ui .MyGridView ;
3737import com .nightonke .saver .util .CoCoinUtil ;
3838import com .nispok .snackbar .Snackbar ;
39+ import com .nispok .snackbar .SnackbarManager ;
40+ import com .nispok .snackbar .enums .SnackbarType ;
3941import com .nispok .snackbar .listeners .ActionClickListener ;
4042import com .squareup .leakcanary .RefWatcher ;
4143
4850import java .util .List ;
4951import java .util .Map ;
5052
53+ import lecho .lib .hellocharts .listener .PieChartOnValueSelectListener ;
5154import lecho .lib .hellocharts .model .Line ;
5255import lecho .lib .hellocharts .model .LineChartData ;
5356import lecho .lib .hellocharts .model .PieChartData ;
5457import lecho .lib .hellocharts .model .PointValue ;
58+ import lecho .lib .hellocharts .model .SelectedValue ;
5559import lecho .lib .hellocharts .model .SliceValue ;
5660import lecho .lib .hellocharts .model .ValueShape ;
5761import lecho .lib .hellocharts .util .ChartUtils ;
@@ -112,6 +116,7 @@ public class ReportViewFragment extends Fragment
112116
113117 private TextView fromDate ;
114118 private TextView expenseTV ;
119+ private boolean isEmpty = false ;
115120 private TextView emptyTip ;
116121 private TextView tagsTV ;
117122
@@ -120,7 +125,6 @@ public class ReportViewFragment extends Fragment
120125
121126 private SuperToast superToast ;
122127
123- private PieChartView pie ;
124128 private LineChartView line ;
125129
126130 private MaterialIconView iconRight ;
@@ -136,11 +140,6 @@ public class ReportViewFragment extends Fragment
136140 // the original target value of the whole pie
137141 private float [] originalTargets ;
138142
139- // the selected position of one part of the pie
140- private int pieSelectedPosition = 0 ;
141- // the last selected position of one part of the pie
142- private int lastPieSelectedPosition = -1 ;
143-
144143 // the date string on the footer and header
145144 private String dateString ;
146145 // the date string shown in the dialog
@@ -155,6 +154,12 @@ public class ReportViewFragment extends Fragment
155154 // year, month(-1 means the whole year), records, expenses
156155 private ArrayList <double []> selectListData = null ;
157156
157+ // pie
158+ private LinearLayout pieLayout ;
159+ private PieChartView pie ;
160+ private int pieSelectedPosition = 0 ; // the selected position of one part of the pie
161+ private int lastPieSelectedPosition = -1 ; // the last selected position of one part of the pie
162+
158163 // highest tag list
159164 private LinearLayout highestTagLayout ;
160165 private LinearLayout highestFirst ;
@@ -236,19 +241,96 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
236241 tagsTV .setTypeface (CoCoinUtil .getInstance ().typefaceLatoLight );
237242 tagsTV .setText ("" );
238243
244+ pieLayout = (LinearLayout )view .findViewById (R .id .pie_layout );
245+ pieLayout .setVisibility (View .GONE );
239246 pie = (PieChartView )view .findViewById (R .id .chart_pie );
240- pie .setVisibility (View .INVISIBLE );
247+ pie .setChartRotationEnabled (false );
248+ pie .setOnValueTouchListener (new PieChartOnValueSelectListener () {
249+ @ Override
250+ public void onValueSelected (int p , SliceValue sliceValue ) {
251+ // snack bar
252+ String text ;
253+ tagId = Integer .valueOf (String .valueOf (sliceValue .getLabelAsChars ()));
254+ double percent = sliceValue .getValue () / expense * 100 ;
255+ if ("zh" .equals (CoCoinUtil .GetLanguage ())) {
256+ text = CoCoinUtil .GetSpendString ((int ) sliceValue .getValue ()) +
257+ CoCoinUtil .GetPercentString (percent ) + "\n " +
258+ "于" + CoCoinUtil .GetTagName (tagId );
259+ } else {
260+ text = CoCoinUtil .GetSpendString ((int ) sliceValue .getValue ())
261+ + " (takes " + String .format ("%.2f" , percent ) + "%)\n "
262+ + "in " + CoCoinUtil .GetTagName (tagId );
263+ }
264+ if ("zh" .equals (CoCoinUtil .GetLanguage ())) {
265+ if (selectYear ) {
266+ dialogTitle = from .get (Calendar .YEAR ) + "年" + "\n " +
267+ CoCoinUtil .GetSpendString ((int ) sliceValue .getValue ()) +
268+ "于" + CoCoinUtil .GetTagName (tagId );
269+ } else {
270+ dialogTitle = from .get (Calendar .YEAR ) + "年" + (from .get (Calendar .MONTH ) + 1 ) + "月" + "\n " +
271+ CoCoinUtil .GetSpendString ((int ) sliceValue .getValue ()) +
272+ "于" + CoCoinUtil .GetTagName (tagId );
273+ }
274+ } else {
275+ if (selectYear ) {
276+ dialogTitle = CoCoinUtil .GetSpendString ((int ) sliceValue .getValue ()) + " in " + from .get (Calendar .YEAR ) + "\n " +
277+ "on " + CoCoinUtil .GetTagName (tagId );
278+ } else {
279+ dialogTitle = CoCoinUtil .GetSpendString ((int ) sliceValue .getValue ()) + " in " + CoCoinUtil .GetMonthShort (from .get (Calendar .MONTH ) + 1 ) + " " + from .get (Calendar .YEAR ) + "\n " +
280+ "on " + CoCoinUtil .GetTagName (tagId );
281+ }
282+ }
283+ Snackbar snackbar =
284+ Snackbar
285+ .with (mContext )
286+ .type (SnackbarType .MULTI_LINE )
287+ .duration (Snackbar .SnackbarDuration .LENGTH_SHORT )
288+ .position (Snackbar .SnackbarPosition .BOTTOM )
289+ .margin (15 , 15 )
290+ .backgroundDrawable (CoCoinUtil .GetSnackBarBackground (-3 ))
291+ .text (text )
292+ .textTypeface (CoCoinUtil .GetTypeface ())
293+ .textColor (Color .WHITE )
294+ .actionLabelTypeface (CoCoinUtil .GetTypeface ())
295+ .actionLabel (mContext .getResources ()
296+ .getString (R .string .check ))
297+ .actionColor (Color .WHITE )
298+ .actionListener (new ActionClickListener () {
299+ @ Override
300+ public void onActionClicked (Snackbar snackbar ) {
301+ new GetData (from , to , tagId , dialogTitle ).execute ();
302+ }
303+ });
304+ SnackbarManager .show (snackbar );
305+
306+ if (p == lastPieSelectedPosition ) {
307+ return ;
308+ } else {
309+ lastPieSelectedPosition = p ;
310+ }
311+ }
241312
313+ @ Override
314+ public void onValueDeselected () {
315+
316+ }
317+ });
242318 iconRight = (MaterialIconView )view .findViewById (R .id .icon_right );
319+ iconRight .setOnClickListener (this );
243320 iconLeft = (MaterialIconView )view .findViewById (R .id .icon_left );
244- iconRight .setVisibility (View .INVISIBLE );
245- iconLeft .setVisibility (View .INVISIBLE );
321+ iconLeft .setOnClickListener (this );
246322
247323// all = (MaterialIconView)view.findViewById(R.id.all);
248324// all.setVisibility(View.INVISIBLE);
249325
250326 emptyTip = (TextView )view .findViewById (R .id .empty_tip );
251327 emptyTip .setTypeface (CoCoinUtil .GetTypeface ());
328+ if (RecordManager .getInstance (CoCoinApplication .getAppContext ()).RECORDS .size () != 0 ) {
329+ emptyTip .setText (mContext .getResources ().getString (R .string .report_view_please_select_data ));
330+ } else {
331+ emptyTip .setText (mContext .getResources ().getString (R .string .report_view_no_data ));
332+ isEmpty = true ;
333+ }
252334
253335 highestTagLayout = (LinearLayout )view .findViewById (R .id .highest_tag_layout );
254336 highestTagLayout .setVisibility (View .GONE );
@@ -325,7 +407,36 @@ Pair<Boolean, SublimeOptions> getOptions() {
325407
326408 @ Override
327409 public void onClick (View v ) {
410+ SelectedValue selectedValue = null ;
328411 switch (v .getId ()) {
412+ case R .id .icon_right :
413+ if (lastPieSelectedPosition != -1 ) {
414+ pieSelectedPosition = lastPieSelectedPosition ;
415+ }
416+ pieSelectedPosition
417+ = (pieSelectedPosition - 1 + pieChartData .getValues ().size ())
418+ % pieChartData .getValues ().size ();
419+ selectedValue =
420+ new SelectedValue (
421+ pieSelectedPosition ,
422+ 0 ,
423+ SelectedValue .SelectedValueType .NONE );
424+ pie .selectValue (selectedValue );
425+ break ;
426+ case R .id .icon_left :
427+ if (lastPieSelectedPosition != -1 ) {
428+ pieSelectedPosition = lastPieSelectedPosition ;
429+ }
430+ pieSelectedPosition
431+ = (pieSelectedPosition + 1 )
432+ % pieChartData .getValues ().size ();
433+ selectedValue =
434+ new SelectedValue (
435+ pieSelectedPosition ,
436+ 0 ,
437+ SelectedValue .SelectedValueType .NONE );
438+ pie .selectValue (selectedValue );
439+ break ;
329440 case R .id .highest_first :
330441 onItemClick (highestTags , highestTags .getChildAt (0 ), -1 , -1 );
331442 break ;
@@ -352,7 +463,7 @@ public void onClick(View v) {
352463 }
353464 break ;
354465 case R .id .button :
355- showSelectListDataDialog ();
466+ if (! isEmpty ) showSelectListDataDialog ();
356467 break ;
357468 }
358469 }
@@ -391,18 +502,6 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
391502 }
392503 }
393504
394- private class mActionClickListenerForPie implements ActionClickListener {
395- @ Override
396- public void onActionClicked (Snackbar snackbar ) {
397- List <CoCoinRecord > shownCoCoinRecords = Expanse .get (tagId );
398- ((FragmentActivity )mContext ).getSupportFragmentManager ()
399- .beginTransaction ()
400- .add (new RecordCheckDialogFragment (
401- mContext , shownCoCoinRecords , dialogTitle ), "MyDialog" )
402- .commit ();
403- }
404- }
405-
406505 // get select list for dialog
407506 private MaterialDialog progressDialog ;
408507 public class GetSelectListData extends AsyncTask <String , Void , String > {
@@ -643,10 +742,10 @@ public int compare(double[] lhs, double[] rhs) {
643742 });
644743 // use tag expense values to generate pie data
645744 ArrayList <SliceValue > sliceValues = new ArrayList <>();
646- for (int i = 0 ; i < highestTagExpense .size (); i ++) {
745+ for (int i = 0 ; i < lowestTagExpense .size (); i ++) {
647746 SliceValue sliceValue = new SliceValue (
648- (float )(double )highestTagExpense .get (i )[0 ], CoCoinUtil .GetTagColor ((int )highestTagExpense .get (i )[2 ]));
649- sliceValue .setLabel (String .valueOf ((int )highestTagExpense .get (i )[2 ]));
747+ (float )(double )lowestTagExpense .get (i )[0 ], CoCoinUtil .GetTagColor ((int )lowestTagExpense .get (i )[2 ]));
748+ sliceValue .setLabel (String .valueOf ((int )lowestTagExpense .get (i )[2 ]));
650749 sliceValues .add (sliceValue );
651750 }
652751 pieChartData = new PieChartData (sliceValues );
@@ -811,6 +910,12 @@ protected void onPostExecute(String result) {
811910 // for basic information
812911 expenseTV .setText (CoCoinUtil .getInstance ().GetInMoney ((int )expense ));
813912 tagsTV .setText (records + CoCoinApplication .getAppContext ().getResources ().getString (R .string .report_view_records ) + tags + CoCoinApplication .getAppContext ().getResources ().getString (R .string .report_view_tags ));
913+ emptyTip .setVisibility (View .GONE );
914+
915+ // for pie
916+ pieLayout .setVisibility (View .VISIBLE );
917+ pie .setVisibility (View .VISIBLE );
918+ pie .setPieChartData (pieChartData );
814919
815920 // for highest tag expense
816921 highestTagLayout .setVisibility (View .VISIBLE );
0 commit comments