diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ImageViewExtensions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ImageViewExtensions.kt index 5b73454ba..7b5fbd00d 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ImageViewExtensions.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ImageViewExtensions.kt @@ -21,6 +21,8 @@ package org.kiwix.kiwixmobile.core.extensions import android.content.res.ColorStateList import android.widget.ImageView import androidx.annotation.ColorInt +import androidx.annotation.DrawableRes +import androidx.core.content.ContextCompat import androidx.core.widget.ImageViewCompat import org.kiwix.kiwixmobile.core.downloader.model.Base64String @@ -35,6 +37,10 @@ fun ImageView.setBitmapFromString(string: String?) { setBitmap(Base64String(string)) } +fun ImageView.setImageDrawableCompat(@DrawableRes id: Int) { + setImageDrawable(ContextCompat.getDrawable(context, id)) +} + fun ImageView.tint(@ColorInt colorId: Int) { ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(colorId)) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryAdapter.java b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryAdapter.java deleted file mode 100644 index f87476575..000000000 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryAdapter.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 Kiwix - * 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 . - * - */ - -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 { - private static final int TYPE_ITEM = 1; - private final List historyList; - private final OnItemClickListener itemClickListener; - private final List deleteList; - - HistoryAdapter(List historyList, List 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); - } - } -} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryAdapter.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryAdapter.kt new file mode 100644 index 000000000..7ee2df077 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryAdapter.kt @@ -0,0 +1,63 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * 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 . + * + */ +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, + val deleteList: List, + val itemClickListener: OnItemClickListener +) : Adapter() { + + 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 + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryDateHeaderItemViewHolder.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryDateHeaderItemViewHolder.kt new file mode 100644 index 000000000..ee90cf098 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryDateHeaderItemViewHolder.kt @@ -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(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 + } + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryItemViewHolder.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryItemViewHolder.kt new file mode 100644 index 000000000..ee41db18d --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryItemViewHolder.kt @@ -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, + val itemClickListener: OnItemClickListener +) : BaseViewHolder(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) } + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryListItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryListItem.kt index 897c0a342..2b58f540f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryListItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/history/HistoryListItem.kt @@ -20,6 +20,9 @@ package org.kiwix.kiwixmobile.core.history import org.kiwix.kiwixmobile.core.dao.entities.HistoryEntity import org.kiwix.kiwixmobile.core.reader.ZimFileReader +const val HISTORY_ITEM_VIEW_TYPE = 1 +const val DATE_ITEM_VIEW_TYPE = 0 + sealed class HistoryListItem { abstract val id: Long @@ -68,6 +71,7 @@ sealed class HistoryListItem { data class DateItem( val dateString: String, - override val id: Long = dateString.hashCode().toLong() + override val id: Long = dateString.hashCode() + .toLong() ) : HistoryListItem() }