From dd5e514dd65d1e4a753954deacf10ad78767444c Mon Sep 17 00:00:00 2001 From: PaulStets Date: Tue, 19 Dec 2017 19:00:53 +0200 Subject: [PATCH] Refactoring --- .../plus/liveupdates/ReportsFragment.java | 16 ++--- .../plus/liveupdates/UsersReportFragment.java | 71 ++++++++----------- 2 files changed, 35 insertions(+), 52 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/ReportsFragment.java b/OsmAnd/src/net/osmand/plus/liveupdates/ReportsFragment.java index 5207d72332..20373f42b6 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/ReportsFragment.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/ReportsFragment.java @@ -131,17 +131,11 @@ public class ReportsFragment extends BaseOsmAndFragment implements CountrySelect String countryUrlString = selectedCountryItem.getDownloadName(); if (countryUrlString.length() > 0) { Bundle bl = new Bundle(); - if (v.getId() == R.id.numberOfRecipientsLayout) { - bl.putString(UsersReportFragment.URL_REQUEST, - String.format(RECIPIENTS_BY_MONTH, monthUrlString, countryUrlString)); - userReportFragment.setArguments(bl); - userReportFragment.show(getChildFragmentManager(), RECIPIENTS_FRAGMENT); - } else { - bl.putString(UsersReportFragment.URL_REQUEST, - String.format(USERS_RANKING_BY_MONTH, monthUrlString, countryUrlString)); - userReportFragment.setArguments(bl); - userReportFragment.show(getChildFragmentManager(), EDITS_FRAGMENT); - } + boolean is = v.getId() == R.id.numberOfRecipientsLayout; + bl.putString(UsersReportFragment.URL_REQUEST, + String.format(is ? RECIPIENTS_BY_MONTH : USERS_RANKING_BY_MONTH, monthUrlString, countryUrlString)); + userReportFragment.setArguments(bl); + userReportFragment.show(getChildFragmentManager(), is ? RECIPIENTS_FRAGMENT : EDITS_FRAGMENT); } } }; diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/UsersReportFragment.java b/OsmAnd/src/net/osmand/plus/liveupdates/UsersReportFragment.java index e78388b44a..9d03c3ccf8 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/UsersReportFragment.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/UsersReportFragment.java @@ -35,7 +35,7 @@ public class UsersReportFragment extends BaseOsmAndDialogFragment { //String reg = getArguments().getString(REGION_NAME); view.findViewById(R.id.progress).setVisibility(View.VISIBLE); if (getTag().equals(ReportsFragment.EDITS_FRAGMENT)) { - ((TextView)view.findViewById(R.id.titleTextView)).setText(R.string.osm_editors_ranking); + ((TextView) view.findViewById(R.id.titleTextView)).setText(R.string.osm_editors_ranking); GetJsonAsyncTask task = new GetJsonAsyncTask<>(RankingUserByMonthResponse.class); task.setOnResponseListener(new OnResponseListener() { @@ -43,14 +43,15 @@ public class UsersReportFragment extends BaseOsmAndDialogFragment { public void onResponse(RankingUserByMonthResponse response) { if (response != null && response.rows != null) { for (UserRankingByMonth rankingByMonth : response.rows) { - adapter.add(rankingByMonth); + if (rankingByMonth != null) { + adapter.add(rankingByMonth); + } } } view.findViewById(R.id.progress).setVisibility(View.GONE); } }); task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url); - listView.setAdapter(adapter); } else if (getTag().equals(ReportsFragment.RECIPIENTS_FRAGMENT)) { ((TextView)view.findViewById(R.id.titleTextView)).setText(R.string.osm_recipients_label); GetJsonAsyncTask task = new GetJsonAsyncTask<>(Protocol.RecipientsByMonth.class); @@ -60,15 +61,17 @@ public class UsersReportFragment extends BaseOsmAndDialogFragment { public void onResponse(Protocol.RecipientsByMonth response) { if (response != null && response.rows != null) { for (Protocol.Recipient recipient : response.rows) { - adapter.add(recipient); + if (recipient != null) { + adapter.add(recipient); + } } } view.findViewById(R.id.progress).setVisibility(View.GONE); } }); task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url); - listView.setAdapter(adapter); } + listView.setAdapter(adapter); ImageButton clearButton = (ImageButton) view.findViewById(R.id.closeButton); //setThemedDrawable(clearButton, R.drawable.ic_action_remove_dark); clearButton.setOnClickListener(new View.OnClickListener() { @@ -109,43 +112,29 @@ public class UsersReportFragment extends BaseOsmAndDialogFragment { } @Override - public View getView(int position, View convertView, ViewGroup parent) { - if (getItem(position) instanceof UserRankingByMonth) { - UserRankingByMonth item = (UserRankingByMonth) getItem(position); - View v = convertView; - if (v == null) { - LayoutInflater inflater = getActivity().getLayoutInflater(); - v = inflater.inflate(android.R.layout.simple_list_item_2, parent, false); - } - TextView text1 = (TextView) v.findViewById(android.R.id.text1); - TextView text2 = (TextView) v.findViewById(android.R.id.text2); - text1.setText(item.user); - text2.setText(getString(R.string.osm_user_stat, - String.valueOf(item.changes), String.valueOf(item.rank), String.valueOf(item.globalchanges))); - text1.setTextColor(textColor); - text2.setTextColor(textSecondaryColor); - text1.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null); - text1.setCompoundDrawablePadding(getResources().getDimensionPixelSize(R.dimen.list_content_padding)); - return v; - } else { - Protocol.Recipient item = (Protocol.Recipient) getItem(position); - View v = convertView; - if (v == null) { - LayoutInflater inflater = getActivity().getLayoutInflater(); - v = inflater.inflate(android.R.layout.simple_list_item_2, parent, false); - } - TextView text1 = (TextView) v.findViewById(android.R.id.text1); - TextView text2 = (TextView) v.findViewById(android.R.id.text2); - text1.setText(item.osmid); - text2.setText(getString(R.string.osm_recipient_stat, - String.valueOf(item.changes), String.format("%.4f", (item.btc*1000f)))); - text1.setTextColor(textColor); - text2.setTextColor(textSecondaryColor); - text1.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null); - text1.setCompoundDrawablePadding(getResources().getDimensionPixelSize(R.dimen.list_content_padding)); - return v; + public View getView(int position, View v, ViewGroup parent) { + if (v == null) { + v = getActivity().getLayoutInflater().inflate(android.R.layout.simple_list_item_2, parent, false); } - + TextView text1 = (TextView) v.findViewById(android.R.id.text1); + TextView text2 = (TextView) v.findViewById(android.R.id.text2); + text1.setTextColor(textColor); + text2.setTextColor(textSecondaryColor); + text1.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null); + text1.setCompoundDrawablePadding(getResources().getDimensionPixelSize(R.dimen.list_content_padding)); + Object item = getItem(position); + if (item instanceof UserRankingByMonth) { + UserRankingByMonth rankingByMonth = (UserRankingByMonth) item; + text1.setText(rankingByMonth.user); + text2.setText(getString(R.string.osm_user_stat, + String.valueOf(rankingByMonth.changes), String.valueOf(rankingByMonth.rank), String.valueOf(rankingByMonth.globalchanges))); + } else if (item instanceof Protocol.Recipient){ + Protocol.Recipient recipient = (Protocol.Recipient) item; + text1.setText(recipient.osmid); + text2.setText(getString(R.string.osm_recipient_stat, + String.valueOf(recipient.changes), String.format("%.4f", (recipient.btc*1000f)))); + } + return v; } } }