Skip to content

Commit 43f9422

Browse files
authored
Remove DataBinding from the Test App (#22)
1 parent 3670f7c commit 43f9422

File tree

11 files changed

+168
-232
lines changed

11 files changed

+168
-232
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ buildscript {
1616
maven { url "https://s3.amazonaws.com/repo.commonsware.com" }
1717
}
1818
dependencies {
19-
classpath 'com.android.tools.build:gradle:7.0.2'
19+
classpath 'com.android.tools.build:gradle:7.0.3'
2020
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
2121
}
2222
}

test-app/build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ android {
5151
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
5252
}
5353
buildFeatures {
54-
dataBinding true
5554
viewBinding true
5655
}
5756
buildTypes {
@@ -89,7 +88,7 @@ dependencies {
8988
implementation "androidx.appcompat:appcompat:1.3.1"
9089
implementation "androidx.browser:browser:1.3.0"
9190
implementation "androidx.cardview:cardview:1.0.0"
92-
implementation "androidx.constraintlayout:constraintlayout:2.1.0"
91+
implementation "androidx.constraintlayout:constraintlayout:2.1.1"
9392
implementation "androidx.fragment:fragment-ktx:1.3.6"
9493
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
9594
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
@@ -114,7 +113,7 @@ dependencies {
114113
//noinspection GradleDependency
115114
implementation "com.squareup.picasso:picasso:2.5.2"
116115
implementation "joda-time:joda-time:2.10.10"
117-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0"
116+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"
118117
// AM NOTE: needs to stay this version for now (June 24,2020)
119118
//noinspection GradleDependency
120119
implementation 'org.jsoup:jsoup:1.13.1'

test-app/src/main/java/org/readium/r2/testapp/bookshelf/BookshelfAdapter.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ package org.readium.r2.testapp.bookshelf
88

99
import android.view.LayoutInflater
1010
import android.view.ViewGroup
11-
import androidx.databinding.DataBindingUtil
1211
import androidx.recyclerview.widget.DiffUtil
1312
import androidx.recyclerview.widget.ListAdapter
1413
import androidx.recyclerview.widget.RecyclerView
14+
import com.squareup.picasso.Picasso
1515
import org.readium.r2.testapp.R
1616
import org.readium.r2.testapp.databinding.ItemRecycleBookBinding
1717
import org.readium.r2.testapp.domain.model.Book
1818
import org.readium.r2.testapp.utils.singleClick
19+
import java.io.File
1920

2021

2122
class BookshelfAdapter(
@@ -28,9 +29,8 @@ class BookshelfAdapter(
2829
viewType: Int
2930
): ViewHolder {
3031
return ViewHolder(
31-
DataBindingUtil.inflate(
32-
LayoutInflater.from(parent.context),
33-
R.layout.item_recycle_book, parent, false
32+
ItemRecycleBookBinding.inflate(
33+
LayoutInflater.from(parent.context), parent, false
3434
)
3535
)
3636
}
@@ -46,7 +46,13 @@ class BookshelfAdapter(
4646
RecyclerView.ViewHolder(binding.root) {
4747

4848
fun bind(book: Book) {
49-
binding.book = book
49+
binding.bookshelfTitleText.text = book.title
50+
val coverImageFile =
51+
File("${binding.root.context?.filesDir?.path}/covers/${book.id}.png")
52+
Picasso.with(binding.root.context)
53+
.load(coverImageFile)
54+
.placeholder(R.drawable.cover)
55+
.into(binding.bookshelfCoverImage)
5056
binding.root.singleClick {
5157
onBookClick(book)
5258
}
@@ -71,9 +77,9 @@ class BookshelfAdapter(
7177
newItem: Book
7278
): Boolean {
7379
return oldItem.title == newItem.title
74-
&& oldItem.href == newItem.href
75-
&& oldItem.author == newItem.author
76-
&& oldItem.identifier == newItem.identifier
80+
&& oldItem.href == newItem.href
81+
&& oldItem.author == newItem.author
82+
&& oldItem.identifier == newItem.identifier
7783
}
7884
}
7985

test-app/src/main/java/org/readium/r2/testapp/bookshelf/BookshelfFragment.kt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@ import android.view.View
1919
import android.view.ViewGroup
2020
import android.webkit.URLUtil
2121
import android.widget.EditText
22-
import android.widget.ImageView
2322
import androidx.activity.result.ActivityResultLauncher
2423
import androidx.activity.result.contract.ActivityResultContracts
2524
import androidx.appcompat.app.AlertDialog
2625
import androidx.core.content.ContextCompat
27-
import androidx.databinding.BindingAdapter
2826
import androidx.fragment.app.Fragment
2927
import androidx.fragment.app.viewModels
3028
import androidx.recyclerview.widget.RecyclerView
3129
import com.google.android.material.dialog.MaterialAlertDialogBuilder
3230
import com.google.android.material.snackbar.Snackbar
33-
import com.squareup.picasso.Picasso
3431
import org.json.JSONObject
3532
import org.readium.r2.shared.extensions.tryOrLog
3633
import org.readium.r2.shared.publication.Locator
@@ -39,7 +36,6 @@ import org.readium.r2.testapp.databinding.FragmentBookshelfBinding
3936
import org.readium.r2.testapp.domain.model.Book
4037
import org.readium.r2.testapp.opds.GridAutoFitLayoutManager
4138
import org.readium.r2.testapp.reader.ReaderContract
42-
import java.io.File
4339

4440

4541
class BookshelfFragment : Fragment() {
@@ -71,7 +67,6 @@ class BookshelfFragment : Fragment() {
7167
_binding = FragmentBookshelfBinding.inflate(
7268
inflater, container, false
7369
)
74-
binding.viewModel = bookshelfViewModel
7570
return binding.root
7671
}
7772

@@ -83,6 +78,7 @@ class BookshelfFragment : Fragment() {
8378
documentPickerLauncher =
8479
registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
8580
uri?.let {
81+
binding.bookshelfProgressBar.visibility = View.VISIBLE
8682
bookshelfViewModel.importPublicationFromUri(it)
8783
}
8884
}
@@ -139,6 +135,7 @@ class BookshelfFragment : Fragment() {
139135
} else {
140136
val url = urlEditText.text.toString()
141137
val uri = Uri.parse(url)
138+
binding.bookshelfProgressBar.visibility = View.VISIBLE
142139
bookshelfViewModel.importPublicationFromUri(uri, url)
143140
urlDialog.dismiss()
144141
}
@@ -165,6 +162,7 @@ class BookshelfFragment : Fragment() {
165162
"Error: " + event.errorMessage
166163
}
167164
}
165+
binding.bookshelfProgressBar.visibility = View.GONE
168166
Snackbar.make(
169167
requireView(),
170168
message,
@@ -266,12 +264,3 @@ class BookshelfFragment : Fragment() {
266264
.show()
267265
}
268266
}
269-
270-
@BindingAdapter("coverImage")
271-
fun loadImage(view: ImageView, bookId: Long?) {
272-
val coverImageFile = File("${view.context?.filesDir?.path}/covers/${bookId}.png")
273-
Picasso.with(view.context)
274-
.load(coverImageFile)
275-
.placeholder(R.drawable.cover)
276-
.into(view)
277-
}

test-app/src/main/java/org/readium/r2/testapp/bookshelf/BookshelfViewModel.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import android.content.Context
1111
import android.graphics.Bitmap
1212
import android.graphics.BitmapFactory
1313
import android.net.Uri
14-
import androidx.databinding.ObservableBoolean
1514
import androidx.lifecycle.AndroidViewModel
1615
import androidx.lifecycle.viewModelScope
1716
import kotlinx.coroutines.Dispatchers
@@ -66,7 +65,6 @@ class BookshelfViewModel(application: Application) : AndroidViewModel(applicatio
6665
)
6766
private var r2Directory: String = R2App.R2DIRECTORY
6867
val channel = EventChannel(Channel<Event>(Channel.BUFFERED), viewModelScope)
69-
val showProgressBar = ObservableBoolean()
7068

7169
val books = repository.books()
7270

@@ -111,7 +109,6 @@ class BookshelfViewModel(application: Application) : AndroidViewModel(applicatio
111109
uri: Uri,
112110
sourceUrl: String? = null
113111
) = viewModelScope.launch {
114-
showProgressBar.set(true)
115112
uri.copyToTempFile(r2Application, r2Directory)
116113
?.let {
117114
importPublication(it, sourceUrl)
@@ -138,7 +135,6 @@ class BookshelfViewModel(application: Application) : AndroidViewModel(applicatio
138135
{
139136
tryOrNull { sourceFile.delete() }
140137
Timber.d(it)
141-
showProgressBar.set(false)
142138
channel.send(Event.ImportPublicationFailed(it.message))
143139
return
144140
}
@@ -154,7 +150,6 @@ class BookshelfViewModel(application: Application) : AndroidViewModel(applicatio
154150
} catch (e: Exception) {
155151
Timber.d(e)
156152
tryOrNull { publicationAsset.file.delete() }
157-
showProgressBar.set(false)
158153
channel.send(Event.UnableToMovePublication)
159154
return
160155
}
@@ -163,7 +158,6 @@ class BookshelfViewModel(application: Application) : AndroidViewModel(applicatio
163158
.onSuccess {
164159
addPublicationToDatabase(libraryAsset.file.path, libraryAsset.mediaType(), it).let { id ->
165160

166-
showProgressBar.set(false)
167161
if (id != -1L)
168162
channel.send(Event.ImportPublicationSuccess)
169163
else
@@ -173,7 +167,6 @@ class BookshelfViewModel(application: Application) : AndroidViewModel(applicatio
173167
.onFailure {
174168
tryOrNull { libraryAsset.file.delete() }
175169
Timber.d(it)
176-
showProgressBar.set(false)
177170
channel.send(Event.ImportPublicationFailed(it.getUserMessage(r2Application)))
178171
}
179172
}

test-app/src/main/java/org/readium/r2/testapp/catalogs/CatalogViewModel.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package org.readium.r2.testapp.catalogs
99
import android.app.Application
1010
import android.graphics.Bitmap
1111
import android.graphics.BitmapFactory
12-
import androidx.databinding.ObservableBoolean
1312
import androidx.lifecycle.AndroidViewModel
1413
import androidx.lifecycle.MutableLiveData
1514
import androidx.lifecycle.viewModelScope
@@ -48,7 +47,6 @@ class CatalogViewModel(application: Application) : AndroidViewModel(application)
4847
val detailChannel = EventChannel(Channel<Event.DetailEvent>(Channel.BUFFERED), viewModelScope)
4948
val eventChannel = EventChannel(Channel<Event.FeedEvent>(Channel.BUFFERED), viewModelScope)
5049
val parseData = MutableLiveData<ParseData>()
51-
val showProgressBar = ObservableBoolean()
5250

5351
fun parseCatalog(catalog: Catalog) = viewModelScope.launch {
5452
var parseRequest: Try<ParseData, Exception>? = null
@@ -74,7 +72,6 @@ class CatalogViewModel(application: Application) : AndroidViewModel(application)
7472
}
7573

7674
fun downloadPublication(publication: Publication) = viewModelScope.launch {
77-
showProgressBar.set(true)
7875
val downloadUrl = getDownloadURL(publication)
7976
val publicationUrl = opdsDownloader.publicationUrl(downloadUrl.toString())
8077
publicationUrl.onSuccess {
@@ -88,8 +85,6 @@ class CatalogViewModel(application: Application) : AndroidViewModel(application)
8885
.onFailure {
8986
detailChannel.send(Event.DetailEvent.ImportPublicationFailed)
9087
}
91-
92-
showProgressBar.set(false)
9388
}
9489

9590
private fun getDownloadURL(publication: Publication): URL? =

test-app/src/main/java/org/readium/r2/testapp/catalogs/PublicationDetailFragment.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import android.os.Bundle
1010
import android.view.LayoutInflater
1111
import android.view.View
1212
import android.view.ViewGroup
13-
import androidx.databinding.DataBindingUtil
1413
import androidx.fragment.app.Fragment
1514
import androidx.fragment.app.viewModels
1615
import com.google.android.material.snackbar.Snackbar
@@ -35,14 +34,11 @@ class PublicationDetailFragment : Fragment() {
3534
inflater: LayoutInflater, container: ViewGroup?,
3635
savedInstanceState: Bundle?
3736
): View {
38-
_binding = DataBindingUtil.inflate(
39-
LayoutInflater.from(context),
40-
R.layout.fragment_publication_detail, container, false
37+
_binding = FragmentPublicationDetailBinding.inflate(
38+
inflater, container, false
4139
)
4240
catalogViewModel.detailChannel.receive(this) { handleEvent(it) }
4341
publication = arguments?.getPublicationOrNull()
44-
binding.publication = publication
45-
binding.viewModel = catalogViewModel
4642
return binding.root
4743
}
4844

@@ -53,8 +49,12 @@ class PublicationDetailFragment : Fragment() {
5349
Picasso.with(requireContext()).load(publication?.images?.first()?.href)
5450
.into(binding.catalogDetailCoverImage)
5551

52+
binding.catalogDetailDescriptionText.text = publication?.metadata?.description
53+
binding.catalogDetailTitleText.text = publication?.metadata?.title
54+
5655
binding.catalogDetailDownloadButton.setOnClickListener {
5756
publication?.let { it1 ->
57+
binding.catalogDetailProgressBar.visibility = View.VISIBLE
5858
catalogViewModel.downloadPublication(
5959
it1
6060
)
@@ -68,6 +68,7 @@ class PublicationDetailFragment : Fragment() {
6868
is CatalogViewModel.Event.DetailEvent.ImportPublicationSuccess -> getString(R.string.import_publication_success)
6969
is CatalogViewModel.Event.DetailEvent.ImportPublicationFailed -> getString(R.string.unable_add_pub_database)
7070
}
71+
binding.catalogDetailProgressBar.visibility = View.GONE
7172
Snackbar.make(
7273
requireView(),
7374
message,
Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,35 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<layout xmlns:android="http://schemas.android.com/apk/res/android">
3-
4-
<data>
5-
6-
<import type="android.view.View" />
7-
8-
<variable
9-
name="viewModel"
10-
type="org.readium.r2.testapp.bookshelf.BookshelfViewModel" />
11-
12-
</data>
13-
14-
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"
15-
xmlns:tools="http://schemas.android.com/tools"
2+
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".bookshelf.BookshelfFragment">
8+
9+
<androidx.recyclerview.widget.RecyclerView
10+
android:id="@+id/bookshelf_bookList"
11+
android:name="org.readium.r2.testapp.bookshelf"
1612
android:layout_width="match_parent"
1713
android:layout_height="match_parent"
18-
tools:context=".bookshelf.BookshelfFragment">
19-
20-
<androidx.recyclerview.widget.RecyclerView
21-
android:id="@+id/bookshelf_bookList"
22-
android:name="org.readium.r2.testapp.bookshelf"
23-
android:layout_width="match_parent"
24-
android:layout_height="match_parent"
25-
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
26-
tools:context="org.readium.r2.testapp.bookshelf"
27-
tools:listitem="@layout/item_recycle_book" />
28-
29-
<ProgressBar
30-
android:id="@+id/bookshelf_progressBar"
31-
style="?android:attr/progressBarStyle"
32-
android:layout_width="wrap_content"
33-
android:layout_height="wrap_content"
34-
android:layout_gravity="center"
35-
android:visibility="@{viewModel.showProgressBar ? View.VISIBLE : View.GONE}" />
36-
37-
<com.google.android.material.floatingactionbutton.FloatingActionButton
38-
android:id="@+id/bookshelf_addBookFab"
39-
android:layout_width="wrap_content"
40-
android:layout_height="wrap_content"
41-
android:layout_gravity="end|bottom"
42-
android:layout_margin="16dp"
43-
android:contentDescription="@string/add_book"
44-
android:src="@drawable/ic_add_white_24dp" />
45-
46-
</androidx.coordinatorlayout.widget.CoordinatorLayout>
47-
</layout>
14+
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
15+
tools:context="org.readium.r2.testapp.bookshelf"
16+
tools:listitem="@layout/item_recycle_book" />
17+
18+
<ProgressBar
19+
android:id="@+id/bookshelf_progressBar"
20+
style="?android:attr/progressBarStyle"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:layout_gravity="center"
24+
android:visibility="gone" />
25+
26+
<com.google.android.material.floatingactionbutton.FloatingActionButton
27+
android:id="@+id/bookshelf_addBookFab"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:layout_gravity="end|bottom"
31+
android:layout_margin="16dp"
32+
android:contentDescription="@string/add_book"
33+
android:src="@drawable/ic_add_white_24dp" />
34+
35+
</androidx.coordinatorlayout.widget.CoordinatorLayout>

0 commit comments

Comments
 (0)