mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-17 11:25:34 -04:00
Merge pull request #1973 from Frans-Lukas/1971-convert-historyadapter-to-kotlin
1971 convert historyadapter to kotlin
This commit is contained in:
commit
cab9934018
@ -21,6 +21,8 @@ package org.kiwix.kiwixmobile.core.extensions
|
|||||||
import android.content.res.ColorStateList
|
import android.content.res.ColorStateList
|
||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
import androidx.annotation.ColorInt
|
import androidx.annotation.ColorInt
|
||||||
|
import androidx.annotation.DrawableRes
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.widget.ImageViewCompat
|
import androidx.core.widget.ImageViewCompat
|
||||||
import org.kiwix.kiwixmobile.core.downloader.model.Base64String
|
import org.kiwix.kiwixmobile.core.downloader.model.Base64String
|
||||||
|
|
||||||
@ -35,6 +37,10 @@ fun ImageView.setBitmapFromString(string: String?) {
|
|||||||
setBitmap(Base64String(string))
|
setBitmap(Base64String(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun ImageView.setImageDrawableCompat(@DrawableRes id: Int) {
|
||||||
|
setImageDrawable(ContextCompat.getDrawable(context, id))
|
||||||
|
}
|
||||||
|
|
||||||
fun ImageView.tint(@ColorInt colorId: Int) {
|
fun ImageView.tint(@ColorInt colorId: Int) {
|
||||||
ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(colorId))
|
ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(colorId))
|
||||||
}
|
}
|
||||||
|
@ -1,133 +0,0 @@
|
|||||||
/*
|
|
||||||
* Kiwix Android
|
|
||||||
* Copyright (c) 2019 Kiwix <android.kiwix.org>
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.kiwix.kiwixmobile.core.history;
|
|
||||||
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.ImageView;
|
|
||||||
import android.widget.TextView;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
|
||||||
import butterknife.BindView;
|
|
||||||
import butterknife.ButterKnife;
|
|
||||||
import java.util.List;
|
|
||||||
import org.kiwix.kiwixmobile.core.R;
|
|
||||||
import org.kiwix.kiwixmobile.core.R2;
|
|
||||||
import org.kiwix.kiwixmobile.core.extensions.ImageViewExtensionsKt;
|
|
||||||
import org.threeten.bp.LocalDate;
|
|
||||||
import org.threeten.bp.format.DateTimeFormatter;
|
|
||||||
|
|
||||||
class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
|
||||||
private static final int TYPE_ITEM = 1;
|
|
||||||
private final List<HistoryListItem> historyList;
|
|
||||||
private final OnItemClickListener itemClickListener;
|
|
||||||
private final List<HistoryListItem> deleteList;
|
|
||||||
|
|
||||||
HistoryAdapter(List<HistoryListItem> historyList, List<HistoryListItem> deleteList,
|
|
||||||
OnItemClickListener itemClickListener) {
|
|
||||||
this.historyList = historyList;
|
|
||||||
this.deleteList = deleteList;
|
|
||||||
this.itemClickListener = itemClickListener;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
|
||||||
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
||||||
if (viewType == TYPE_ITEM) {
|
|
||||||
View view = LayoutInflater.from(parent.getContext())
|
|
||||||
.inflate(R.layout.item_bookmark_history, parent, false);
|
|
||||||
return new Item(view);
|
|
||||||
} else {
|
|
||||||
View view =
|
|
||||||
LayoutInflater.from(parent.getContext()).inflate(R.layout.header_date, parent, false);
|
|
||||||
return new Category(view);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
|
||||||
if (holder instanceof Item) {
|
|
||||||
HistoryListItem.HistoryItem history = (HistoryListItem.HistoryItem) historyList.get(position);
|
|
||||||
Item item = (Item) holder;
|
|
||||||
item.title.setText(history.getHistoryTitle());
|
|
||||||
if (deleteList.contains(history)) {
|
|
||||||
item.favicon.setImageDrawable(ContextCompat.getDrawable(item.favicon.getContext(),
|
|
||||||
R.drawable.ic_check_circle_blue_24dp));
|
|
||||||
} else {
|
|
||||||
ImageViewExtensionsKt.setBitmapFromString(item.favicon, history.getFavicon());
|
|
||||||
}
|
|
||||||
item.itemView.setOnClickListener(v -> itemClickListener.onItemClick(item.favicon, history));
|
|
||||||
item.itemView.setOnLongClickListener(v ->
|
|
||||||
itemClickListener.onItemLongClick(item.favicon, history));
|
|
||||||
} else {
|
|
||||||
String date = ((HistoryListItem.DateItem) historyList.get(position)).getDateString();
|
|
||||||
String todaysDate, yesterdayDate;
|
|
||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d MMM yyyy");
|
|
||||||
todaysDate = LocalDate.now().format(formatter);
|
|
||||||
yesterdayDate = LocalDate.now().minusDays(1).format(formatter);
|
|
||||||
if (todaysDate.contentEquals(date)) {
|
|
||||||
((Category) holder).date.setText(R.string.time_today);
|
|
||||||
} else if (yesterdayDate.contentEquals(date)) {
|
|
||||||
((Category) holder).date.setText(R.string.time_yesterday);
|
|
||||||
} else {
|
|
||||||
((Category) holder).date.setText(date);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getItemViewType(int position) {
|
|
||||||
return historyList.get(position) instanceof HistoryListItem.DateItem ? 0 : TYPE_ITEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getItemCount() {
|
|
||||||
return historyList.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
interface OnItemClickListener {
|
|
||||||
void onItemClick(ImageView favicon, HistoryListItem.HistoryItem history);
|
|
||||||
|
|
||||||
boolean onItemLongClick(ImageView favicon, HistoryListItem.HistoryItem history);
|
|
||||||
}
|
|
||||||
|
|
||||||
class Item extends RecyclerView.ViewHolder {
|
|
||||||
@BindView(R2.id.favicon)
|
|
||||||
ImageView favicon;
|
|
||||||
@BindView(R2.id.title)
|
|
||||||
TextView title;
|
|
||||||
|
|
||||||
Item(View itemView) {
|
|
||||||
super(itemView);
|
|
||||||
ButterKnife.bind(this, itemView);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Category extends RecyclerView.ViewHolder {
|
|
||||||
@BindView(R2.id.header_date)
|
|
||||||
TextView date;
|
|
||||||
|
|
||||||
Category(View itemView) {
|
|
||||||
super(itemView);
|
|
||||||
ButterKnife.bind(this, itemView);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* Kiwix Android
|
||||||
|
* Copyright (c) 2019 Kiwix <android.kiwix.org>
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.kiwix.kiwixmobile.core.history
|
||||||
|
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.ImageView
|
||||||
|
import androidx.recyclerview.widget.RecyclerView.Adapter
|
||||||
|
import androidx.recyclerview.widget.RecyclerView.ViewHolder
|
||||||
|
import org.kiwix.kiwixmobile.core.R
|
||||||
|
import org.kiwix.kiwixmobile.core.extensions.ViewGroupExtensions.inflate
|
||||||
|
import org.kiwix.kiwixmobile.core.history.HistoryListItem.DateItem
|
||||||
|
import org.kiwix.kiwixmobile.core.history.HistoryListItem.HistoryItem
|
||||||
|
|
||||||
|
class HistoryAdapter(
|
||||||
|
val historyList: List<HistoryListItem>,
|
||||||
|
val deleteList: List<HistoryListItem>,
|
||||||
|
val itemClickListener: OnItemClickListener
|
||||||
|
) : Adapter<ViewHolder>() {
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
|
||||||
|
if (viewType == HISTORY_ITEM_VIEW_TYPE)
|
||||||
|
HistoryItemViewHolder(
|
||||||
|
parent.inflate(R.layout.item_bookmark_history, false),
|
||||||
|
deleteList,
|
||||||
|
itemClickListener
|
||||||
|
)
|
||||||
|
else
|
||||||
|
HistoryDateHeaderItemViewHolder(parent.inflate(R.layout.header_date, false))
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||||
|
if (holder is HistoryItemViewHolder) {
|
||||||
|
holder.bind(historyList[position] as HistoryItem)
|
||||||
|
} else if (holder is HistoryDateHeaderItemViewHolder) {
|
||||||
|
holder.bind(historyList[position] as DateItem)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemViewType(position: Int): Int =
|
||||||
|
if (historyList[position] is DateItem) DATE_ITEM_VIEW_TYPE else HISTORY_ITEM_VIEW_TYPE
|
||||||
|
|
||||||
|
override fun getItemCount(): Int = historyList.size
|
||||||
|
|
||||||
|
interface OnItemClickListener {
|
||||||
|
fun onItemClick(favicon: ImageView, history: HistoryItem)
|
||||||
|
|
||||||
|
fun onItemLongClick(favicon: ImageView, history: HistoryItem): Boolean
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package org.kiwix.kiwixmobile.core.history
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import kotlinx.android.synthetic.main.header_date.header_date
|
||||||
|
import org.kiwix.kiwixmobile.core.R
|
||||||
|
import org.kiwix.kiwixmobile.core.base.adapter.BaseViewHolder
|
||||||
|
import org.kiwix.kiwixmobile.core.history.HistoryListItem.DateItem
|
||||||
|
import org.threeten.bp.format.DateTimeFormatter
|
||||||
|
import org.threeten.bp.LocalDate
|
||||||
|
|
||||||
|
class HistoryDateHeaderItemViewHolder(itemView: View) : BaseViewHolder<DateItem>(itemView) {
|
||||||
|
override fun bind(item: DateItem) {
|
||||||
|
val todaysDate = LocalDate.now()
|
||||||
|
val yesterdayDate = todaysDate.minusDays(1)
|
||||||
|
val givenDate = LocalDate.parse(item.dateString, DateTimeFormatter.ofPattern("d MMM yyyy"))
|
||||||
|
|
||||||
|
when (givenDate) {
|
||||||
|
todaysDate -> header_date.setText(R.string.time_today)
|
||||||
|
yesterdayDate -> header_date.setText(R.string.time_yesterday)
|
||||||
|
else -> header_date.text = item.dateString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package org.kiwix.kiwixmobile.core.history
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import kotlinx.android.synthetic.main.item_bookmark_history.favicon
|
||||||
|
import kotlinx.android.synthetic.main.item_bookmark_history.title
|
||||||
|
import org.kiwix.kiwixmobile.core.R
|
||||||
|
import org.kiwix.kiwixmobile.core.base.adapter.BaseViewHolder
|
||||||
|
import org.kiwix.kiwixmobile.core.downloader.model.Base64String
|
||||||
|
import org.kiwix.kiwixmobile.core.extensions.setBitmap
|
||||||
|
import org.kiwix.kiwixmobile.core.extensions.setImageDrawableCompat
|
||||||
|
import org.kiwix.kiwixmobile.core.history.HistoryAdapter.OnItemClickListener
|
||||||
|
import org.kiwix.kiwixmobile.core.history.HistoryListItem.HistoryItem
|
||||||
|
|
||||||
|
class HistoryItemViewHolder(
|
||||||
|
itemView: View,
|
||||||
|
val deleteList: List<HistoryListItem>,
|
||||||
|
val itemClickListener: OnItemClickListener
|
||||||
|
) : BaseViewHolder<HistoryItem>(itemView) {
|
||||||
|
override fun bind(item: HistoryItem) {
|
||||||
|
title.text = item.historyTitle
|
||||||
|
if (deleteList.contains(item)) {
|
||||||
|
favicon.setImageDrawableCompat(R.drawable.ic_check_circle_blue_24dp)
|
||||||
|
} else {
|
||||||
|
favicon.setBitmap(Base64String(item.favicon))
|
||||||
|
}
|
||||||
|
itemView.setOnClickListener { itemClickListener.onItemClick(favicon, item) }
|
||||||
|
itemView.setOnLongClickListener { itemClickListener.onItemLongClick(favicon, item) }
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,9 @@ package org.kiwix.kiwixmobile.core.history
|
|||||||
import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity
|
import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity
|
||||||
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
|
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
|
||||||
|
|
||||||
|
const val HISTORY_ITEM_VIEW_TYPE = 1
|
||||||
|
const val DATE_ITEM_VIEW_TYPE = 0
|
||||||
|
|
||||||
sealed class HistoryListItem {
|
sealed class HistoryListItem {
|
||||||
abstract val id: Long
|
abstract val id: Long
|
||||||
|
|
||||||
@ -68,6 +71,7 @@ sealed class HistoryListItem {
|
|||||||
|
|
||||||
data class DateItem(
|
data class DateItem(
|
||||||
val dateString: String,
|
val dateString: String,
|
||||||
override val id: Long = dateString.hashCode().toLong()
|
override val id: Long = dateString.hashCode()
|
||||||
|
.toLong()
|
||||||
) : HistoryListItem()
|
) : HistoryListItem()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user