Skip to content

Commit

Permalink
v3.3 #28
Browse files Browse the repository at this point in the history
Clickable random verses #22
Progress for verse reading
Improve search experience #17
Select favourite verse
  • Loading branch information
WirelessAlien authored Feb 8, 2024
2 parents d07e0f4 + 06dbaad commit 02b4b2e
Show file tree
Hide file tree
Showing 29 changed files with 873 additions and 449 deletions.
5 changes: 2 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.wirelessalien.android.bhagavadgita"
minSdk 23
targetSdk 33
versionCode 7
versionName "3.2"
versionCode 8
versionName "3.3"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -50,7 +50,11 @@ dependencies {
implementation "androidx.recyclerview:recyclerview:1.3.1"
implementation "androidx.viewpager2:viewpager2:1.1.0-beta02"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1'
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.0"


testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
android:value="" />
</activity>
<activity
android:name=".activity.ChapterDetailActivity"
android:name=".activity.ChapterDetailsActivity"
android:launchMode="singleTop"
android:exported="false">
<meta-data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ class MainActivity : AppCompatActivity() {
startActivity(intent)
}
}

override fun onResume() {
super.onResume()
val adapterC = binding.recyclerView.adapter as? ChapterAdapter
adapterC?.updateProgressData()
adapterC?.notifyDataSetChanged()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main, menu)
return super.onCreateOptionsMenu(menu)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.wirelessalien.android.bhagavadgita.activity

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.BackgroundColorSpan
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
Expand All @@ -17,7 +22,6 @@ import com.wirelessalien.android.bhagavadgita.utils.Themes
import kotlinx.coroutines.*
import java.io.IOException

