Skip to content

Commit 9cad8df

Browse files
committed
chore: convert SharedDecksDownloadFragment to ViewBinding
Issue 11116
1 parent 0086523 commit 9cad8df

2 files changed

Lines changed: 54 additions & 51 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/SharedDecksDownloadFragment.kt

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ import android.database.Cursor
2727
import android.os.Bundle
2828
import android.os.Handler
2929
import android.os.Looper
30+
import android.view.LayoutInflater
3031
import android.view.View
32+
import android.view.ViewGroup
3133
import android.webkit.CookieManager
32-
import android.widget.Button
33-
import android.widget.ProgressBar
34-
import android.widget.TextView
3534
import androidx.activity.OnBackPressedCallback
3635
import androidx.annotation.VisibleForTesting
3736
import androidx.appcompat.app.AlertDialog
@@ -40,6 +39,7 @@ import androidx.core.content.FileProvider
4039
import androidx.core.net.toUri
4140
import androidx.fragment.app.Fragment
4241
import com.ichi2.anki.SharedDecksActivity.Companion.DOWNLOAD_FILE
42+
import com.ichi2.anki.databinding.FragmentSharedDecksDownloadBinding
4343
import com.ichi2.anki.snackbar.showSnackbar
4444
import com.ichi2.anki.utils.openUrl
4545
import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
@@ -58,22 +58,18 @@ import kotlin.math.abs
5858
* Only one download is supported at a time, since importing multiple decks
5959
* simultaneously is not supported.
6060
*/
61-
class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_download) {
61+
class SharedDecksDownloadFragment : Fragment() {
62+
// binding pattern to handle onCreateView/onDestroyView
63+
private var fragmentBinding: FragmentSharedDecksDownloadBinding? = null
64+
private val binding get() = fragmentBinding!!
65+
6266
private var downloadId: Long = 0
6367

6468
private var fileName: String? = null
6569

6670
private var handler: Handler = Handler(Looper.getMainLooper())
6771
private var isProgressCheckerRunning = false
6872

69-
private lateinit var cancelButton: Button
70-
private lateinit var tryAgainButton: Button
71-
private lateinit var importDeckButton: Button
72-
private lateinit var downloadPercentageText: TextView
73-
private lateinit var downloadProgressBar: ProgressBar
74-
private lateinit var checkNetworkInfoText: TextView
75-
private lateinit var openInBrowserButton: Button
76-
7773
/**
7874
* Android's DownloadManager - Used here to manage the functionality of downloading decks, one
7975
* at a time. Responsible for enqueuing a download and generating the corresponding download ID,
@@ -142,52 +138,59 @@ class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_down
142138
}
143139
}
144140

141+
override fun onCreateView(
142+
inflater: LayoutInflater,
143+
container: ViewGroup?,
144+
savedInstanceState: Bundle?,
145+
) = FragmentSharedDecksDownloadBinding
146+
.inflate(inflater, container, false)
147+
.apply {
148+
fragmentBinding = this
149+
}.root
150+
145151
override fun onViewCreated(
146152
view: View,
147153
savedInstanceState: Bundle?,
148154
) {
149155
super.onViewCreated(view, savedInstanceState)
150156

151-
downloadPercentageText = view.findViewById(R.id.download_percentage)
152-
downloadProgressBar = view.findViewById(R.id.download_progress)
153-
cancelButton = view.findViewById(R.id.cancel_shared_decks_download)
154-
importDeckButton = view.findViewById(R.id.import_shared_deck_button)
155-
tryAgainButton = view.findViewById(R.id.try_again_deck_download)
156-
checkNetworkInfoText = view.findViewById(R.id.check_network_info_text)
157-
openInBrowserButton = view.findViewById(R.id.download_shared_deck_from_browser)
158-
159157
val fileToBeDownloaded = arguments?.getSerializableCompat<DownloadFile>(DOWNLOAD_FILE)!!
160158
downloadManager = (activity as SharedDecksActivity).downloadManager
161159

162160
downloadFile(fileToBeDownloaded)
163161

164-
cancelButton.setOnClickListener {
162+
binding.cancelDownloadButton.setOnClickListener {
165163
Timber.i("Cancel download button clicked which would lead to showing of confirmation dialog")
166164
showCancelConfirmationDialog()
167165
}
168166

169-
importDeckButton.setOnClickListener {
167+
binding.importSharedDeckButton.setOnClickListener {
170168
Timber.i("Import deck button clicked")
171169
openDownloadedDeck(context)
172170
}
173171

174-
openInBrowserButton.setOnClickListener {
172+
binding.openInWebBrowserButton.setOnClickListener {
175173
Timber.i("'Open in Browser' clicked")
176174
downloadManager.remove(downloadId)
177175
openUrl(requireContext().getDeckPageUri(fileToBeDownloaded.url).toUri())
178176
parentFragmentManager.popBackStack()
179177
}
180178

181-
tryAgainButton.setOnClickListener {
179+
binding.tryDownloadAgainButton.setOnClickListener {
182180
Timber.i("Try again button clicked, retry downloading of deck")
183181
downloadManager.remove(downloadId)
184182
downloadFile(fileToBeDownloaded)
185-
cancelButton.visibility = View.VISIBLE
186-
tryAgainButton.visibility = View.GONE
187-
openInBrowserButton.visibility = View.GONE
183+
binding.cancelDownloadButton.visibility = View.VISIBLE
184+
binding.tryDownloadAgainButton.visibility = View.GONE
185+
binding.openInWebBrowserButton.visibility = View.GONE
188186
}
189187
}
190188

189+
override fun onDestroyView() {
190+
super.onDestroyView()
191+
fragmentBinding = null
192+
}
193+
191194
/**
192195
* Register broadcast receiver for listening to download completion.
193196
* Set the request for downloading a deck, enqueue it in DownloadManager, store download ID and
@@ -225,7 +228,7 @@ class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_down
225228
onBackPressedCallback.isEnabled = isDownloadInProgress
226229
Timber.d("Download ID -> $downloadId")
227230
Timber.d("File name -> $fileName")
228-
view?.findViewById<TextView>(R.id.downloading_title)?.text = getString(R.string.downloading_file, fileName)
231+
fragmentBinding?.downloadingTitle?.text = getString(R.string.downloading_file, fileName)
229232
startDownloadProgressChecker()
230233
}
231234

@@ -337,12 +340,12 @@ class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_down
337340

338341
if (isVisible) {
339342
// Setting these since progress checker can stop before progress is updated to represent 100%
340-
downloadPercentageText.text = getString(R.string.percentage, DOWNLOAD_COMPLETED_PROGRESS_PERCENTAGE)
341-
downloadProgressBar.progress = DOWNLOAD_COMPLETED_PROGRESS_PERCENTAGE.toInt()
343+
binding.downloadPercentageText.text = getString(R.string.percentage, DOWNLOAD_COMPLETED_PROGRESS_PERCENTAGE)
344+
binding.downloadProgressBar.progress = DOWNLOAD_COMPLETED_PROGRESS_PERCENTAGE.toInt()
342345

343346
// Remove cancel button and show import deck button
344-
cancelButton.visibility = View.GONE
345-
importDeckButton.visibility = View.VISIBLE
347+
binding.cancelDownloadButton.visibility = View.GONE
348+
binding.importSharedDeckButton.visibility = View.VISIBLE
346349
}
347350

348351
Timber.i("Opening downloaded deck for import")
@@ -410,8 +413,8 @@ class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_down
410413
Timber.d("Starting download progress checker")
411414
downloadProgressChecker.run()
412415
isProgressCheckerRunning = true
413-
downloadPercentageText.text = getString(R.string.percentage, DOWNLOAD_STARTED_PROGRESS_PERCENTAGE)
414-
downloadProgressBar.progress = DOWNLOAD_STARTED_PROGRESS_PERCENTAGE.toInt()
416+
binding.downloadPercentageText.text = getString(R.string.percentage, DOWNLOAD_STARTED_PROGRESS_PERCENTAGE)
417+
binding.downloadProgressBar.progress = DOWNLOAD_STARTED_PROGRESS_PERCENTAGE.toInt()
415418
}
416419

417420
/**
@@ -452,8 +455,8 @@ class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_down
452455
// Show download progress percentage up to 1 decimal place.
453456
"%.1f".format(downloadProgress)
454457
}
455-
downloadPercentageText.text = getString(R.string.percentage, percentageValue)
456-
downloadProgressBar.progress = downloadProgress.toInt()
458+
binding.downloadPercentageText.text = getString(R.string.percentage, percentageValue)
459+
binding.downloadProgressBar.progress = downloadProgress.toInt()
457460

458461
val columnIndexForStatus = it.getColumnIndex(DownloadManager.COLUMN_STATUS)
459462
val columnIndexForReason = it.getColumnIndex(DownloadManager.COLUMN_REASON)
@@ -472,9 +475,9 @@ class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_down
472475
if (it.getInt(columnIndexForStatus) == DownloadManager.STATUS_PAUSED &&
473476
it.getInt(columnIndexForReason) == DownloadManager.PAUSED_WAITING_FOR_NETWORK
474477
) {
475-
checkNetworkInfoText.visibility = View.VISIBLE
478+
binding.checkNetworkInfoText.visibility = View.VISIBLE
476479
} else {
477-
checkNetworkInfoText.visibility = View.GONE
480+
binding.checkNetworkInfoText.visibility = View.GONE
478481
}
479482
}
480483
}
@@ -528,11 +531,11 @@ class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_down
528531
Timber.i("Download failed, update UI and provide option to retry")
529532
context?.let { showThemedToast(it, R.string.something_wrong, false) }
530533
// Update UI if download could not be successful
531-
tryAgainButton.visibility = View.VISIBLE
532-
openInBrowserButton.visibility = View.VISIBLE
533-
cancelButton.visibility = View.GONE
534-
downloadPercentageText.text = getString(R.string.download_failed)
535-
downloadProgressBar.progress = DOWNLOAD_STARTED_PROGRESS_PERCENTAGE.toInt()
534+
binding.tryDownloadAgainButton.visibility = View.VISIBLE
535+
binding.openInWebBrowserButton.visibility = View.VISIBLE
536+
binding.cancelDownloadButton.visibility = View.GONE
537+
binding.downloadPercentageText.text = getString(R.string.download_failed)
538+
binding.downloadProgressBar.progress = DOWNLOAD_STARTED_PROGRESS_PERCENTAGE.toInt()
536539
}
537540
}
538541

AnkiDroid/src/main/res/layout/fragment_shared_decks_download.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
app:layout_constraintTop_toBottomOf="@id/downloading_title" />
3838

3939
<com.ichi2.ui.FixedTextView
40-
android:id="@+id/download_percentage"
40+
android:id="@+id/download_percentage_text"
4141
android:layout_width="wrap_content"
4242
android:layout_height="wrap_content"
4343
android:textSize="20sp"
@@ -48,7 +48,7 @@
4848
tools:text="100 %" />
4949

5050
<ProgressBar
51-
android:id="@+id/download_progress"
51+
android:id="@+id/download_progress_bar"
5252
android:layout_width="match_parent"
5353
android:layout_height="wrap_content"
5454
android:layout_marginTop="16dp"
@@ -57,7 +57,7 @@
5757
android:scaleY="4"
5858
app:layout_constraintStart_toStartOf="parent"
5959
app:layout_constraintEnd_toEndOf="parent"
60-
app:layout_constraintTop_toBottomOf="@id/download_percentage"
60+
app:layout_constraintTop_toBottomOf="@id/download_percentage_text"
6161
style="@style/Widget.AppCompat.ProgressBar.Horizontal" />
6262

6363
<com.ichi2.ui.FixedTextView
@@ -73,11 +73,11 @@
7373
android:gravity="center"
7474
app:layout_constraintStart_toStartOf="parent"
7575
app:layout_constraintEnd_toEndOf="parent"
76-
app:layout_constraintTop_toBottomOf="@id/download_progress"
76+
app:layout_constraintTop_toBottomOf="@id/download_progress_bar"
7777
android:visibility="gone" />
7878

7979
<android.widget.Button
80-
android:id="@+id/download_shared_deck_from_browser"
80+
android:id="@+id/open_in_web_browser_button"
8181
android:layout_width="match_parent"
8282
android:layout_height="wrap_content"
8383
android:text="@string/open_in_browser"
@@ -89,11 +89,11 @@
8989
android:layout_marginEnd="24dp"
9090
app:layout_constraintStart_toStartOf="parent"
9191
app:layout_constraintEnd_toEndOf="parent"
92-
app:layout_constraintBottom_toTopOf="@id/try_again_deck_download"
92+
app:layout_constraintBottom_toTopOf="@id/try_download_again_button"
9393
android:visibility="gone" />
9494

9595
<android.widget.Button
96-
android:id="@+id/cancel_shared_decks_download"
96+
android:id="@+id/cancel_download_button"
9797
android:layout_width="match_parent"
9898
android:layout_height="wrap_content"
9999
android:text="@string/cancel_download"
@@ -108,7 +108,7 @@
108108
app:layout_constraintEnd_toEndOf="parent" />
109109

110110
<android.widget.Button
111-
android:id="@+id/try_again_deck_download"
111+
android:id="@+id/try_download_again_button"
112112
android:layout_width="match_parent"
113113
android:layout_height="wrap_content"
114114
android:text="@string/try_again"

0 commit comments

Comments
 (0)