Skip to content

Commit

Permalink
adding ability to search verse, translation within the app #17
Browse files Browse the repository at this point in the history
  • Loading branch information
WirelessAlien committed Dec 22, 2023
1 parent accdf50 commit d91339b
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.wirelessalien.android.bhagavadgita.activity

import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
Expand All @@ -16,13 +17,16 @@ import com.wirelessalien.android.bhagavadgita.utils.Themes
import kotlinx.coroutines.*
import java.io.IOException

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

private lateinit var binding: AllVerseActivityBinding
private var verseList: List<Verse> = emptyList()
private var currentTextSize: Int = 16
private lateinit var searchView: SearchView

private lateinit var adapter: AllVerseAdapter
private lateinit var translationList: Map<Int, Translation>
private lateinit var commentaryList: Map<Int, Commentary>

@DelicateCoroutinesApi
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -33,37 +37,51 @@ class AllVerseActivity: AppCompatActivity() {
binding = AllVerseActivityBinding.inflate(layoutInflater)
setContentView(binding.root)

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

updateAdapterTextSize(currentTextSize, verseList)
// Initialize UI components
initUI()

val translationList = loadTranslations() // Load translations from the respective JSON
val commentaryList = loadCommentaries() // Load commentaries from the respective JSON
// Load translations and commentaries
translationList = loadTranslations()
commentaryList = loadCommentaries()

// Initialize the adapter with translations
val adapter = AllVerseAdapter(verseList, currentTextSize, translationList)
adapter = AllVerseAdapter(verseList, currentTextSize)
binding.verseRecyclerView.adapter = adapter
binding.verseRecyclerView.layoutManager = LinearLayoutManager(this)

// Load the verses asynchronously
loadVersesAsync()

// Set up search functionality
setupSearchView()
}

private fun initUI() {
val sharedPrefTextSize =
getSharedPreferences("text_size_prefs", Context.MODE_PRIVATE)
currentTextSize = sharedPrefTextSize.getInt("text_size", 16)
updateAdapterTextSize(currentTextSize, verseList)
}

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

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

// Update the UI on the main thread
withContext(Dispatchers.Main) {
// Set the chapter details in the UI
binding.verseRecyclerView.layoutManager = LinearLayoutManager(this@AllVerseActivity)
binding.verseRecyclerView.adapter = AllVerseAdapter(verseList, currentTextSize, translationList)

// Hide the ProgressBar once the verses are loaded

binding.verseRecyclerView.adapter = AllVerseAdapter(verseList, currentTextSize)
binding.progressBar.visibility = View.GONE // Hide the ProgressBar once the verses are loaded
}
}
}

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

private fun setupSearchView() {
searchView = binding.searchView

searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
Expand All @@ -72,43 +90,56 @@ class AllVerseActivity: AppCompatActivity() {
}

override fun onQueryTextChange(newText: String?): Boolean {
val verseSearchResult = when {
newText.isNullOrBlank() -> verseList
else -> verseList.filter { verse ->
val titleMatch = verse.title.contains(newText, ignoreCase = true)
val textMatch = verse.text.contains(newText, ignoreCase = true)
val transliterationMatch = verse.transliteration.contains(newText, ignoreCase = true)
val chapterNumberMatch = verse.chapter_number.toString().contains(newText, ignoreCase = true)
val wordMeaningsMatch = verse.word_meanings.contains(newText, ignoreCase = true)

titleMatch || textMatch || transliterationMatch || chapterNumberMatch || wordMeaningsMatch
}
}
// Perform search in the background
searchInBackground(newText)
return true
}
})
}

val translationSearchResult = translationList.values.filter { translation ->
translation.authorName.contains(newText!!, ignoreCase = true) ||
translation.description.contains(newText, ignoreCase = true)
private fun searchInBackground(newText: String?) {
GlobalScope.launch(Dispatchers.IO) {
val verseSearchResult = when {
newText.isNullOrBlank() -> verseList
else -> verseList.filter { verse ->
val titleMatch = verse.title.contains(newText, ignoreCase = true)
val textMatch = verse.text.contains(newText, ignoreCase = true)
val transliterationMatch =
verse.transliteration.contains(newText, ignoreCase = true)
val chapterNumberMatch =
verse.chapter_number.toString().contains(newText, ignoreCase = true)
val wordMeaningsMatch =
verse.word_meanings.contains(newText, ignoreCase = true)

titleMatch || textMatch || transliterationMatch || chapterNumberMatch || wordMeaningsMatch
}
}

val commentarySearchResult = commentaryList.values.filter { commentary ->
commentary.authorName.contains(newText!!, ignoreCase = true) ||
commentary.description.contains(newText, ignoreCase = true)
}
val translationSearchResult = translationList.values.filter { translation ->
translation.authorName.contains(newText!!, ignoreCase = true) ||
translation.description.contains(newText, ignoreCase = true)
}

val commentarySearchResult = commentaryList.values.filter { commentary ->
commentary.authorName.contains(newText!!, ignoreCase = true) ||
commentary.description.contains(newText, ignoreCase = true)
}

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()
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()

// Filter the original verseList based on the verse_ids
verseList.filter { it.verse_id in verseIds }
} else {
emptyList()
}
// Filter the original verseList based on the verse_ids
verseList.filter { it.verse_id in verseIds }
} else {
emptyList()
}

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

private fun loadTranslations(): Map<Int, Translation> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import com.wirelessalien.android.bhagavadgita.utils.Themes
@SuppressLint("CustomSplashScreen")
class SplashActivity : AppCompatActivity() {

private val splashTimeOut: Long = 2000 // 3 seconds
private val splashTimeOut: Long = 2000 // 2 seconds

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.wirelessalien.android.bhagavadgita.activity.VerseDetailActivity
import com.wirelessalien.android.bhagavadgita.data.Translation
import com.wirelessalien.android.bhagavadgita.data.Verse
import com.wirelessalien.android.bhagavadgita.databinding.AllVerseCardviewItemBinding

class AllVerseAdapter(
private var verses: List<Verse>,
private var textSize: Int,
private var translations: Map<Int, Translation>,
) : RecyclerView.Adapter<AllVerseAdapter.AllVerseViewHolder>() {

inner class AllVerseViewHolder(private val binding: AllVerseCardviewItemBinding) :
Expand All @@ -45,11 +43,6 @@ class AllVerseAdapter(
binding.verseTitleTextView.textSize = textSize.toFloat()
binding.verseTextView.textSize = textSize.toFloat()

val translation = translations[verse.verse_id]
translation?.let {
binding.verseDescriptionTextView.text = it.description
}

binding.root.setOnClickListener {
val intent = newIntent(binding.root.context, verse)
binding.root.context.startActivity(intent)
Expand Down
Binary file added app/src/main/res/drawable/slider_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions app/src/main/res/layout/all_verse_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_margin="8dp"
app:defaultQueryHint="Search Anything"
app:iconifiedByDefault="false" />
</com.google.android.material.card.MaterialCardView>

Expand All @@ -27,4 +28,13 @@
android:layout_height="match_parent"
tools:listitem="@layout/all_verse_cardview_item"/>


<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"
android:visibility="gone" />

</LinearLayout>
12 changes: 0 additions & 12 deletions app/src/main/res/layout/all_verse_cardview_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,6 @@
android:textAlignment="center"
android:textSize="14sp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/verseDescriptionTextView"
android:layout_marginTop="10dp"
android:textSize="14sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/verseCommentaryTextView"
android:layout_marginTop="10dp"
android:textSize="14sp"/>
</LinearLayout>

</com.google.android.material.card.MaterialCardView>
14 changes: 11 additions & 3 deletions app/src/main/res/layout/slider_verse_cardview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,31 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Customize this layout as per your design preference -->

<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="15dp"
style="@style/Widget.Material3.CardView.Outlined"
android:layout_margin="10dp"
app:cardElevation="5dp"
app:strokeColor="@color/md_theme_light_primary"
app:strokeWidth="2dp"
app:cardCornerRadius="5dp">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/slider_background"
android:scaleType="centerCrop"
android:alpha="0.3"
tools:ignore="ContentDescription" />

<TextView
android:id="@+id/verseTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAlignment="center"
android:textStyle="bold"
android:autoSizeMaxTextSize="20sp"
android:autoSizeMinTextSize="10dp"
android:autoSizeTextType="uniform"
Expand Down

0 comments on commit d91339b

Please sign in to comment.