From 33d5ab76347d80bb8c59728d8e75d63775c152dc Mon Sep 17 00:00:00 2001 From: timbze Date: Mon, 14 Feb 2022 07:50:36 -0600 Subject: [PATCH 1/5] Start adding horizontal days scrollbar in DailyReading --- .../android/control/ApplicationComponent.kt | 2 - .../view/activity/readingplan/DailyReading.kt | 35 +++++------- .../readingplan/DailyReadingDayBarAdapter.kt | 54 +++++++++++++++++++ .../activity/readingplan/model/DayBarItem.kt | 36 +++++++++++++ .../main/res/layout/reading_day_bar_box.xml | 22 ++++++++ .../main/res/layout/reading_plan_one_day.xml | 20 +++++-- 6 files changed, 140 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/net/bible/android/view/activity/readingplan/DailyReadingDayBarAdapter.kt create mode 100644 app/src/main/java/net/bible/android/view/activity/readingplan/model/DayBarItem.kt create mode 100644 app/src/main/res/layout/reading_day_bar_box.xml diff --git a/app/src/main/java/net/bible/android/control/ApplicationComponent.kt b/app/src/main/java/net/bible/android/control/ApplicationComponent.kt index 69df8874db..b2d7754934 100644 --- a/app/src/main/java/net/bible/android/control/ApplicationComponent.kt +++ b/app/src/main/java/net/bible/android/control/ApplicationComponent.kt @@ -28,7 +28,6 @@ import net.bible.android.control.readingplan.ReadingPlanControl import net.bible.android.control.search.SearchControl import net.bible.android.control.speak.SpeakControl import net.bible.android.control.versification.BibleTraverser -import net.bible.android.view.activity.readingplan.actionbar.ReadingPlanActionBarManager import net.bible.android.view.activity.search.searchresultsactionbar.SearchResultsActionBarManager import net.bible.android.view.activity.speak.actionbarbuttons.SpeakActionBarButton import net.bible.android.view.activity.speak.actionbarbuttons.SpeakStopActionBarButton @@ -66,6 +65,5 @@ interface ApplicationComponent { fun speakActionBarButton(): SpeakActionBarButton fun speakStopActionBarButton(): SpeakStopActionBarButton - fun readingPlanActionBarManager(): ReadingPlanActionBarManager fun searchResultsActionBarManager(): SearchResultsActionBarManager } 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 94981dbfb2..10ed8a2ac5 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 @@ -39,7 +39,6 @@ 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.actionbar.ReadingPlanActionBarManager import net.bible.service.common.CommonUtils import net.bible.service.db.ReadingPlansUpdatedViaSyncEvent import net.bible.service.readingplan.OneDaysReadingsDto @@ -50,11 +49,7 @@ import java.util.Calendar import javax.inject.Inject -/** Allow user to enter search criteria - * - * @author Martin Denham [mjdenham at gmail dot com] - */ -class DailyReading : CustomTitlebarActivityBase(R.menu.reading_plan) { +class DailyReading : CustomTitlebarActivityBase() { private lateinit var binding: ReadingPlanOneDayBinding @@ -67,7 +62,6 @@ class DailyReading : CustomTitlebarActivityBase(R.menu.reading_plan) { private lateinit var readingsDto: OneDaysReadingsDto @Inject lateinit var readingPlanControl: ReadingPlanControl - @Inject lateinit var readingPlanActionBarManager: ReadingPlanActionBarManager private var readingStatus: ReadingStatus? = null private val getReadingStatus: ReadingStatus @@ -88,8 +82,6 @@ class DailyReading : CustomTitlebarActivityBase(R.menu.reading_plan) { binding = ReadingPlanOneDayBinding.inflate(layoutInflater) setContentView(binding.root) - super.setActionBarManager(readingPlanActionBarManager) - if (!readingPlanControl.isReadingPlanSelected || !readingPlanControl.currentPlanExists) { val intent = Intent(this, ReadingPlanSelectorList::class.java) selectReadingPlan.launch(intent) @@ -131,8 +123,6 @@ class DailyReading : CustomTitlebarActivityBase(R.menu.reading_plan) { planCodeLoaded = readingPlanControl.currentPlanCode - readingPlanActionBarManager.updateButtons() - // get readings for chosen day readingsDto = readingPlanControl.getDaysReading(dayLoaded) @@ -230,10 +220,19 @@ class DailyReading : CustomTitlebarActivityBase(R.menu.reading_plan) { } override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.reading_plan, menu) MenuCompat.setGroupDividerEnabled(menu, true) return super.onCreateOptionsMenu(menu) } + override fun onPrepareOptionsMenu(menu: Menu): Boolean { + if (::readingsDto.isInitialized && readingsDto.isDateBasedPlan) { + menu.findItem(R.id.setCurrentDay).isVisible = false + menu.findItem(R.id.setStartDate).isVisible = false + } + return super.onPrepareOptionsMenu(menu) + } + /** user pressed read button by 1 reading */ private fun onRead(readingNo: Int) { @@ -330,14 +329,6 @@ class DailyReading : CustomTitlebarActivityBase(R.menu.reading_plan) { startActivity(intent) } - override fun onPrepareOptionsMenu(menu: Menu): Boolean { - if (::readingsDto.isInitialized && readingsDto.isDateBasedPlan) { - menu.findItem(R.id.setCurrentDay).isVisible = false - menu.findItem(R.id.setStartDate).isVisible = false - } - return super.onPrepareOptionsMenu(menu) - } - /** * on Click handlers */ @@ -441,7 +432,7 @@ class DailyReading : CustomTitlebarActivityBase(R.menu.reading_plan) { } } - val installZipLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { + private val installZipLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { // TODO load imported plan if result is OK // still need to set up "InstallZip" to return reading plan fileName (code) } @@ -495,7 +486,7 @@ class DailyReading : CustomTitlebarActivityBase(R.menu.reading_plan) { val planDescription: String ) - val PLAN = "net.bible.android.view.activity.readingplan.Plan" - val DAY = "net.bible.android.view.activity.readingplan.Day" + const val PLAN = "net.bible.android.view.activity.readingplan.Plan" + const val DAY = "net.bible.android.view.activity.readingplan.Day" } } 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 new file mode 100644 index 0000000000..916e2465fb --- /dev/null +++ b/app/src/main/java/net/bible/android/view/activity/readingplan/DailyReadingDayBarAdapter.kt @@ -0,0 +1,54 @@ +/* + * 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 + +import android.view.ViewGroup +import androidx.recyclerview.widget.DiffUtil +import androidx.recyclerview.widget.ListAdapter +import androidx.recyclerview.widget.RecyclerView +import net.bible.android.activity.databinding.ReadingDayBarBoxBinding +import net.bible.android.view.activity.readingplan.model.DayBarItem + +class DailyReadingDayBarAdapter : ListAdapter(DIFF_CALLBACK) { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + TODO("Not yet implemented") + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + TODO("Not yet implemented") + } + + inner class ViewHolder(val binding: ReadingDayBarBoxBinding) : RecyclerView.ViewHolder(binding.root) + + companion object { + const val TAG = "DailyReadingDayBarAdapt" + val DIFF_CALLBACK: DiffUtil.ItemCallback = object: DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: DayBarItem, newItem: DayBarItem):Boolean { + // User properties may have changed if reloaded from the DB, but ID is fixed + return oldItem.dayNumber == newItem.dayNumber + } + override fun areContentsTheSame(oldItem: DayBarItem, newItem: DayBarItem):Boolean { + // NOTE: if you use equals, your object must properly override Object#equals() + // Incorrectly returning false here will result in too many animations. + return oldItem == newItem + } + } + } +} 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 new file mode 100644 index 0000000000..12b73a98b2 --- /dev/null +++ b/app/src/main/java/net/bible/android/view/activity/readingplan/model/DayBarItem.kt @@ -0,0 +1,36 @@ +package net.bible.android.view.activity.readingplan.model + +import java.util.Date + +data class DayBarItem ( + val dayNumber: Int, + val date: Date, + /** This day's readings are being shown in daily reading */ + var dayActive: Boolean, + var dayReadPartial: Boolean, + var dayReadComplete: Boolean, +) { + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as DayBarItem + + if (dayNumber != other.dayNumber) return false + if (date != other.date) return false + if (dayActive != other.dayActive) return false + if (dayReadPartial != other.dayReadPartial) return false + if (dayReadComplete != other.dayReadComplete) return false + + return true + } + override fun hashCode(): Int { + var result = dayNumber + result = 31 * result + date.hashCode() + result = 31 * result + dayActive.hashCode() + result = 31 * result + dayReadPartial.hashCode() + result = 31 * result + dayReadComplete.hashCode() + return result + } +} diff --git a/app/src/main/res/layout/reading_day_bar_box.xml b/app/src/main/res/layout/reading_day_bar_box.xml new file mode 100644 index 0000000000..c843738734 --- /dev/null +++ b/app/src/main/res/layout/reading_day_bar_box.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/app/src/main/res/layout/reading_plan_one_day.xml b/app/src/main/res/layout/reading_plan_one_day.xml index 4bb3128dd9..18e9043a18 100644 --- a/app/src/main/res/layout/reading_plan_one_day.xml +++ b/app/src/main/res/layout/reading_plan_one_day.xml @@ -21,10 +21,11 @@ @author Martin Denham [mjdenham at gmail dot com] --> + xmlns:tools="http://schemas.android.com/tools" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + + From 8a672c67a1e2d9bd6b9eec720a3d0352364403d9 Mon Sep 17 00:00:00 2001 From: timbze Date: Mon, 1 Aug 2022 07:22:17 -0600 Subject: [PATCH 2/5] 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 d0f9f4343e..3f6816d538 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 @@ -25,6 +25,7 @@ import net.bible.android.control.event.passage.BeforeCurrentPageChangeEvent import net.bible.android.control.page.CurrentPageManager import net.bible.android.control.page.window.WindowControl 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.ReadingPlansUpdatedViaSyncEvent import net.bible.service.db.readingplan.ReadingPlanRepository @@ -102,6 +103,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 10ed8a2ac5..860d5c8148 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 @@ -28,6 +28,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.db.ReadingPlansUpdatedViaSyncEvent import net.bible.service.readingplan.OneDaysReadingsDto @@ -211,6 +215,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) @@ -219,6 +225,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 a003a19ada..074b376223 100644 --- a/app/src/main/java/net/bible/service/readingplan/OneDaysReadingsDto.kt +++ b/app/src/main/java/net/bible/service/readingplan/OneDaysReadingsDto.kt @@ -40,9 +40,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() @@ -54,22 +62,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 @@ Date: Thu, 24 Aug 2023 18:09:35 -0600 Subject: [PATCH 3/5] Clicking day loads day readings --- .../view/activity/readingplan/DailyReading.kt | 16 ++++++++++++---- .../readingplan/DailyReadingDayBarAdapter.kt | 9 +++++++-- app/src/main/res/layout/reading_day_bar_box.xml | 2 ++ app/src/main/res/layout/reading_plan_one_day.xml | 2 ++ 4 files changed, 23 insertions(+), 6 deletions(-) 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 860d5c8148..5d8ac53bf1 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 @@ -93,6 +93,7 @@ class DailyReading : CustomTitlebarActivityBase() { } loadDailyReading(null, null) + setupRecycler() ABEventBus.register(this) } @@ -215,8 +216,6 @@ class DailyReading : CustomTitlebarActivityBase() { } // end All - setupRecycler() - Log.i(TAG, "Finished displaying Reading view") } catch (e: Exception) { Log.e(TAG, "Error showing daily readings", e) @@ -228,12 +227,21 @@ class DailyReading : CustomTitlebarActivityBase() { private lateinit var viewAdapter: DailyReadingDayBarAdapter private fun setupRecycler() { val days = readingPlanControl.currentPlansReadingDayBarItems - viewAdapter = DailyReadingDayBarAdapter() + viewAdapter = DailyReadingDayBarAdapter(object : OnItemClickListener { + override fun onItemClick(item: DayBarItem) { + days.forEach { d -> if (d.dayActive) { + d.dayActive = false + viewAdapter.notifyItemChanged(days.indexOf(d)) + } } + item.dayActive = true + loadDailyReading(planCodeLoaded, item.dayNumber) + viewAdapter.notifyItemChanged(days.indexOf(item)) + } + }) viewAdapter.submitList(days) binding.daysRecycler.apply { layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false) adapter = viewAdapter - addItemDecoration(DividerItemDecoration(context, LinearLayoutManager.HORIZONTAL)) } } 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 3ea9d8e2d1..239a35c167 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 @@ -30,8 +30,10 @@ import net.bible.service.common.CommonUtils.getResourceColor import java.text.SimpleDateFormat import java.util.Locale -class DailyReadingDayBarAdapter : ListAdapter(DIFF_CALLBACK) { - +interface OnItemClickListener { + fun onItemClick(item: DayBarItem) +} +class DailyReadingDayBarAdapter(private val itemClickListener: OnItemClickListener) : ListAdapter(DIFF_CALLBACK) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val binding = ReadingDayBarBoxBinding.inflate(LayoutInflater.from(parent.context), parent, false) return ViewHolder(binding) @@ -39,6 +41,9 @@ class DailyReadingDayBarAdapter : ListAdapter From 0a1b2c3d48cd47b275128504e8e39f10504deed0 Mon Sep 17 00:00:00 2001 From: Timmy Braun Date: Tue, 10 Oct 2023 07:17:33 -0600 Subject: [PATCH 4/5] Remove deprecated ReadingPlanDatabaseDefinition (was only needed for db migration 5-6 which is very old now) --- ...EPRECATED_ReadingPlanDatabaseDefinition.kt | 185 ------------------ .../service/db/oldmigrations/OldMigrations.kt | 8 - 2 files changed, 193 deletions(-) delete mode 100644 app/src/main/java/net/bible/service/db/oldmigrations/DEPRECATED_ReadingPlanDatabaseDefinition.kt diff --git a/app/src/main/java/net/bible/service/db/oldmigrations/DEPRECATED_ReadingPlanDatabaseDefinition.kt b/app/src/main/java/net/bible/service/db/oldmigrations/DEPRECATED_ReadingPlanDatabaseDefinition.kt deleted file mode 100644 index 343ac0394a..0000000000 --- a/app/src/main/java/net/bible/service/db/oldmigrations/DEPRECATED_ReadingPlanDatabaseDefinition.kt +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Copyright (c) 2020-2023 Martin Denham, Tuomas Airaksinen and the AndBible contributors. - * - * This file is part of AndBible: Bible Study (http://github.com/AndBible/and-bible). - * - * AndBible 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. - * - * AndBible 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 AndBible. - * If not, see http://www.gnu.org/licenses/. - */ - -package net.bible.service.db.oldmigrations - -import android.content.ContentValues -import io.requery.android.database.sqlite.SQLiteDatabase.CONFLICT_FAIL -import android.provider.BaseColumns -import android.util.Log -import androidx.sqlite.db.SupportSQLiteDatabase -import net.bible.android.control.readingplan.ReadingStatus -import net.bible.service.common.CommonUtils -import net.bible.service.readingplan.ReadingPlanTextFileDao -import java.lang.Exception -import kotlin.collections.ArrayList -import kotlin.math.max - -/** @author Timmy Braun [tim.bze at gmail dot com] (Oct. 21, 2019) - * UPDATE 2020-04-04 TB - The only reason this class continues to exist is for - * old versions of AB with db version 5 or less. MIGRATION_5_6 is the only - * place that uses this - - */ -object ReadingPlanDatabaseDefinition { - - /** Table to keep track of plan start date and current day progress - */ - object ReadingPlan : BaseColumns { - const val TABLE_NAME = "readingplan" - const val COLUMN_ID = BaseColumns._ID - const val COLUMN_PLAN_CODE = "plan_code" - const val COLUMN_PLAN_START_DATE = "plan_start_date" - const val COLUMN_PLAN_CURRENT_DAY = "plan_current_day" - } - - /** Table to keep track of which chapters have been read - */ - object ReadingPlanStatus : BaseColumns { - const val TABLE_NAME = "readingplan_status" - const val COLUMN_ID = BaseColumns._ID - const val COLUMN_PLAN_CODE = "plan_code" - const val COLUMN_PLAN_DAY = "plan_day" - const val COLUMN_READING_STATUS = "reading_status" - } -} - -class ReadingPlanDatabaseOperations { - companion object { - val instance = ReadingPlanDatabaseOperations() - } - - private val TAG = "ReadingPlanDbOps" - - private val readingPlan = ReadingPlanDatabaseDefinition.ReadingPlan - private val SQL_CREATE_READING_PLAN = readingPlan.run { - """ - CREATE TABLE $TABLE_NAME ( - $COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT, - $COLUMN_PLAN_CODE TEXT NOT NULL, - $COLUMN_PLAN_START_DATE INTEGER NOT NULL, - $COLUMN_PLAN_CURRENT_DAY INTEGER NOT NULL DEFAULT 1 - ); - """ - } - - private val readingPlanStatus = ReadingPlanDatabaseDefinition.ReadingPlanStatus - private val SQL_CREATE_READING_PLAN_STATUS = readingPlanStatus.run { - """ - CREATE TABLE $TABLE_NAME ( - $COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT, - $COLUMN_PLAN_CODE TEXT NOT NULL, - $COLUMN_PLAN_DAY INTEGER NOT NULL, - $COLUMN_READING_STATUS TEXT NOT NULL - ); - CREATE INDEX code_day ON $TABLE_NAME($COLUMN_PLAN_CODE,$COLUMN_PLAN_DAY); - """ - } - - fun onCreate(db: SupportSQLiteDatabase) { - - try { - Log.i(TAG, "Creating table ${ReadingPlanDatabaseDefinition.ReadingPlan.TABLE_NAME}") - db.execSQL(SQL_CREATE_READING_PLAN) - } catch (e: Exception) { - Log.e(TAG, "Error creating table ${ReadingPlanDatabaseDefinition.ReadingPlan.TABLE_NAME}") - } - - try { - Log.i(TAG, "Creating table ${ReadingPlanDatabaseDefinition.ReadingPlanStatus.TABLE_NAME}") - db.execSQL(SQL_CREATE_READING_PLAN_STATUS) - } catch (e: Exception) { - Log.e(TAG, "Error creating table ${ReadingPlanDatabaseDefinition.ReadingPlanStatus.TABLE_NAME}") - } - } - - fun migratePrefsToDatabase(db: SupportSQLiteDatabase) { - Log.i(TAG, "Now importing reading plan preferences from shared preferences to database") - try { - val DAY_EXT = "_day" - val START_EXT = "_start" - val readingPlanDao = ReadingPlanTextFileDao() - - val readingPlans: ArrayList = ArrayList(readingPlanDao.internalPlanCodes) - val userPlans = readingPlanDao.userPlanCodes() - if (userPlans != null) readingPlans.addAll(userPlans.toTypedArray()) - - val prefs = CommonUtils.realSharedPreferences - for (planCode in readingPlans) { - Log.i(TAG, "Importing status for plan $planCode") - val start = prefs.getLong(planCode + START_EXT, 0) - var day = prefs.getInt(planCode + DAY_EXT, 0) - val values = ContentValues().apply { put(ReadingPlanDatabaseDefinition.ReadingPlan.COLUMN_PLAN_CODE, planCode) } - if (start > 0L) { - values.put(ReadingPlanDatabaseDefinition.ReadingPlan.COLUMN_PLAN_START_DATE, start) - day = max(day, 1) - } - if (day > 0) values.put(ReadingPlanDatabaseDefinition.ReadingPlan.COLUMN_PLAN_CURRENT_DAY, day) - - if ((start > 0L || day > 0) && db.insert(ReadingPlanDatabaseDefinition.ReadingPlan.TABLE_NAME, CONFLICT_FAIL, values) < 0) - Log.e(TAG, "Error inserting start date and current day to db for plan $planCode") - - val prefKey = "${planCode}_$day" - if (prefs.contains(prefKey)) { - val prefDayStatus = prefs.getString(prefKey, "") - if (!prefDayStatus.isNullOrEmpty()) { - enterStatusToDb(prefDayStatus, planCode, day, db) - } - } - - prefs.edit() - .remove(planCode + START_EXT) - .remove(planCode + DAY_EXT) - .remove(prefKey) - .apply() - - // find other days that have reading status in shared preferences - for ((key, value) in prefs.all) { - if (key.contains((planCode + "_[0-9]{1,3}$").toRegex()) && value.toString().contains("^[0-1]*$".toRegex())) { - val day = "(?<=_)[0-9]{1,3}$".toRegex().find(key)?.value?.toIntOrNull() - day ?: continue - enterStatusToDb(value.toString(), planCode, day, db) - - prefs.edit() - .remove(key) - .apply() - } - } - } - } catch (e: Exception) { - Log.e(TAG, "Error migrating readingplans from preferences to database!") - } - } - - private fun enterStatusToDb(prefDayStatus: String, planCode: String, day: Int, db: SupportSQLiteDatabase) { - val status = ReadingStatus(planCode, day, prefDayStatus.length) - for (i in prefDayStatus.indices) { - val isRead = prefDayStatus[i].toString().toInt().toBoolean() - status.setStatus(i+1, isRead,false - ) - } - val statusValues = ContentValues().apply { - put(ReadingPlanDatabaseDefinition.ReadingPlanStatus.COLUMN_READING_STATUS, status.toString()) - put(ReadingPlanDatabaseDefinition.ReadingPlanStatus.COLUMN_PLAN_DAY, day) - put(ReadingPlanDatabaseDefinition.ReadingPlanStatus.COLUMN_PLAN_CODE, planCode) - } - if (db.insert(ReadingPlanDatabaseDefinition.ReadingPlanStatus.TABLE_NAME, CONFLICT_FAIL, statusValues) < 0) - Log.e(TAG, "Error inserting reading status to db for plan $planCode day #$day") - } - - private fun Int.toBoolean() = this > 0 -} diff --git a/app/src/main/java/net/bible/service/db/oldmigrations/OldMigrations.kt b/app/src/main/java/net/bible/service/db/oldmigrations/OldMigrations.kt index 67c9f98561..8578d0aa9c 100644 --- a/app/src/main/java/net/bible/service/db/oldmigrations/OldMigrations.kt +++ b/app/src/main/java/net/bible/service/db/oldmigrations/OldMigrations.kt @@ -150,15 +150,7 @@ private val MIGRATION_53_54_booleanSettings = object : Migration(53, 54) { } } -private val MIGRATION_5_6 = object : Migration(5, 6) { - override fun doMigrate(db: SupportSQLiteDatabase) { - ReadingPlanDatabaseOperations.instance.onCreate(db) - ReadingPlanDatabaseOperations.instance.migratePrefsToDatabase(db) - } -} - val oldMigrations = arrayOf( - MIGRATION_5_6, MIGRATION_37_38_MyNotes_To_Bookmarks, MIGRATION_53_54_booleanSettings, ) From 91466e8328f24ea7827e6b338f6e406e11fa264b Mon Sep 17 00:00:00 2001 From: Timmy Braun Date: Tue, 10 Oct 2023 07:18:24 -0600 Subject: [PATCH 5/5] Remove unused parameter --- .../net/bible/android/control/readingplan/ReadingStatus.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/net/bible/android/control/readingplan/ReadingStatus.kt b/app/src/main/java/net/bible/android/control/readingplan/ReadingStatus.kt index f1a879ae03..408e605b53 100644 --- a/app/src/main/java/net/bible/android/control/readingplan/ReadingStatus.kt +++ b/app/src/main/java/net/bible/android/control/readingplan/ReadingStatus.kt @@ -72,7 +72,7 @@ open class ReadingStatus(val planCode: String, val day: Int, private val numRead setStatus(readingNo, false) } - fun setStatus(readingNo: Int, read: Boolean, saveStatus: Boolean = true) { + private fun setStatus(readingNo: Int, read: Boolean) { val chapterRead = status.chapterReadArray.find { it.readingNumber == readingNo } if (chapterRead == null) { status.chapterReadArray.add(ChapterRead(readingNo, read)) @@ -82,7 +82,7 @@ open class ReadingStatus(val planCode: String, val day: Int, private val numRead } status.chapterReadArray.sortBy { it.readingNumber } - if (saveStatus) saveStatus() + saveStatus() } open fun isRead(readingNo: Int): Boolean {