package com.frozendevs.cache.cleaner.model.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.frozendevs.cache.cleaner.model.AppsListItem;
import com.frozendevs.cache.cleaner.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class AppsListAdapter extends BaseAdapter {

    public static final int SORT_BY_APP_NAME = 0;
    public static final int SORT_BY_CACHE_SIZE = 1;

    private List<AppsListItem> mItems, mFilteredItems;
    private Context mContext;
    private SharedPreferences mSharedPreferences;

    public AppsListAdapter(Context context, SharedPreferences sharedPreferences) {
        mContext = context;
        mSharedPreferences = sharedPreferences;

        mItems = new ArrayList<AppsListItem>();
        mFilteredItems = new ArrayList<AppsListItem>(mItems);
    }

    @Override
    public int getCount() {
        return mFilteredItems.size();
    }

    @Override
    public Object getItem(int i) {
        return mFilteredItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewParent) {
        final AppsListItem item = mFilteredItems.get(i);

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, viewParent, false);
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + item.getPackageName()));

                mContext.startActivity(intent);
            }
        });

        ImageView imageView = (ImageView) convertView.findViewById(R.id.app_icon);
        imageView.setImageDrawable(item.getApplicationIcon());

        TextView nameView = (TextView) convertView.findViewById(R.id.app_name);
        nameView.setText(item.getApplicationName());

        TextView sizeView = (TextView) convertView.findViewById(R.id.app_size);
        sizeView.setText(Formatter.formatShortFileSize(mContext, item.getCacheSize()));

        return convertView;
    }

    public void setItems(List<AppsListItem> items) {
        mItems = new ArrayList<AppsListItem>(items);

        sort();
    }

    public void filterAppsByName(String filter) {
        List<AppsListItem> filteredItems = new ArrayList<AppsListItem>();

        Locale current = mContext.getResources().getConfiguration().locale;

        for (AppsListItem item : mItems) {
            if (item.getApplicationName().toLowerCase(current).contains(filter.toLowerCase(current)))
                filteredItems.add(item);
        }

        mFilteredItems = filteredItems;

        notifyDataSetChanged();
    }

    public void clearFilter() {
        mFilteredItems = new ArrayList<AppsListItem>(mItems);

        notifyDataSetChanged();
    }

    public long getTotalCacheSize() {
        long size = 0;

        for (AppsListItem app : mItems)
            size += app.getCacheSize();

        return size;
    }

    public void sort() {
        Collections.sort(mItems, new Comparator<AppsListItem>() {
            @Override
            public int compare(AppsListItem lhs, AppsListItem rhs) {
                switch (mSharedPreferences.getInt(mContext.getString(R.string.sort_by_key),
                        SORT_BY_CACHE_SIZE)) {
                    case SORT_BY_APP_NAME:
                        return lhs.getApplicationName().compareToIgnoreCase(rhs.getApplicationName());

                    case SORT_BY_CACHE_SIZE:
                        return (int) (rhs.getCacheSize() - lhs.getCacheSize());
                }

                return 0;
            }
        });

        mFilteredItems = new ArrayList<AppsListItem>(mItems);
    }
}

@@ -926,24 +926,144 @@
 eferences;%0A%0A
+    private class ViewHolder %7B%0A        ImageView image;%0A        TextView name, size;%0A        String packageName;%0A    %7D%0A%0A
     public A
@@ -1647,20 +1647,19 @@
  return 
-fals
+tru
 e;%0A    %7D
@@ -1944,24 +1944,25 @@
 false);%0A
+%0A
         
 %7D%0A%0A     
@@ -1945,35 +1945,377 @@
 alse);%0A%0A        
-%7D%0A%0A
+    ViewHolder viewHolder = new ViewHolder();%0A%0A            viewHolder.image = (ImageView) convertView.findViewById(R.id.app_icon);%0A            viewHolder.name = (TextView) convertView.findViewById(R.id.app_name);%0A            viewHolder.size = (TextView) convertView.findViewById(R.id.app_size);%0A%0A            convertView.setTag(viewHolder);%0A%0A    
         convertV
@@ -2370,32 +2370,36 @@
 ) %7B%0A            
+   
+ 
 @Override%0A      
@@ -2384,32 +2384,36 @@
       @Override%0A
+    
             publ
@@ -2454,16 +2454,20 @@
         
+    
 Intent i
@@ -2496,32 +2496,36 @@
                 
+  
+  
 intent.setFlags(
@@ -2557,16 +2557,20 @@
 R_TOP);%0A
+    
         
@@ -2666,32 +2666,36 @@
                 
+    
 intent.setData(U
@@ -2720,17 +2720,35 @@
 %22 + 
-item.getP
+((ViewHolder) v.getTag()).p
 acka
@@ -2753,23 +2753,25 @@
 kageName
-()
 ));%0A%0A
+    
         
@@ -2814,32 +2814,36 @@
 t);%0A            
+    
 %7D%0A        %7D);%0A%0A 
@@ -2840,104 +2840,103 @@
     
+ 
+   
 %7D);%0A
-%0A        ImageView imageView = (ImageView) convertView.findViewById(R.id.app_icon
+        %7D%0A%0A        ViewHolder viewHolder = (ViewHolder) convertView.getTag(
 );%0A
+%0A
         
 imag
@@ -2935,17 +2935,24 @@
     
+viewHolder.
 image
-View
 .set
@@ -2989,33 +2989,32 @@
 ionIcon());%0A
-%0A
         
 TextView nam
@@ -3005,96 +3005,23 @@
     
-TextView nameView = (TextView) convertView.findViewById(R.id.app_name);%0A        nameView
+viewHolder.name
 .set
@@ -3053,25 +3053,24 @@
 ame());%0A
-%0A
         
 TextView
@@ -3065,96 +3065,23 @@
     
-TextView sizeView = (TextView) convertView.findViewById(R.id.app_size);%0A        sizeView
+viewHolder.size
 .set
@@ -3147,16 +3147,72 @@
 ize()));
+%0A        viewHolder.packageName = item.getPackageName();
 %0A%0A      

Improve performance