convert directoryadapter to kotlin

This commit is contained in:
tibbi 2016-11-06 15:54:53 +01:00
parent f0e8746bce
commit d60521411e
2 changed files with 73 additions and 90 deletions

View file

@ -1,90 +0,0 @@
package com.simplemobiletools.gallery.adapters;
import android.content.Context;
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.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.signature.StringSignature;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.models.Directory;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class DirectoryAdapter extends BaseAdapter {
private final Context mContext;
private final List<Directory> mDirs;
private final LayoutInflater mInflater;
public DirectoryAdapter(Context context, List<Directory> dirs) {
mContext = context;
mDirs = dirs;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.directory_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final Directory dir = mDirs.get(position);
viewHolder.dirName.setText(dir.getName());
viewHolder.photoCnt.setText(String.valueOf(dir.getMediaCnt()));
final String tmb = dir.getThumbnail();
final StringSignature timestampSignature = new StringSignature(String.valueOf(dir.getTimestamp()));
if (tmb.endsWith(".gif")) {
Glide.with(mContext).load(tmb).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature)
.placeholder(R.color.tmb_background).centerCrop().crossFade().into(viewHolder.dirThumbnail);
} else {
Glide.with(mContext).load(tmb).diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature)
.placeholder(R.color.tmb_background).centerCrop().crossFade().into(viewHolder.dirThumbnail);
}
return convertView;
}
@Override
public int getCount() {
return mDirs.size();
}
@Override
public Object getItem(int position) {
return mDirs.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public void updateItems(List<Directory> newDirs) {
mDirs.clear();
mDirs.addAll(newDirs);
notifyDataSetChanged();
}
static class ViewHolder {
@BindView(R.id.dir_name) TextView dirName;
@BindView(R.id.photo_cnt) TextView photoCnt;
@BindView(R.id.dir_thumbnail) ImageView dirThumbnail;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}

View file

@ -0,0 +1,73 @@
package com.simplemobiletools.gallery.adapters
import android.content.Context
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.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.signature.StringSignature
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.models.Directory
import kotlinx.android.synthetic.main.directory_item.view.*
import kotlinx.android.synthetic.main.directory_tmb.view.*
class DirectoryAdapter(private val mContext: Context, private val mDirs: MutableList<Directory>) : BaseAdapter() {
private val mInflater: LayoutInflater
init {
mInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
}
override fun getView(position: Int, view: View?, parent: ViewGroup): View {
var convertView = view
val viewHolder: ViewHolder
if (convertView == null) {
convertView = mInflater.inflate(R.layout.directory_item, parent, false)
viewHolder = ViewHolder(convertView)
convertView!!.tag = viewHolder
} else {
viewHolder = convertView.tag as ViewHolder
}
val dir = mDirs[position]
viewHolder.dirName.text = formatDirectoryName(dir)
viewHolder.photoCnt.text = dir.mediaCnt.toString()
val tmb = dir.thumbnail
val timestampSignature = StringSignature(dir.timestamp.toString())
if (tmb.endsWith(".gif")) {
Glide.with(mContext).load(tmb).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature)
.placeholder(R.color.tmb_background).centerCrop().crossFade().into(viewHolder.dirThumbnail)
} else {
Glide.with(mContext).load(tmb).diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature)
.placeholder(R.color.tmb_background).centerCrop().crossFade().into(viewHolder.dirThumbnail)
}
return convertView
}
private fun formatDirectoryName(dir: Directory): String {
return dir.name
}
override fun getCount(): Int {
return mDirs.size
}
override fun getItem(position: Int): Any {
return mDirs[position]
}
override fun getItemId(position: Int): Long {
return 0
}
internal class ViewHolder(view: View) {
val dirName: TextView = view.dir_name
val photoCnt: TextView = view.photo_cnt
val dirThumbnail: ImageView = view.dir_thumbnail
}
}