<commit_before>package net.mabako.steamgifts.fragments;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import net.mabako.steamgifts.activities.WebViewActivity;
import net.mabako.steamgifts.adapters.GiveawayAdapter;
import net.mabako.steamgifts.adapters.IEndlessAdaptable;
import net.mabako.steamgifts.core.R;
import net.mabako.steamgifts.data.User;
import net.mabako.steamgifts.fragments.interfaces.IUserNotifications;
import net.mabako.steamgifts.tasks.LoadUserDetailsTask;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;

import jp.wasabeef.picasso.transformations.RoundedCornersTransformation;

public class UserDetailFragment extends Fragment implements IUserNotifications {
    private static final String TAG = UserDetailFragment.class.getSimpleName();
    public static final String ARG_USER = "user";
    private static final String SAVED_USER = "user";

    private User user;

    private CustomPagerAdapter viewPagerAdapter;
    private ViewPager viewPager;
    private TabLayout tabLayout;

    public static UserDetailFragment newInstance(String userName) {
        UserDetailFragment fragment = new UserDetailFragment();

        Bundle args = new Bundle();
        args.putSerializable(SAVED_USER, new User(userName));
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            user = (User) getArguments().getSerializable(SAVED_USER);
        } else {
            user = (User) savedInstanceState.getSerializable(SAVED_USER);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(SAVED_USER, user);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_user, container, false);

        ActionBar toolbar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (toolbar != null)
            toolbar.setTitle(getNonConfusingUsername());

        UserGiveawayListFragment fragmentSent = UserGiveawayListFragment.newInstance(user, "", true);
        fragmentSent.setiUserNotification(UserDetailFragment.this);

        UserGiveawayListFragment fragmentWon = UserGiveawayListFragment.newInstance(user, "/giveaways/won", false);
        fragmentWon.setiUserNotification(UserDetailFragment.this);

        viewPager = (ViewPager) layout.findViewById(R.id.viewPager);
        viewPagerAdapter = new CustomPagerAdapter((AppCompatActivity) getActivity(), viewPager, fragmentSent, fragmentWon);
        viewPager.setAdapter(viewPagerAdapter);