@OptIn(DelicateCoroutinesApi::class)
class AllVerseActivity : AppCompatActivity() {

private lateinit var binding: AllVerseActivityBinding
Expand Down Expand Up @@ -60,15 +64,15 @@ class AllVerseActivity : AppCompatActivity() {
val sharedPrefTextSize =
getSharedPreferences("text_size_prefs", Context.MODE_PRIVATE)
currentTextSize = sharedPrefTextSize.getInt("text_size", 16)
updateAdapterTextSize(currentTextSize, verseList)
updateAdapterTextSize(currentTextSize, verseList, "", emptyList())
}

private fun loadVersesAsync() {
// Show ProgressBar
binding.progressBar.visibility = View.VISIBLE

// Load verses asynchronously
GlobalScope.launch(Dispatchers.IO) {
lifecycleScope.launch(Dispatchers.IO) {
verseList = loadAllVerses()

// Update the UI on the main thread
Expand Down Expand Up @@ -98,7 +102,7 @@ class AllVerseActivity : AppCompatActivity() {
}

private fun searchInBackground(newText: String?) {
GlobalScope.launch(Dispatchers.IO) {
lifecycleScope.launch(Dispatchers.IO) {
val verseSearchResult = when {
newText.isNullOrBlank() -> verseList
else -> verseList.filter { verse ->
Expand All @@ -125,6 +129,65 @@ class AllVerseActivity : AppCompatActivity() {
commentary.description.contains(newText, ignoreCase = true)
}

val matchedContexts = mutableListOf<String>()

// Add matched contexts for verse titles
verseSearchResult.forEach { verse ->
if (verse.title.contains(newText!!, ignoreCase = true)) {
matchedContexts.add("Match found in verse title: ${verse.title}")
}
}

// Add matched contexts for verse text
verseSearchResult.forEach { verse ->
if (verse.text.contains(newText!!, ignoreCase = true)) {
matchedContexts.add(verse.text)
}
}

// Add matched contexts for chapter numbers
verseSearchResult.forEach { verse ->
if (verse.chapter_number.toString().contains(newText!!, ignoreCase = true)) {
matchedContexts.add("Match found in chapter: ${verse.chapter_number}")
}
}

// Add matched contexts for transliteration
verseSearchResult.forEach { verse ->
if (verse.transliteration.contains(newText!!, ignoreCase = true)) {
matchedContexts.add("Match found in transliteration: ${verse.transliteration}")
}
}

// Add matched contexts for word meanings
verseSearchResult.forEach { verse ->
if (verse.word_meanings.contains(newText!!, ignoreCase = true)) {
matchedContexts.add("Match found in word meanings: ${verse.word_meanings}")
}
}

// Add matched contexts for translations
translationSearchResult.forEach { translation ->
if (translation.authorName.contains(newText!!, ignoreCase = true)) {
matchedContexts.add("Match found in translation author: ${translation.authorName}")
}
if (translation.description.contains(newText, ignoreCase = true)) {
matchedContexts.add("Match found in translation: ${translation.description}")
}
}

// Add matched contexts for commentaries
commentarySearchResult.forEach { commentary ->
if (commentary.authorName.contains(newText!!, ignoreCase = true)) {
matchedContexts.add("Match found in commentary author: ${commentary.authorName}")
}
if (commentary.description.contains(newText, ignoreCase = true)) {
matchedContexts.add("Match found in commentary: ${commentary.description}")
}
}

val matchedText = if (!newText.isNullOrBlank()) newText else null

val filteredList = if (verseSearchResult.isNotEmpty() || translationSearchResult.isNotEmpty() || commentarySearchResult.isNotEmpty()) {
// Combine the results and get unique verse_ids
val verseIds = (verseSearchResult.map { it.verse_id } + translationSearchResult.map { it.verse_id } + commentarySearchResult.map { it.verse_id }).toSet()
Expand All @@ -135,13 +198,33 @@ class AllVerseActivity : AppCompatActivity() {
emptyList()
}

// Update the UI on the main thread
val highlightedMatchedContexts = matchedContexts.map { context ->
val spannableString = SpannableString(context)
val startIndex = context.indexOf(newText!!, ignoreCase = true)
if (startIndex != -1) {
val endIndex = startIndex + newText.length
spannableString.setSpan(
BackgroundColorSpan(Color.YELLOW), // Highlight color
startIndex, endIndex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
spannableString
}

// Update UI on the main thread
withContext(Dispatchers.Main) {
updateAdapterTextSize(currentTextSize, filteredList)
updateAdapterTextSize(currentTextSize, filteredList, matchedText, highlightedMatchedContexts)
}
}
}

private fun updateAdapterTextSize(newSize: Int, filteredList: List<Verse>, matchedText: String?, matchedContexts: List<SpannableString>) {
val recyclerViewC = binding.verseRecyclerView
val adapterC = recyclerViewC.adapter as? AllVerseAdapter
adapterC?.updateTextSize(newSize, filteredList, matchedText, matchedContexts)
}

private fun loadTranslations(): Map<Int, Translation> {
val jsonString = loadJsonFromAsset("translation.json")
val translationListType = object : TypeToken<List<Translation>>() {}.type
Expand Down Expand Up @@ -178,10 +261,4 @@ class AllVerseActivity : AppCompatActivity() {

return Gson().fromJson(jsonString, verseListType)
}

private fun updateAdapterTextSize(newSize: Int, filteredList: List<Verse>) {
val recyclerViewC = binding.verseRecyclerView
val adapterC = recyclerViewC.adapter as? AllVerseAdapter
adapterC?.updateTextSize(newSize, filteredList)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.wirelessalien.android.bhagavadgita.R
import com.wirelessalien.android.bhagavadgita.adapter.VerseAdapter
import com.wirelessalien.android.bhagavadgita.data.Verse
import com.wirelessalien.android.bhagavadgita.databinding.ActivityChapterDetailBinding
import com.wirelessalien.android.bhagavadgita.utils.Themes
import kotlinx.coroutines.*
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.IOException

class ChapterDetailActivity : AppCompatActivity() {
class ChapterDetailsActivity : AppCompatActivity() {

private lateinit var binding: ActivityChapterDetailBinding
private var verseList: List<Verse> = emptyList()
Expand All @@ -60,13 +63,15 @@ class ChapterDetailActivity : AppCompatActivity() {
progressBar = binding.progressBar
updateAdapterTextSize(currentTextSize)
updateTextSize(currentTextSize)
setSupportActionBar(binding.toolbar)

// Retrieve the chapter details from the intent
val chapterNumber = intent.getIntExtra("chapter_number", 0)
val chapterName = intent.getStringExtra("chapter_name")
val chapterNameMeaning = intent.getStringExtra("name_meaning")
val chapterSummary = intent.getStringExtra("chapter_summary")
val chapterSummaryHindi = intent.getStringExtra("chapter_summary_hindi")
val versesCount = intent.getIntExtra("verses_count", 0)

progressBar.visibility = View.VISIBLE

Expand All @@ -82,12 +87,13 @@ class ChapterDetailActivity : AppCompatActivity() {
// Update the UI on the main thread
withContext(Dispatchers.Main) {
// Set the chapter details in the UI
binding.chapterNumberTextView.text = chapterNumber.toString()
binding.chapterNameTextView.text = chapterName
binding.chapterNameMeaningTextView.text = chapterNameMeaning
binding.verseRecyclerView.layoutManager = LinearLayoutManager(this@ChapterDetailActivity)
binding.verseRecyclerView.layoutManager = LinearLayoutManager(this@ChapterDetailsActivity)
binding.verseRecyclerView.adapter = VerseAdapter(verseList, currentTextSize)

supportActionBar?.title = "Chapter $chapterNumber"

// Hide the ProgressBar once the verses are loaded
progressBar.visibility = View.GONE
}
Expand Down Expand Up @@ -121,6 +127,23 @@ class ChapterDetailActivity : AppCompatActivity() {

binding.verseRecyclerView.layoutManager = LinearLayoutManager(this)
binding.verseRecyclerView.adapter = VerseAdapter(verseList, 16)

// Calculate the number of read verses
val sharedPreferences = binding.root.context.getSharedPreferences("read_verses", Context.MODE_PRIVATE)
val readVerses = sharedPreferences.all.keys.count {
it.endsWith("-chapter") && sharedPreferences.getInt(it, 0) == chapterNumber && sharedPreferences.getBoolean(it.removeSuffix("-chapter"), false) }

val progress = (readVerses.toDouble() / versesCount.toDouble()) * 100

binding.progressBarReadCount.progress = progress.toInt()
binding.progressTextView.text = String.format("%.2f%%", progress)
}

override fun onResume() {
super.onResume()
val adapter = binding.verseRecyclerView.adapter as? VerseAdapter
adapter?.updateProgressData()
adapter?.notifyDataSetChanged()
}

private fun getEllipsizedText(text: String, maxLines: Int, maxCharactersPerLine: Int): String {
Expand Down Expand Up @@ -156,7 +179,6 @@ class ChapterDetailActivity : AppCompatActivity() {

currentTextSize = newSize
val textViewList = listOf(
binding.chapterNumberTextView,
binding.chapterNameTextView,
binding.chapterNameMeaningTextView,
binding.chapterSummaryTextView,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package com.wirelessalien.android.bhagavadgita.activity
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -30,6 +31,7 @@ import com.google.gson.reflect.TypeToken
import com.wirelessalien.android.bhagavadgita.adapter.FavouriteVerseAdapter
import com.wirelessalien.android.bhagavadgita.data.FavouriteVerse
import com.wirelessalien.android.bhagavadgita.databinding.ActivityFavouriteBinding
import com.wirelessalien.android.bhagavadgita.utils.Themes

class FavouriteActivity : AppCompatActivity() {

Expand All @@ -39,9 +41,13 @@ class FavouriteActivity : AppCompatActivity() {
private val favoriteList = mutableListOf<FavouriteVerse>()
private lateinit var sharedPreferences: SharedPreferences
private val gson = Gson()
private var currentTextSize: Int = 16 // Default text size

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Themes.loadTheme(this)

binding = ActivityFavouriteBinding.inflate(layoutInflater)
setContentView(binding.root)

Expand All @@ -51,6 +57,9 @@ class FavouriteActivity : AppCompatActivity() {
// Initialize SharedPreferences
sharedPreferences = getSharedPreferences("favorites", Context.MODE_PRIVATE)

val sharedPrefTextSize = getSharedPreferences("text_size_prefs", Context.MODE_PRIVATE)
currentTextSize = sharedPrefTextSize.getInt("text_size", 16) // Get the saved text size

// Load the list of favorite items
loadFavoriteList()

Expand All @@ -73,6 +82,14 @@ class FavouriteActivity : AppCompatActivity() {
val favoriteListType = object : TypeToken<List<FavouriteVerse>>() {}.type
favoriteList.clear()
favoriteList.addAll(gson.fromJson(favoritesJson, favoriteListType))

if (favoriteList.isEmpty()) {
// If the list is empty, show the emptyTextView
binding.emptyTextView.visibility = View.VISIBLE
} else {
// If the list is not empty, hide the emptyTextView
binding.emptyTextView.visibility = View.GONE
}
}

private fun saveFavoriteList() {
Expand Down
Loading

0 comments on commit 02b4b2e

Please sign in to comment.