From 701760d80f503b6f9a78441ac561d5dc4ecd8e90 Mon Sep 17 00:00:00 2001 From: timbze Date: Mon, 1 Aug 2022 07:22:17 -0600 Subject: [PATCH] Days bar is showing for reading plan --- .../control/readingplan/ReadingPlanControl.kt | 15 ++++++++ .../view/activity/readingplan/DailyReading.kt | 18 ++++++++++ .../readingplan/DailyReadingDayBarAdapter.kt | 23 +++++++++++-- .../activity/readingplan/model/DayBarItem.kt | 18 ++++++++++ .../service/readingplan/OneDaysReadingsDto.kt | 34 +++++++++---------- .../main/res/layout/reading_day_bar_box.xml | 3 ++ 6 files changed, 92 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/net/bible/android/control/readingplan/ReadingPlanControl.kt b/app/src/main/java/net/bible/android/control/readingplan/ReadingPlanControl.kt index 315a953c62..ea45781e0c 100644 --- a/app/src/main/java/net/bible/android/control/readingplan/ReadingPlanControl.kt +++ b/app/src/main/java/net/bible/android/control/readingplan/ReadingPlanControl.kt @@ -26,6 +26,7 @@ import net.bible.android.control.event.passage.BeforeCurrentPageChangeEvent import net.bible.android.control.page.CurrentPageManager import net.bible.android.control.page.window.ActiveWindowPageManagerProvider import net.bible.android.control.speak.SpeakControl +import net.bible.android.view.activity.readingplan.model.DayBarItem import net.bible.service.common.CommonUtils import net.bible.service.db.readingplan.ReadingPlanRepository import net.bible.service.readingplan.OneDaysReadingsDto @@ -90,6 +91,20 @@ class ReadingPlanControl @Inject constructor( val currentPlansReadingList: List get() = readingPlanTextDao.getReadingList(currentPlanCode) + val currentPlansReadingDayBarItems: List + get() { + val planCurrentDay = currentPlanDay + return readingPlanTextDao.getReadingList(currentPlanCode).map { + DayBarItem( + it.day, + it.readingDate, + it.day == planCurrentDay, + false, // TODO add functionality + it.day < planCurrentDay + ) + } + } + val currentPlanExists: Boolean get() = try { readingPlanTextDao.getReading(currentPlanCode, 1) true diff --git a/app/src/main/java/net/bible/android/view/activity/readingplan/DailyReading.kt b/app/src/main/java/net/bible/android/view/activity/readingplan/DailyReading.kt index 499b78c92b..d2d2850d78 100644 --- a/app/src/main/java/net/bible/android/view/activity/readingplan/DailyReading.kt +++ b/app/src/main/java/net/bible/android/view/activity/readingplan/DailyReading.kt @@ -29,6 +29,9 @@ import android.view.View import android.widget.TableLayout import androidx.activity.result.contract.ActivityResultContracts import androidx.core.view.MenuCompat +import androidx.recyclerview.widget.DividerItemDecoration +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import net.bible.android.BibleApplication import net.bible.android.activity.R @@ -39,6 +42,7 @@ import net.bible.android.control.readingplan.ReadingStatus import net.bible.android.view.activity.base.CustomTitlebarActivityBase import net.bible.android.view.activity.base.Dialogs import net.bible.android.view.activity.installzip.InstallZip +import net.bible.android.view.activity.readingplan.model.DayBarItem import net.bible.service.common.CommonUtils import net.bible.service.readingplan.OneDaysReadingsDto @@ -199,6 +203,8 @@ class DailyReading : CustomTitlebarActivityBase() { } // end All + setupRecycler() + Log.i(TAG, "Finished displaying Reading view") } catch (e: Exception) { Log.e(TAG, "Error showing daily readings", e) @@ -207,6 +213,18 @@ class DailyReading : CustomTitlebarActivityBase() { } + private lateinit var viewAdapter: DailyReadingDayBarAdapter + private fun setupRecycler() { + val days = readingPlanControl.currentPlansReadingDayBarItems + viewAdapter = DailyReadingDayBarAdapter() + viewAdapter.submitList(days) + binding.daysRecycler.apply { + layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false) + adapter = viewAdapter + addItemDecoration(DividerItemDecoration(context, LinearLayoutManager.HORIZONTAL)) + } + } + override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.reading_plan, menu) MenuCompat.setGroupDividerEnabled(menu, true) diff --git a/app/src/main/java/net/bible/android/view/activity/readingplan/DailyReadingDayBarAdapter.kt b/app/src/main/java/net/bible/android/view/activity/readingplan/DailyReadingDayBarAdapter.kt index 916e2465fb..3ea9d8e2d1 100644 --- a/app/src/main/java/net/bible/android/view/activity/readingplan/DailyReadingDayBarAdapter.kt +++ b/app/src/main/java/net/bible/android/view/activity/readingplan/DailyReadingDayBarAdapter.kt @@ -18,21 +18,40 @@ package net.bible.android.view.activity.readingplan +import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView +import net.bible.android.activity.R import net.bible.android.activity.databinding.ReadingDayBarBoxBinding import net.bible.android.view.activity.readingplan.model.DayBarItem +import net.bible.service.common.CommonUtils.getResourceColor +import java.text.SimpleDateFormat +import java.util.Locale class DailyReadingDayBarAdapter : ListAdapter(DIFF_CALLBACK) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - TODO("Not yet implemented") + val binding = ReadingDayBarBoxBinding.inflate(LayoutInflater.from(parent.context), parent, false) + return ViewHolder(binding) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { - TODO("Not yet implemented") + val item = getItem(position) ?: return + holder.binding.apply { + dayNumberView.text = item.dayNumber.toString() + dateView.text = SimpleDateFormat("MMM dd", Locale.getDefault()).format(item.date) + + boxContainer.setBackgroundColor( + if (item.dayActive) + getResourceColor(R.color.sync_on_green) + else if (item.dayReadComplete) + getResourceColor(R.color.grey_700) + else + getResourceColor(R.color.grey_500) + ) + } } inner class ViewHolder(val binding: ReadingDayBarBoxBinding) : RecyclerView.ViewHolder(binding.root) diff --git a/app/src/main/java/net/bible/android/view/activity/readingplan/model/DayBarItem.kt b/app/src/main/java/net/bible/android/view/activity/readingplan/model/DayBarItem.kt index 12b73a98b2..bacb94c335 100644 --- a/app/src/main/java/net/bible/android/view/activity/readingplan/model/DayBarItem.kt +++ b/app/src/main/java/net/bible/android/view/activity/readingplan/model/DayBarItem.kt @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022 Martin Denham, Tuomas Airaksinen and the And Bible contributors. + * + * This file is part of And Bible (http://github.com/AndBible/and-bible). + * + * And Bible 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. + * + * And Bible 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 And Bible. + * If not, see http://www.gnu.org/licenses/. + * + */ + package net.bible.android.view.activity.readingplan.model import java.util.Date diff --git a/app/src/main/java/net/bible/service/readingplan/OneDaysReadingsDto.kt b/app/src/main/java/net/bible/service/readingplan/OneDaysReadingsDto.kt index f70dedb7dd..84a48caf16 100644 --- a/app/src/main/java/net/bible/service/readingplan/OneDaysReadingsDto.kt +++ b/app/src/main/java/net/bible/service/readingplan/OneDaysReadingsDto.kt @@ -41,9 +41,17 @@ class OneDaysReadingsDto(val day: Int, private val readingsString: String?, val private val dateBasedWithYear = SimpleDateFormat("MMM-d/yyyy", Locale.getDefault()) private var readingKeys: List? = null + + private var _readingDate: Date? = null /** reading date for date-based plan, else null */ - var readingDate: Date? = null + var readingDate: Date + get() { + if (_readingDate == null) + _readingDate = calculateDate(readingPlanInfo.startDate ?: Date()) + return _readingDate!! + } + set(value) { _readingDate = value } init { checkKeysGenerated() @@ -55,22 +63,14 @@ class OneDaysReadingsDto(val day: Int, private val readingsString: String?, val /** get a string representing the date this reading is planned for */ val readingDateString: String - get() { - val readingDate = readingDate - return if (readingDate != null) { - SimpleDateFormat.getDateInstance().format(readingDate) - } else { - var dateString = "" - val startDate = readingPlanInfo.startDate - if (startDate != null) { - val cal = Calendar.getInstance() - cal.time = startDate - cal.add(Calendar.DAY_OF_MONTH, day - 1) - dateString = SimpleDateFormat.getDateInstance().format(cal.time) - } - dateString - } - } + get() = SimpleDateFormat.getDateInstance().format(readingDate) + + private fun calculateDate(startDate: Date): Date { + val cal = Calendar.getInstance() + cal.time = startDate + cal.add(Calendar.DAY_OF_MONTH, day - 1) + return cal.time + } val readingsDesc: String get() { diff --git a/app/src/main/res/layout/reading_day_bar_box.xml b/app/src/main/res/layout/reading_day_bar_box.xml index c843738734..cf9ead2162 100644 --- a/app/src/main/res/layout/reading_day_bar_box.xml +++ b/app/src/main/res/layout/reading_day_bar_box.xml @@ -1,17 +1,20 @@