From ccb2f00014dc55aba7b659d7e5bfa1f8c35184d7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 8 Sep 2016 22:57:07 +0200 Subject: [PATCH] convert ItemsAdapter to kotlin --- .../filemanager/adapters/ItemsAdapter.java | 98 ------------------- .../filemanager/adapters/ItemsAdapter.kt | 78 +++++++++++++++ 2 files changed, 78 insertions(+), 98 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/filemanager/adapters/ItemsAdapter.java create mode 100644 app/src/main/java/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt diff --git a/app/src/main/java/com/simplemobiletools/filemanager/adapters/ItemsAdapter.java b/app/src/main/java/com/simplemobiletools/filemanager/adapters/ItemsAdapter.java deleted file mode 100644 index 6386ad6d..00000000 --- a/app/src/main/java/com/simplemobiletools/filemanager/adapters/ItemsAdapter.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.simplemobiletools.filemanager.adapters; - -import android.content.Context; -import android.content.res.Resources; -import android.graphics.Bitmap; -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.simplemobiletools.filemanager.R; -import com.simplemobiletools.filemanager.Utils; -import com.simplemobiletools.filemanager.models.FileDirItem; - -import java.util.List; - -import butterknife.BindView; -import butterknife.ButterKnife; - -public class ItemsAdapter extends BaseAdapter { - private final List mItems; - private final LayoutInflater mInflater; - private final Bitmap mFileBmp; - private final Bitmap mDirectoryBmp; - private final Resources mRes; - - public ItemsAdapter(Context context, List items) { - mItems = items; - mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - - mRes = context.getResources(); - mDirectoryBmp = Utils.getColoredIcon(mRes, R.color.lightGrey, R.mipmap.directory); - mFileBmp = Utils.getColoredIcon(mRes, R.color.lightGrey, R.mipmap.file); - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - ViewHolder viewHolder; - if (convertView == null) { - convertView = mInflater.inflate(R.layout.list_item, parent, false); - viewHolder = new ViewHolder(convertView); - convertView.setTag(viewHolder); - } else { - viewHolder = (ViewHolder) convertView.getTag(); - } - - final FileDirItem item = mItems.get(position); - viewHolder.name.setText(item.getName()); - - if (item.isDirectory()) { - viewHolder.icon.setImageBitmap(mDirectoryBmp); - viewHolder.details.setText(getChildrenCnt(item)); - } else { - viewHolder.icon.setImageBitmap(mFileBmp); - viewHolder.details.setText(Utils.getFormattedSize(item)); - } - - return convertView; - } - - private String getChildrenCnt(FileDirItem item) { - final int children = item.getChildren(); - return mRes.getQuantityString(R.plurals.items, children, children); - } - - @Override - public int getCount() { - return mItems.size(); - } - - @Override - public Object getItem(int position) { - return mItems.get(position); - } - - @Override - public long getItemId(int position) { - return 0; - } - - public void updateItems(List newItems) { - mItems.clear(); - mItems.addAll(newItems); - notifyDataSetChanged(); - } - - static class ViewHolder { - @BindView(R.id.item_name) TextView name; - @BindView(R.id.item_icon) ImageView icon; - @BindView(R.id.item_details) TextView details; - - public ViewHolder(View view) { - ButterKnife.bind(this, view); - } - } -} diff --git a/app/src/main/java/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt b/app/src/main/java/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt new file mode 100644 index 00000000..2645493f --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt @@ -0,0 +1,78 @@ +package com.simplemobiletools.filemanager.adapters + +import android.content.Context +import android.content.res.Resources +import android.graphics.Bitmap +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.simplemobiletools.filemanager.R +import com.simplemobiletools.filemanager.Utils +import com.simplemobiletools.filemanager.models.FileDirItem +import kotlinx.android.synthetic.main.list_item.view.* + +class ItemsAdapter(context: Context, private val mItems: List) : BaseAdapter() { + private val mInflater: LayoutInflater + private val mFileBmp: Bitmap + private val mDirectoryBmp: Bitmap + private val mRes: Resources + + init { + mInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + + mRes = context.resources + mDirectoryBmp = Utils.getColoredIcon(mRes, R.color.lightGrey, R.mipmap.directory) + mFileBmp = Utils.getColoredIcon(mRes, R.color.lightGrey, R.mipmap.file) + } + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + var view = convertView + val viewHolder: ViewHolder + if (view == null) { + view = mInflater.inflate(R.layout.list_item, parent, false) + viewHolder = ViewHolder(view) + view!!.tag = viewHolder + } else { + viewHolder = view.tag as ViewHolder + } + + val item = mItems[position] + viewHolder.name.text = item.name + + if (item.isDirectory) { + viewHolder.icon.setImageBitmap(mDirectoryBmp) + viewHolder.details.text = getChildrenCnt(item) + } else { + viewHolder.icon.setImageBitmap(mFileBmp) + viewHolder.details.text = Utils.getFormattedSize(item) + } + + return view + } + + private fun getChildrenCnt(item: FileDirItem): String { + val children = item.children + return mRes.getQuantityString(R.plurals.items, children, children) + } + + override fun getCount(): Int { + return mItems.size + } + + override fun getItem(position: Int): Any { + return mItems[position] + } + + override fun getItemId(position: Int): Long { + return 0 + } + + internal class ViewHolder(view: View) { + val name: TextView = view.item_name + val icon: ImageView = view.item_icon + val details: TextView = view.item_details + } +}