Search in sources :

Example 1 with StatsCache

use of im.tny.segvault.disturbances.StatsCache in project underlx by underlx.

the class HomeStatsFragment method redraw.

private void redraw(Context context) {
    if (mListener == null || mListener.getMainService() == null) {
        return;
    }
    StatsCache cache = mListener.getMainService().getStatsCache();
    if (cache == null) {
        return;
    }
    StatsCache.Stats stats = cache.getNetworkStats(networkId);
    if (stats == null) {
        return;
    }
    // Calculate days difference like the website
    Calendar today = Calendar.getInstance();
    today.setTime(new Date());
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);
    Calendar lastDisturbance = Calendar.getInstance();
    lastDisturbance.setTime(stats.lastDisturbance);
    lastDisturbance.set(Calendar.HOUR_OF_DAY, 0);
    lastDisturbance.set(Calendar.MINUTE, 0);
    lastDisturbance.set(Calendar.SECOND, 0);
    lastDisturbance.set(Calendar.MILLISECOND, 0);
    SharedPreferences sharedPref = context.getSharedPreferences("settings", MODE_PRIVATE);
    boolean locationEnabled = sharedPref.getBoolean(PreferenceNames.LocationEnable, true);
    if (locationEnabled) {
        if (stats.curOnInTransit == 0) {
            usersOnlineView.setText(R.string.frag_stats_few_users_in_network);
        } else {
            usersOnlineView.setText(String.format(getString(R.string.frag_stats_users_in_network), stats.curOnInTransit));
        }
        usersOnlineView.setVisibility(View.VISIBLE);
    } else {
        usersOnlineView.setVisibility(View.GONE);
    }
    long days = (today.getTime().getTime() - lastDisturbance.getTime().getTime()) / (24 * 60 * 60 * 1000);
    if (days < 2) {
        long hours = (new Date().getTime() - stats.lastDisturbance.getTime()) / (60 * 60 * 1000);
        lastDisturbanceView.setHtml(String.format(getString(R.string.frag_stats_last_disturbance_hours), hours));
    } else {
        lastDisturbanceView.setHtml(String.format(getString(R.string.frag_stats_last_disturbance_days), days));
    }
    Network net = mListener.getMainService().getNetwork(networkId);
    if (net == null) {
        return;
    }
    List<Line> lines = new ArrayList<>(net.getLines());
    Collections.sort(lines, new Comparator<Line>() {

        @Override
        public int compare(Line t0, Line t1) {
            return Integer.valueOf(t0.getOrder()).compareTo(t1.getOrder());
        }
    });
    lineStatsLayout.removeAllViews();
    TableRow header = (TableRow) getActivity().getLayoutInflater().inflate(R.layout.line_stats_header, lineStatsLayout, false);
    lineStatsLayout.addView(header);
    for (Line line : lines) {
        double weekAvail;
        if (stats.weekLineStats.get(line.getId()) == null) {
            continue;
        } else {
            weekAvail = stats.weekLineStats.get(line.getId()).availability;
        }
        double monthAvail;
        if (stats.monthLineStats.get(line.getId()) == null) {
            continue;
        } else {
            monthAvail = stats.monthLineStats.get(line.getId()).availability;
        }
        TableRow row = (TableRow) getActivity().getLayoutInflater().inflate(R.layout.line_stats_row, lineStatsLayout, false);
        TextView lineNameView = (TextView) row.findViewById(R.id.line_name_view);
        lineNameView.setText(Util.getLineNames(getContext(), line)[0]);
        lineNameView.setTextColor(line.getColor());
        ((TextView) row.findViewById(R.id.week_availability_view)).setText(String.format("%.3f%%", weekAvail * 100));
        ((TextView) row.findViewById(R.id.month_availability_view)).setText(String.format("%.3f%%", monthAvail * 100));
        lineStatsLayout.addView(row);
    }
    if (new Date().getTime() - stats.updated.getTime() > java.util.concurrent.TimeUnit.MINUTES.toMillis(5)) {
        lineStatsLayout.setAlpha(0.6f);
        lastDisturbanceView.setAlpha(0.6f);
        usersOnlineView.setAlpha(0.6f);
        updateInformationView.setTypeface(null, Typeface.BOLD);
    } else {
        lineStatsLayout.setAlpha(1f);
        lastDisturbanceView.setAlpha(1f);
        usersOnlineView.setAlpha(1f);
        updateInformationView.setTypeface(null, Typeface.NORMAL);
    }
    updateInformationView.setText(String.format(getString(R.string.frag_stats_updated), DateUtils.getRelativeTimeSpanString(context, stats.updated.getTime(), true)));
}
Also used : SharedPreferences(android.content.SharedPreferences) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Date(java.util.Date) Line(im.tny.segvault.subway.Line) StatsCache(im.tny.segvault.disturbances.StatsCache) Network(im.tny.segvault.subway.Network) TableRow(android.widget.TableRow) HtmlTextView(org.sufficientlysecure.htmltextview.HtmlTextView) TextView(android.widget.TextView)

Aggregations

SharedPreferences (android.content.SharedPreferences)1 TableRow (android.widget.TableRow)1 TextView (android.widget.TextView)1 StatsCache (im.tny.segvault.disturbances.StatsCache)1 Line (im.tny.segvault.subway.Line)1 Network (im.tny.segvault.subway.Network)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HtmlTextView (org.sufficientlysecure.htmltextview.HtmlTextView)1