<commit_before>package com.afollestad.silk.fragments;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.afollestad.silk.R;
import com.afollestad.silk.TimeUtils;
import com.afollestad.silk.cache.SilkComparable;

import java.io.File;
import java.util.Calendar;

/**
 * A {@link com.afollestad.silk.fragments.SilkCachedFeedFragment} that allows you to show a frame at the top of the list,
 * indicating the last time the fragment refreshed, and allowing the user to invoke a new refresh by pressing a button.
 *
 * @author <NAME> (afollestad)
 */
public abstract class SilkLastUpdatedFragment<T extends SilkComparable> extends SilkCachedFeedFragment<T> {

    /**
     * Initializes a new SilkLastUpdatedFragment.
     *
     * @param cacheTitle The title to use for the Fragment's {@link com.afollestad.silk.cache.SilkCacheManager}.
     */
    public SilkLastUpdatedFragment(String cacheTitle) {
        super(cacheTitle);
    }

    /**
     * Initializes a new SilkLastUpdatedFragment.
     *
     * @param cacheTitle     The title to use for the Fragment's {@link com.afollestad.silk.cache.SilkCacheManager}.
     * @param cacheDirectory The directory set to the Fragment's {@link com.afollestad.silk.cache.SilkCacheManager}, will be '/sdcard/Silk' by default.
     */
    public SilkLastUpdatedFragment(String cacheTitle, File cacheDirectory) {
        super(cacheTitle, cacheDirectory);
    }

    private TextView mLastUpdateLabel;
    private ImageView mLastUpdateAction;

    private SharedPreferences getPrefs() {
        return getActivity().getSharedPreferences("feed_last_update", 0);
    }

    /**
     * Sets whether or not the last updated frame is visible.
     */
    public final void setLastUpdatedVisibile(boolean visible) {
        View v = getView();
        if (v == null) return;
        v.findViewById(R.id.lastUpdatedFrame).setVisibility(visible ? View.VISIBLE : View.GONE);
        v.findViewById(R.id.divider).setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * Gets the last time the fragment was updated (did a full refresh from the web).
     */
    public final Calendar getLastUpdatedTime() {
        SharedPreferences prefs = getPrefs();
        if (prefs.contains(mCacheTitle)) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(prefs.getLong(mCacheTitle, 0));
            return cal;
        }
        return null;
    }

    /**
     * Gets whether or not right now is a good time to show the last updated frame. Can be overridden to change behavior.
     */
    public boolean getShouldShowLastUpdated() {
        Calendar last = getLastUpdatedTime();
        if (last == null) return false;
        else if (getAdapter().getCount() == 0) return true;
        Calendar now = Calendar.getInstance();
        double lastHours = last.getTimeInMillis() / (1000 * 60 * 60);
        double nowHours = now.getTimeInMillis() / (1000 * 60 * 60);
        // An hour or more difference in last refresh time will return true
        return (nowHours - lastHours) >= 1;
    }

    /**
     * Invalidates the last updated frame. Decides whether or not the last updated frame should be visible based on
     * the return value of {#getShouldShowLastUpdated}, and updates the label if it's shown.
     */
    public final boolean invalidateLastUpdated() {
        boolean shouldShow = getShouldShowLastUpdated();
        setLastUpdatedVisibile(shouldShow);
        if (shouldShow) {
            mLastUpdateLabel.setText(getString(R.string.last_updated).replace("{date}",
                    TimeUtils.getFriendlyTimeLong(getLastUpdatedTime(), false)));
        }
        return shouldShow;
    }


    @Override
    public int getLayout() {
        return R.layout.fragment_list_lastupdated;
    }
<commit_msg>Changed names of TimeUtils methods and added customizable options.<commit_after>package com.afollestad.silk.fragments;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.afollestad.silk.R;
import com.afollestad.silk.TimeUtils;
import com.afollestad.silk.cache.SilkComparable;

import java.io.File;
import java.util.Calendar;

/**
 * A {@link com.afollestad.silk.fragments.SilkCachedFeedFragment} that allows you to show a frame at the top of the list,
 * indicating the last time the fragment refreshed, and allowing the user to invoke a new refresh by pressing a button.
 *
 * @author <NAME> (afollestad)
 */
public abstract class SilkLastUpdatedFragment<T extends SilkComparable> extends SilkCachedFeedFragment<T> {

    /**
     * Initializes a new SilkLastUpdatedFragment.
     *
     * @param cacheTitle The title to use for the Fragment's {@link com.afollestad.silk.cache.SilkCacheManager}.
     */
    public SilkLastUpdatedFragment(String cacheTitle) {
        super(cacheTitle);
    }

    /**
     * Initializes a new SilkLastUpdatedFragment.
     *
     * @param cacheTitle     The title to use for the Fragment's {@link com.afollestad.silk.cache.SilkCacheManager}.
     * @param cacheDirectory The directory set to the Fragment's {@link com.afollestad.silk.cache.SilkCacheManager}, will be '/sdcard/Silk' by default.
     */
    public SilkLastUpdatedFragment(String cacheTitle, File cacheDirectory) {
        super(cacheTitle, cacheDirectory);
    }

    private TextView mLastUpdateLabel;
    private ImageView mLastUpdateAction;

    private SharedPreferences getPrefs() {
        return getActivity().getSharedPreferences("feed_last_update", 0);
    }

    /**
     * Sets whether or not the last updated frame is visible.
     */
    public final void setLastUpdatedVisibile(boolean visible) {
        View v = getView();
        if (v == null) return;
        v.findViewById(R.id.lastUpdatedFrame).setVisibility(visible ? View.VISIBLE : View.GONE);
        v.findViewById(R.id.divider).setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * Gets the last time the fragment was updated (did a full refresh from the web).
     */
    public final Calendar getLastUpdatedTime() {
        SharedPreferences prefs = getPrefs();
        if (prefs.contains(mCacheTitle)) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(prefs.getLong(mCacheTitle, 0));
            return cal;
        }
        return null;
    }

    /**
     * Gets whether or not right now is a good time to show the last updated frame. Can be overridden to change behavior.
     */
    public boolean getShouldShowLastUpdated() {
        Calendar last = getLastUpdatedTime();
        if (last == null) return false;
        else if (getAdapter().getCount() == 0) return true;
        Calendar now = Calendar.getInstance();
        double lastHours = last.getTimeInMillis() / (1000 * 60 * 60);
        double nowHours = now.getTimeInMillis() / (1000 * 60 * 60);
        // An hour or more difference in last refresh time will return true
        return (nowHours - lastHours) >= 1;
    }

    /**
     * Invalidates the last updated frame. Decides whether or not the last updated frame should be visible based on
     * the return value of {#getShouldShowLastUpdated}, and updates the label if it's shown.
     */
    public final boolean invalidateLastUpdated() {
        boolean shouldShow = getShouldShowLastUpdated();
        setLastUpdatedVisibile(shouldShow);
        if (shouldShow) {
            mLastUpdateLabel.setText(getString(R.string.last_updated).replace("{date}",
                    TimeUtils.toString(getLastUpdatedTime(), false, true)));
        }
        return shouldShow;
    }


    @Override
    public int getLayout() {
        return R.layout.fragment_list_lastupdated;
    }

{'fraction_non_alphanumeric': 0.09290583186064125, 'fraction_numerical': 0.005049229992426155, 'mean_word_length': 3.0116455696202533, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '531252', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2180, 'n_words': 651}