Skip to content

Commit

Permalink
Replaced the find casting with the new generic findViewById.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Jul 30, 2017
1 parent 81b6789 commit d422714
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import de.ph1b.audiobook.Book
import de.ph1b.audiobook.R
import de.ph1b.audiobook.injection.App
import de.ph1b.audiobook.misc.color
import de.ph1b.audiobook.misc.find
import de.ph1b.audiobook.misc.layoutInflater
import de.ph1b.audiobook.misc.onFirstPreDraw
import de.ph1b.audiobook.misc.supportTransitionName
Expand Down Expand Up @@ -116,9 +115,9 @@ class BookShelfAdapter(private val context: Context, private val bookClicked: (B

inner class ListViewHolder(parent: ViewGroup) : BaseViewHolder(parent.layoutInflater().inflate(R.layout.book_shelf_list_layout, parent, false)) {

private val progressBar = find<ProgressBar>(R.id.progressBar)
private val leftTime: TextView = find(R.id.leftTime)
private val rightTime: TextView = find(R.id.rightTime)
private val progressBar = itemView.findViewById<ProgressBar>(R.id.progressBar)
private val leftTime: TextView = itemView.findViewById<TextView>(R.id.leftTime)
private val rightTime: TextView = itemView.findViewById<TextView>(R.id.rightTime)

init {
MDTintHelper.setTint(progressBar, parent.context.color(R.color.accent))
Expand All @@ -144,10 +143,10 @@ class BookShelfAdapter(private val context: Context, private val bookClicked: (B

/** ViewHolder base class **/
abstract inner class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val coverView: ImageView = itemView.find(R.id.coverView)
private val currentPlayingIndicator: ImageView = itemView.find(R.id.currentPlayingIndicator)
private val titleView: TextView = itemView.find(R.id.title)
private val editBook: View = itemView.find(R.id.editBook)
val coverView: ImageView = itemView.findViewById<ImageView>(R.id.coverView)
private val currentPlayingIndicator: ImageView = itemView.findViewById<ImageView>(R.id.currentPlayingIndicator)
private val titleView: TextView = itemView.findViewById<TextView>(R.id.title)
private val editBook: View = itemView.findViewById<View>(R.id.editBook)
var indicatorVisible = false
private set

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SleepTimerDialogFragment : AppCompatDialogFragment() {
binding.eight.setOnClickListener { appendNumber(8) }
binding.nine.setOnClickListener { appendNumber(9) }
binding.zero.setOnClickListener { appendNumber(0) }
val delete = binding.root.findViewById(R.id.delete)
val delete = binding.root.findViewById<View>(R.id.delete)
// upon delete remove the last number
delete.setOnClickListener {
selectedMinutes /= 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.getbase.floatingactionbutton.FloatingActionsMenu
import de.ph1b.audiobook.R
import de.ph1b.audiobook.databinding.FolderOverviewBinding
import de.ph1b.audiobook.features.folderChooser.FolderChooserActivity
import de.ph1b.audiobook.misc.find
import de.ph1b.audiobook.mvp.MvpController
import de.ph1b.audiobook.uitools.setVisibleWeak
import de.ph1b.audiobook.uitools.visible
Expand All @@ -29,7 +28,7 @@ class FolderOverviewController : MvpController<FolderOverviewController, FolderO
override val layoutRes = R.layout.folder_overview

override fun onBindingCreated(binding: FolderOverviewBinding) {
buttonRepresentingTheFam = binding.root.find(R.id.fab_expand_menu_button)
buttonRepresentingTheFam = binding.root.findViewById<View>(R.id.fab_expand_menu_button)

binding.addAsSingle.setOnClickListener {
startFolderChooserActivity(FolderChooserActivity.OperationMode.SINGLE_BOOK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PermissionHelper(private val activity: Activity, private val permissions:
private val permissionDialogConfirmed = PublishSubject.create<Unit>()

fun storagePermission(gotPermission: () -> Unit = {}) {
val root = activity.findViewById(android.R.id.content)
val root = activity.findViewById<View>(android.R.id.content)
permissions.request(PERMISSION)
.toObservable()
.repeatWhen { it.flatMap { permissionDialogConfirmed } }
Expand Down
11 changes: 1 addition & 10 deletions app/src/main/java/de/ph1b/audiobook/misc/viewExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.ph1b.audiobook.misc

import android.app.Activity
import android.graphics.drawable.Drawable
import android.support.v7.widget.RecyclerView
import android.view.View
Expand Down Expand Up @@ -64,16 +63,8 @@ fun TextView.topCompoundDrawable(): Drawable? = compoundDrawables[1]
fun TextView.rightCompoundDrawable(): Drawable? = compoundDrawables[2]
fun TextView.bottomCompoundDrawable(): Drawable? = compoundDrawables[3]

@Suppress("UNCHECKED_CAST")
fun <T : View> View.find(id: Int): T = findViewById(id) as T

fun <T : View> RecyclerView.ViewHolder.find(id: Int): T = itemView.find(id)

@Suppress("UNCHECKED_CAST")
fun <T : View> Activity.find(id: Int): T = findViewById(id) as T

/** if the recyclerview is computing layout, post the action. else just execute it */
inline fun RecyclerView.postedIfComputingLayout(crossinline action: () -> Unit) {
if (!isComputingLayout) action()
else post { action() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ constructor(
if (it == PlayerState.PLAYING) {
Observable.interval(200L, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
.map { player.currentPosition }
.distinctUntilChanged { it -> it / 1000 } // let the value only pass the full second changed.
.distinctUntilChanged { position -> position / 1000 } // let the value only pass the full second changed.
} else Observable.empty()
}.subscribe {
// update the book
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/de/ph1b/audiobook/uitools/BetterSnack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.view.View
import android.widget.TextView
import de.ph1b.audiobook.R
import de.ph1b.audiobook.misc.color
import de.ph1b.audiobook.misc.find

/**
* Creates [Snackbar]s with convenient values set + theming
Expand Down Expand Up @@ -45,7 +44,7 @@ object BetterSnack {
bar.view.setBackgroundColor(root.context.color(R.color.snackbar_background_color))

// theme text color
val textView: TextView = bar.view.find(android.support.design.R.id.snackbar_text)
val textView = bar.view.findViewById<TextView>(android.support.design.R.id.snackbar_text)
textView.setTextColor(root.context.color(R.color.snackbar_text_color))

bar.show()
Expand Down

0 comments on commit d422714

Please sign in to comment.