Last active
December 20, 2015 00:49
-
-
Save KamilLelonek/6044148 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pwr.rss.reader.database.adapters | |
import android.content.Context | |
import android.database.Cursor | |
import android.support.v4.widget.SimpleCursorAdapter | |
import pwr.rss.reader.R | |
import pwr.rss.reader.database.tables.TableChannels.C_SITE | |
import pwr.rss.reader.database.tables.TableFeeds.C_ADDED_DATE | |
import pwr.rss.reader.database.tables.TableFeeds.C_IMAGE | |
import pwr.rss.reader.database.tables.TableFeeds.C_TITLE | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.LayoutInflater | |
class FeedCursorAdapter(context: Context, cursor: Cursor) | |
extends SimpleCursorAdapter( | |
context, | |
R.layout.list_item, | |
cursor, | |
Array(C_TITLE, C_SITE, C_ADDED_DATE, C_IMAGE), | |
Array(R.id.textViewFeedTitle, R.id.textViewChannelSite, R.id.textViewFeedDate, R.id.imageViewFeedImage), | |
0) { | |
setViewBinder(new FeedCursorViewBinder(context)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pwr.rss.reader.database.adapters | |
import android.content.Context | |
import android.database.Cursor | |
import android.support.v4.widget.CursorAdapter | |
import android.view.ViewGroup | |
import android.view.View | |
import android.widget.ImageView | |
import android.widget.TextView | |
import android.view.LayoutInflater | |
import pwr.rss.reader.R | |
import pwr.rss.reader.utils.BitmapUtils | |
import android.graphics.BitmapFactory | |
import android.graphics.Color | |
import pwr.rss.reader.database.tables.TableChannels.C_SITE | |
import pwr.rss.reader.database.tables.TableFeeds.C_ADDED_DATE | |
import pwr.rss.reader.database.tables.TableFeeds.C_CHANNEL | |
import pwr.rss.reader.database.tables.TableFeeds.C_IMAGE | |
import pwr.rss.reader.database.tables.TableFeeds.C_READ | |
import pwr.rss.reader.database.tables.TableFeeds.C_TITLE | |
import pwr.rss.reader.views.ViewHelper._ | |
import java.util.Date | |
import android.util.Log | |
class FeedCursorAdapter(context: Context, cursor: Cursor) | |
extends CursorAdapter(context, cursor, 0) { | |
implicit def int2bool(int: Int) = if (int == 1) true else false | |
implicit def longDate2String(longDate: Long) = new Date(longDate).toLocaleString | |
private lazy val bitmapUtils = new BitmapUtils(context) | |
private lazy val inflater = LayoutInflater.from(context) | |
private case class ViewHolder( | |
feedTitle: TextView, | |
channelSite: TextView, | |
feedImage: ImageView, | |
feedDate: TextView) | |
override def newView(context: Context, cursor: Cursor, parent: ViewGroup) = | |
inflater.inflate(R.layout.list_item, parent, false) | |
override def bindView(view: View, context: Context, cursor: Cursor) = { | |
def getColumnIndex(columnName: String) = cursor.getColumnIndex(columnName) | |
val indexTitle = getColumnIndex(C_TITLE) | |
val indexRead = getColumnIndex(C_READ) | |
val indexDate = getColumnIndex(C_ADDED_DATE) | |
val indexImage = getColumnIndex(C_IMAGE) | |
val indexSite = getColumnIndex(C_SITE) | |
def setTextAndColor(index: Int, textView: TextView) = { | |
val text = cursor.getString(index) | |
textView.setTextColor(getTextColor) | |
textView.setText(text) | |
true | |
} | |
def getTextColor = { | |
val isRead: Boolean = cursor.getInt(indexRead) | |
if (isRead) Color.GRAY | |
else Color.BLACK | |
} | |
var tag = view.getTag().asInstanceOf[ViewHolder] | |
if (tag == null) { | |
val textViewFeedTitle = findView[TextView](view, R.id.textViewFeedTitle) | |
val textViewChannelSite = findView[TextView](view, R.id.textViewChannelSite) | |
val imageViewFeedImage = findView[ImageView](view, R.id.imageViewFeedImage) | |
val textViewFeedDate = findView[TextView](view, R.id.textViewFeedDate) | |
tag = new ViewHolder(textViewFeedTitle, textViewChannelSite, imageViewFeedImage, textViewFeedDate) | |
view.setTag(tag) | |
} | |
setTextAndColor(indexTitle, tag.feedTitle) | |
setTextAndColor(indexSite, tag.channelSite) | |
val imageId: Int = cursor.getInt(indexImage) | |
val imageFromFile = bitmapUtils.readBitmapForFeed(imageId) | |
val feedImage = | |
if (imageFromFile != null) imageFromFile | |
else BitmapFactory.decodeResource(context.getResources, imageId) | |
tag.feedImage.setImageBitmap(feedImage) | |
val dateLong = cursor.getLong(indexDate) | |
tag.feedDate.setText(dateLong) | |
} | |
override def getViewTypeCount = 1000 | |
override def getItemViewType(position: Int) = position | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pwr.rss.reader.database.adapters | |
import android.graphics.BitmapFactory | |
import android.widget.ImageView | |
import pwr.rss.reader.utils.BitmapUtils | |
import android.graphics.Color | |
import android.widget.TextView | |
import android.database.Cursor | |
import android.view.View | |
import java.util.Date | |
import android.support.v4.widget.SimpleCursorAdapter | |
import android.content.Context | |
import pwr.rss.reader.database.tables.TableChannels.C_SITE | |
import pwr.rss.reader.database.tables.TableFeeds.C_ADDED_DATE | |
import pwr.rss.reader.database.tables.TableFeeds.C_CHANNEL | |
import pwr.rss.reader.database.tables.TableFeeds.C_IMAGE | |
import pwr.rss.reader.database.tables.TableFeeds.C_READ | |
import pwr.rss.reader.database.tables.TableFeeds.C_TITLE | |
class FeedCursorViewBinder(context: Context) extends SimpleCursorAdapter.ViewBinder { | |
implicit def int2bool(int: Int) = if (int == 1) true else false | |
implicit def longDate2String(longDate: Long) = new Date(longDate).toLocaleString | |
private lazy val bitmapUtils = new BitmapUtils(context) | |
override def setViewValue(view: View, cursor: Cursor, columnIndex: Int) = { | |
/** | |
* Get columns indexes from cursor | |
*/ | |
def getColumnIndex(columnName: String) = cursor.getColumnIndex(columnName) | |
val indexTitle = getColumnIndex(C_TITLE) | |
val indexRead = getColumnIndex(C_READ) | |
val indexDate = getColumnIndex(C_ADDED_DATE) | |
val indexImage = getColumnIndex(C_IMAGE) | |
val indexChannel = getColumnIndex(C_CHANNEL) | |
val indexSite = getColumnIndex(C_SITE) | |
def setTextAndColor(index: Int) = { | |
val text = cursor.getString(index) | |
val textView = view.asInstanceOf[TextView] | |
textView.setTextColor(getTextColor) | |
textView.setText(text) | |
true | |
} | |
def getTextColor = { | |
val isRead: Boolean = cursor.getInt(indexRead) | |
if (isRead) Color.GRAY | |
else Color.BLACK | |
} | |
columnIndex match { | |
case `indexTitle` | `indexSite` => setTextAndColor(columnIndex) | |
case `indexImage` => { | |
val imageId: Int = cursor.getInt(indexImage) | |
val imageFromFile = bitmapUtils.readBitmapForFeed(imageId) | |
val feedImage = | |
if (imageFromFile != null) imageFromFile | |
else BitmapFactory.decodeResource(context.getResources, imageId) | |
val imageViewFeedImage = view.asInstanceOf[ImageView] | |
imageViewFeedImage.setImageBitmap(feedImage) | |
true | |
} | |
case `indexDate` => { | |
val dateLong = cursor.getLong(indexDate) | |
val textViewFeedDate = view.asInstanceOf[TextView] | |
textViewFeedDate.setText(dateLong) | |
true | |
} | |
case _ => false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment