Skip to content

Commit

Permalink
api result returns on UI thread(concurrently)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcat97 committed Sep 25, 2021
1 parent f359f2e commit 53bdff2
Show file tree
Hide file tree
Showing 20 changed files with 192 additions and 80 deletions.
17 changes: 0 additions & 17 deletions .idea/deploymentTargetDropDown.xml

This file was deleted.

8 changes: 5 additions & 3 deletions .idea/misc.xml

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

18 changes: 14 additions & 4 deletions QuranApi/src/main/java/media/uqab/quranapi/QuranApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package media.uqab.quranapi

import android.content.Context
import android.util.Log
import androidx.core.content.ContextCompat
import media.uqab.quranapi.database.ApiDatabase
import media.uqab.quranapi.database.SurahInfo

Expand All @@ -18,6 +19,7 @@ import java.nio.charset.StandardCharsets

class QuranApi(context: Context) {
private val dao = ApiDatabase.getInstance(context).apiDao
private val mainExe = ContextCompat.getMainExecutor(context)

companion object {
private const val TAG = "QuranApi"
Expand Down Expand Up @@ -77,7 +79,9 @@ class QuranApi(context: Context) {
fun getVerse(surahNo: Int, verseNo: Int, callback: VerseResultCallback) {
ThreadExecutor.execute {
val content = dao.contentByVerse(surahNo, verseNo)
callback.result(Verse(content))
mainExe.execute {
callback.result(Verse(content))
}
}
}
fun getSurah(surahNo: Int, callback: SurahResultCallback) {
Expand All @@ -89,7 +93,9 @@ class QuranApi(context: Context) {
val surahInfo = dao.surahInfo(surahNo)
val surah = Surah(surahNo, surahInfo.name, surahInfo.nameAr, surahInfo.type,verses)

callback.result(surah)
mainExe.execute {
callback.result(surah)
}
}
}

Expand All @@ -99,7 +105,9 @@ class QuranApi(context: Context) {
val verse: MutableList<Verse> = mutableListOf()
content.forEach { verse.add(Verse(it)) }

callback.result(listOf(Page(pageNo, verse)))
mainExe.execute {
callback.result(listOf(Page(pageNo, verse)))
}
}
}
fun getBySurah(surahNo: Int, pageCallback: PageCallback) {
Expand All @@ -118,7 +126,9 @@ class QuranApi(context: Context) {
pages.add(Page(num, verses))
}

pageCallback.result(pages)
mainExe.execute {
pageCallback.result(pages)
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions QuranApi/version.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Mon Sep 13 08:04:46 BDT 2021
#Sat Sep 25 10:10:45 BDT 2021
MAJOR=0
MINOR=4
VERSION_CODE=414
PATCH=14
VERSION_CODE=428
PATCH=28
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ dependencies {
You can use as you wish. Here is a sample usage shown

```kotlin
val api = QuranApi(this)
val recyclerView = findViewById<RecyclerView>(R.id.textView)
val adapter = Adapter()
recyclerView.adapter = adapter

api.getSurah(2) {
runOnUiThread {
adapter.submitList(it.verses)
}
}
val api = QuranApi(this)
api.getSurah(2) { adapter.submitList(it.verses) }
```

A fully functional app source is included in this repo. For more detail see the [How to](wiki/howTo.md)
Expand Down
15 changes: 6 additions & 9 deletions app/src/main/java/media/uqab/apidemo/FragmentPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,20 @@ class FragmentPage(
val pageAdapter = PageAdapter()
binding.recyclerView.adapter = pageAdapter
binding.recyclerView.layoutManager = LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false)
binding.backButton.setOnClickListener { parentFragmentManager.popBackStack() }

if (pageNo == -1001 && surahNo != -1001) {
requireActivity().title = QuranApi.getSurahInfo(surahNo).name
binding.toolbar.title = "${surahNo}. ${QuranApi.getSurahInfo(surahNo).name}"
api.getBySurah(surahNo) {
Log.d(TAG, "onViewCreated: ${it.size}")
Thread.sleep(1) // to remove glitch
requireActivity().runOnUiThread {
pageAdapter.submitPage(it)
}
// Thread.sleep(1) // to remove glitch
pageAdapter.submitPage(it)
}
}
else if (pageNo != -1001 && surahNo == -1001) {
api.getByPage(pageNo) {
Thread.sleep(1) // to remove glitch
requireActivity().runOnUiThread {
pageAdapter.submitPage(it)
}
// Thread.sleep(1) // to remove glitch
pageAdapter.submitPage(it)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/media/uqab/apidemo/VerseAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class VerseAdapter: RecyclerView.Adapter<VerseAdapter.AyahHolder>() {
binding.basmalah.visibility = View.GONE
}
9 -> {
binding.taAudh.visibility = View.GONE
binding.taAudh.visibility = View.VISIBLE
binding.basmalah.visibility = View.GONE
}
else -> {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/button_circle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:height="16dp" android:width="16dp"/>
<padding android:bottom="2dp" android:top="2dp"
android:left="2dp" android:right="2dp"/>
<stroke android:width="1dp" android:color="?attr/colorOnPrimary"/>
<solid android:color="@android:color/transparent"/>
</shape>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/button_circle_ripple.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorPrimary">
<item android:drawable="@drawable/button_circle"/>
</ripple>
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_arrow_right_alt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal"
android:autoMirrored="true">
<path
android:fillColor="@android:color/white"
android:pathData="M16.01,11H4v2h12.01v3L20,12l-3.99,-4z"/>
</vector>
39 changes: 37 additions & 2 deletions app/src/main/res/layout/fragment_page.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_arrow_right_alt"
android:rotationY="180"
app:tint="?attr/colorOnPrimary"
android:padding="4dp"
android:layout_marginStart="8dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="@drawable/button_circle_ripple"
android:contentDescription="@string/back_button" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="?attr/colorOnPrimary"
app:title="12. Suratul Yusuf"
android:layout_toEndOf="@id/backButton"/>
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
android:layout_below="@id/appbarLayout"
tools:listitem="@layout/item_page"/>

</RelativeLayout>
38 changes: 33 additions & 5 deletions app/src/main/res/layout/fragment_surah.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_arrow_right_alt"
android:rotationY="180"
app:tint="?attr/colorOnPrimary"
android:padding="4dp"
android:layout_marginStart="8dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="@drawable/button_circle_ripple"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="?attr/colorOnPrimary"
app:title="12. Suratul Yusuf"
android:layout_toEndOf="@id/backButton"/>
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
android:layout_below="@id/appbarLayout">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/layout/fragment_surah_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="?attr/colorOnPrimary"
app:title="Quran Api"/>
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/item_surah"
android:layout_below="@id/appbarLayout"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</RelativeLayout>
10 changes: 5 additions & 5 deletions app/src/main/res/layout/item_ayah.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@
android:text="Suratul Fatiha"
android:textSize="40sp"
android:textColor="@color/black"
android:fontFamily="@font/pdms_islamic"
android:fontFamily="@font/amiri"
android:textAlignment="center"/>
<TextView
android:id="@+id/ta_audh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ta_audh"
android:textSize="40sp"
android:textSize="32sp"
android:textColor="@color/black"
android:fontFamily="@font/pdms_islamic"
android:fontFamily="@font/amiri"
android:textAlignment="center"/>
<TextView
android:id="@+id/basmalah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/basmalah"
android:textSize="40sp"
android:textSize="32sp"
android:textColor="@color/black"
android:fontFamily="@font/pdms_islamic"
android:fontFamily="@font/amiri"
android:textAlignment="center"/>

</LinearLayout>
Expand Down
Loading

0 comments on commit 53bdff2

Please sign in to comment.