package com.codepath.apps.dwitter;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.codepath.apps.dwitter.models.Tweet;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import cz.msebera.android.httpclient.ParseException;

/**
 * Created by lee on 8/4/16.
 */
public class TweetsArrayAdapter extends ArrayAdapter<Tweet> {
    public TweetsArrayAdapter(Context context, List<Tweet> tweets) {
        super(context, android.R.layout.simple_list_item_1, tweets);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        1) get tweet
        Tweet tweet = getItem(position);
//        2) find/inflate the template
        if(convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_tweet, parent, false);
        }
//        3) find the subview to fill with data
        ImageView ivProfileImage = (ImageView) convertView.findViewById(R.id.ivProfileImage);
        TextView tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
        TextView tvScreenName = (TextView) convertView.findViewById(R.id.tvScreenName);
        TextView tvBody = (TextView) convertView.findViewById(R.id.tvBody);
        TextView tvRelativeTimestamp = (TextView) convertView.findViewById(R.id.tvRelativeTimestamp);

//        4) populate data into subview
        tvUserName.setText(tweet.getUser().getName());
        tvScreenName.setText("@" + tweet.getUser().getScreenName());
        tvBody.setText(tweet.getBody());
        tvRelativeTimestamp.setText(getRelativeTimeAgo(tweet.getCreatedAt()));
        ivProfileImage.setImageResource(android.R.color.transparent);
        Glide.with(getContext()).load(tweet.getUser().getProfileImageUrl()).into(ivProfileImage);
//        5) return view to be inserted
        return convertView;
    }

    public String getRelativeTimeAgo(String rawJsonDate) {
        String twitterFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";
        SimpleDateFormat sf = new SimpleDateFormat(twitterFormat, Locale.ENGLISH);
        sf.setLenient(true);

        String relativeDate = "";
        try {
            long dateMillis = 0;
            try {
                dateMillis = sf.parse(rawJsonDate).getTime();
            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }
            relativeDate = DateUtils.getRelativeTimeSpanString(dateMillis,
                    System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS).toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return relativeDate;
    }
}

@@ -753,24 +753,226 @@
 ts);%0A    %7D%0A%0A
+    private static class ViewHolder %7B%0A        TextView tvUserName;%0A        ImageView ivProfileImage;%0A        TextView tvScreenName;%0A        TextView tvBody;%0A        TextView tvRelativeTimestamp;%0A    %7D%0A%0A
     @Overrid
@@ -1164,33 +1164,155 @@
     
-if(convertView == null) %7B
+ViewHolder viewHolder;%0A//        3) find the subview to fill with data%0A        if(convertView == null) %7B%0A            viewHolder = new ViewHolder();
 %0A   
@@ -1425,99 +1425,37 @@
     
-%7D%0A//        3) find the subview to fill with data%0A        ImageView ivProfileImage = (Image
+    viewHolder.tvBody = (Text
 View
@@ -1490,42 +1490,40 @@
 .id.
-ivProfileImage);%0A        TextView 
+tvBody);%0A            viewHolder.
 tvUs
@@ -1589,33 +1589,39 @@
 e);%0A        
-TextView 
+    viewHolder.
 tvScreenName
@@ -1687,31 +1687,50 @@
         
-TextView tvBody
+    viewHolder.tvRelativeTimestamp
  = (Text
@@ -1771,116 +1771,257 @@
 d.tv
-Body);%0A        TextView tvRelativeTimestamp = (TextView) convertView.findViewById(R.id.tvRelativeTimestamp);
+RelativeTimestamp);%0A            viewHolder.ivProfileImage = (ImageView) convertView.findViewById(R.id.ivProfileImage);%0A            convertView.setTag(viewHolder);%0A        %7D else %7B%0A            viewHolder = (ViewHolder) convertView.getTag();%0A        %7D
 %0A%0A//
@@ -2066,16 +2066,27 @@
         
+viewHolder.
 tvUserNa
@@ -2132,16 +2132,27 @@
         
+viewHolder.
 tvScreen
@@ -2212,16 +2212,27 @@
         
+viewHolder.
 tvBody.s
@@ -2264,16 +2264,27 @@
         
+viewHolder.
 tvRelati
@@ -2354,16 +2354,27 @@
         
+viewHolder.
 ivProfil
@@ -2508,16 +2508,27 @@
 )).into(
+viewHolder.
 ivProfil

Apply viewholder pattern to TweetArrayAdapter