        tabLayout = (TabLayout) layout.findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);

        setHasOptionsMenu(true);

        return layout;
    }

    @Override
    public void onUserUpdated(User user) {
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if (activity != null) {
            ActionBar actionBar = activity.getSupportActionBar();
            if (actionBar != null) {
                actionBar.setTitle(getNonConfusingUsername());
                actionBar.setSubtitle(getString(R.string.user_level, user.getLevel()));
            }
        }

        // Avatar?
        Picasso.with(getContext()).load(user.getAvatar()).placeholder(R.drawable.default_avatar_mask).transform(new RoundedCornersTransformation(20, 0)).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                AppCompatActivity activity = (AppCompatActivity) getActivity();
                if (activity != null) {
                    ActionBar actionBar = activity.getSupportActionBar();
                    if (actionBar != null) {
                        Drawable drawable = new BitmapDrawable(getResources(), bitmap);
                        actionBar.setIcon(drawable);
                        actionBar.setHomeButtonEnabled(false);
                        actionBar.setDisplayShowHomeEnabled(true);
                        actionBar.setDisplayHomeAsUpEnabled(false);
                    }
                }
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });

        // Refresh tabs
        for (int i = 0; i < viewPagerAdapter.getCount(); ++i)
            tabLayout.getTabAt(i).setText(viewPagerAdapter.getPageTitle(i));
    }

    /**
     * To prevent some confusion, we'll convert names with -some- characters to lowercase as well.
     * This helps in particular if you're called MuIIins, which is actually muiiins and not mullins.
     *
     * @return Lowercase and normal if the user name contains any of the blacklisted characters;<commit_msg>tweaks to avatar display in user profiles<commit_after>package net.mabako.steamgifts.fragments;

import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import net.mabako.steamgifts.activities.WebViewActivity;
import net.mabako.steamgifts.adapters.GiveawayAdapter;
import net.mabako.steamgifts.adapters.IEndlessAdaptable;
import net.mabako.steamgifts.core.R;
import net.mabako.steamgifts.data.User;
import net.mabako.steamgifts.fragments.interfaces.IUserNotifications;
import net.mabako.steamgifts.tasks.LoadUserDetailsTask;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;

import jp.wasabeef.picasso.transformations.RoundedCornersTransformation;

public class UserDetailFragment extends Fragment implements IUserNotifications {
    private static final String TAG = UserDetailFragment.class.getSimpleName();
    public static final String ARG_USER = "user";
    private static final String SAVED_USER = "user";

    private User user;

    private CustomPagerAdapter viewPagerAdapter;
    private ViewPager viewPager;
    private TabLayout tabLayout;

    public static UserDetailFragment newInstance(String userName) {
        UserDetailFragment fragment = new UserDetailFragment();

        Bundle args = new Bundle();
        args.putSerializable(SAVED_USER, new User(userName));
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            user = (User) getArguments().getSerializable(SAVED_USER);
        } else {
            user = (User) savedInstanceState.getSerializable(SAVED_USER);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(SAVED_USER, user);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_user, container, false);

        ActionBar toolbar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (toolbar != null)
            toolbar.setTitle(getNonConfusingUsername());

        UserGiveawayListFragment fragmentSent = UserGiveawayListFragment.newInstance(user, "", true);
        fragmentSent.setiUserNotification(UserDetailFragment.this);

        UserGiveawayListFragment fragmentWon = UserGiveawayListFragment.newInstance(user, "/giveaways/won", false);
        fragmentWon.setiUserNotification(UserDetailFragment.this);

        viewPager = (ViewPager) layout.findViewById(R.id.viewPager);
        viewPagerAdapter = new CustomPagerAdapter((AppCompatActivity) getActivity(), viewPager, fragmentSent, fragmentWon);
        viewPager.setAdapter(viewPagerAdapter);

        tabLayout = (TabLayout) layout.findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);

        setHasOptionsMenu(true);

        return layout;
    }

    @Override
    public void onUserUpdated(User user) {
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if (activity != null) {
            ActionBar actionBar = activity.getSupportActionBar();
            if (actionBar != null) {
                actionBar.setTitle(getNonConfusingUsername());
                actionBar.setSubtitle(getString(R.string.user_level, user.getLevel()));
            }
        }

        // Rescale the avatar to not take up the full navbar height.
        int attrs[] = new int[]{R.attr.actionBarSize};
        TypedArray ta = getContext().getTheme().obtainStyledAttributes(attrs);
        int size = (int) (ta.getDimensionPixelSize(0, 0) * 0.75f);

        Picasso.with(getContext()).load(user.getAvatar()).placeholder(R.drawable.default_avatar_mask).resize(size, size).transform(new RoundedCornersTransformation(20, 0)).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                AppCompatActivity activity = (AppCompatActivity) getActivity();
                if (activity != null) {
                    ActionBar actionBar = activity.getSupportActionBar();
                    if (actionBar != null) {
                        Drawable drawable = new BitmapDrawable(getResources(), bitmap);
                        actionBar.setDisplayUseLogoEnabled(true);
                        actionBar.setDisplayShowHomeEnabled(true);
                        actionBar.setIcon(drawable);
                    }
                }
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });

        // Refresh tabs
        for (int i = 0; i < viewPagerAdapter.getCount(); ++i)
            tabLayout.getTabAt(i).setText(viewPagerAdapter.getPageTitle(i));
    }

    /**
     * To prevent some confusion, we'll convert names with -some- characters to lowercase as well.
     * This helps in particular if you're called MuIIins, which is actually muiiins and not mullins.
     *
     * @return Lowercase and normal if the user name contains any of the blacklisted characters;
{'fraction_non_alphanumeric': 0.08979998340111213, 'fraction_numerical': 0.0017428832268238028, 'mean_word_length': 3.2029996512033483, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6037458', 'n_tokens_mistral': 3338, 'n_tokens_neox': 3194, 'n_words': 672}