Skip to content

Commit d3ffa0b

Browse files
committed
More cleanups
1 parent 2030a70 commit d3ffa0b

File tree

8 files changed

+28
-57
lines changed

8 files changed

+28
-57
lines changed

app/src/main/java/com/pr0gramm/app/feed/FeedFilter.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.pr0gramm.app.feed
33
import android.os.Parcel
44
import android.os.Parcelable
55
import com.google.common.base.Objects
6-
import com.google.common.base.Optional
76
import com.google.common.base.Strings.emptyToNull
87
import com.pr0gramm.app.parcel.core.creator
98

@@ -55,7 +54,7 @@ class FeedFilter() : Parcelable {
5554
*/
5655
fun withTags(tags: String): FeedFilter {
5756
val copy = basic()
58-
copy.tags = fromString(tags)
57+
copy.tags = normalizeString(tags)
5958
return normalize(copy)
6059
}
6160

@@ -64,7 +63,7 @@ class FeedFilter() : Parcelable {
6463
*/
6564
fun withUser(username: String): FeedFilter {
6665
val copy = basic()
67-
copy.username = fromString(username)
66+
copy.username = normalizeString(username)
6867
return normalize(copy)
6968
}
7069

@@ -73,21 +72,20 @@ class FeedFilter() : Parcelable {
7372
*/
7473
fun withLikes(username: String): FeedFilter {
7574
val copy = basic()
76-
copy.likes = fromString(username)
75+
copy.likes = normalizeString(username)
7776
return normalize(copy)
7877
}
7978

8079
fun withTagsNoReset(tags: String): FeedFilter {
8180
val copy = withLikes(likes ?: "")
82-
copy.tags = fromString(tags)
81+
copy.tags = normalizeString(tags)
8382
return normalize(copy)
8483
}
8584

8685
/**
87-
* Creates an []Optional] from a string - trims the input and creates an empty
88-
* [Optional] from empty strings.
86+
* Normalizes the given string by trimming it and setting empty strings to null.
8987
*/
90-
private fun fromString(value: String): String? = emptyToNull(value.trim())
88+
private fun normalizeString(value: String): String? = emptyToNull(value.trim())
9189

9290
private fun copy(fn: FeedFilter.() -> Unit): FeedFilter {
9391
val copy = FeedFilter()

app/src/main/java/com/pr0gramm/app/ui/ScrollHideToolbarListener.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.pr0gramm.app.ui
33
import android.support.v7.widget.RecyclerView
44
import android.view.View
55
import android.view.ViewPropertyAnimator
6-
import com.google.common.base.Optional
76
import com.pr0gramm.app.util.endAction
87
import com.pr0gramm.app.util.visible
98

@@ -118,14 +117,14 @@ class ScrollHideToolbarListener(private val toolbar: View) {
118117
* @param recyclerView The recycler view to estimate scrolling of
119118
*/
120119
@JvmStatic
121-
fun estimateRecyclerViewScrollY(recyclerView: RecyclerView): Optional<Int> {
120+
fun estimateRecyclerViewScrollY(recyclerView: RecyclerView): Int? {
122121
var scrollY: Int? = null
123122
val view = recyclerView.layoutManager.findViewByPosition(0)
124123
if (view != null) {
125124
scrollY = -view.y.toInt()
126125
}
127126

128-
return Optional.fromNullable(scrollY)
127+
return scrollY
129128
}
130129
}
131130
}

app/src/main/java/com/pr0gramm/app/ui/fragments/FeedFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ class FeedFragment : BaseFragment("FeedFragment"), FilterFragment, BackAwareFrag
11771177
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
11781178
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
11791179
if (activity is ToolbarActivity) {
1180-
val y = ScrollHideToolbarListener.estimateRecyclerViewScrollY(recyclerView).or(Integer.MAX_VALUE)
1180+
val y = ScrollHideToolbarListener.estimateRecyclerViewScrollY(recyclerView) ?: Integer.MAX_VALUE
11811181

11821182
val activity = activity as ToolbarActivity
11831183
activity.scrollHideToolbarListener.onScrollFinished(y)

app/src/main/java/com/pr0gramm/app/ui/fragments/PostFragment.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,9 +1028,9 @@ class PostFragment : BaseFragment("PostFragment"), NewTagDialogFragment.OnAddNew
10281028
// get our facts straight
10291029
val recyclerHeight = recyclerView.height
10301030
val scrollEstimate = ScrollHideToolbarListener.estimateRecyclerViewScrollY(recyclerView)
1031-
var viewerVisible = scrollEstimate.isPresent
1031+
var viewerVisible = scrollEstimate != null
10321032

1033-
val scrollY = scrollEstimate.or(viewer.height)
1033+
val scrollY = scrollEstimate ?: viewer.height
10341034
val viewerHeight = viewer.height
10351035
val doFancyScroll = viewerHeight < recyclerHeight
10361036

@@ -1070,7 +1070,7 @@ class PostFragment : BaseFragment("PostFragment"), NewTagDialogFragment.OnAddNew
10701070

10711071
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
10721072
if (!isVideoFullScreen && newState == RecyclerView.SCROLL_STATE_IDLE) {
1073-
val y = ScrollHideToolbarListener.estimateRecyclerViewScrollY(recyclerView).or(Integer.MAX_VALUE)
1073+
val y = ScrollHideToolbarListener.estimateRecyclerViewScrollY(recyclerView) ?: Integer.MAX_VALUE
10741074
(activity as ToolbarActivity).scrollHideToolbarListener.onScrollFinished(y)
10751075
}
10761076
}

app/src/main/java/com/pr0gramm/app/ui/views/viewer/AbstractProgressMediaView.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import android.view.animation.AccelerateInterpolator
88
import android.view.animation.DecelerateInterpolator
99
import android.widget.ProgressBar
1010
import android.widget.SeekBar
11-
import com.google.common.base.Optional
1211
import com.pr0gramm.app.R
1312
import com.pr0gramm.app.util.AndroidUtility
1413
import com.pr0gramm.app.util.hideViewEndAction
@@ -30,7 +29,7 @@ abstract class AbstractProgressMediaView(config: MediaView.Config, @LayoutRes la
3029
private val progressView: ProgressBar = LayoutInflater.from(context)
3130
.inflate(R.layout.player_video_progress, this, false) as ProgressBar
3231

33-
protected abstract val videoProgress: Optional<ProgressInfo>
32+
protected abstract val videoProgress: ProgressInfo?
3433

3534
init {
3635
publishControllerView(progressView)
@@ -126,7 +125,7 @@ abstract class AbstractProgressMediaView(config: MediaView.Config, @LayoutRes la
126125
return
127126

128127
if (!progressTouched) {
129-
val info = videoProgress.orNull()
128+
val info = videoProgress
130129
if (info != null && shouldShowView(info)) {
131130
if (firstTimeProgressValue && progressEnabled) {
132131
firstTimeProgressValue = false

app/src/main/java/com/pr0gramm/app/ui/views/viewer/GifMediaView.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.annotation.SuppressLint
44
import android.view.View
55
import android.widget.ImageView
66
import com.github.salomonbrys.kodein.instance
7-
import com.google.common.base.Optional
87
import com.jakewharton.rxbinding.view.RxView
98
import com.pr0gramm.app.R
109
import com.pr0gramm.app.services.GifDrawableLoader
@@ -89,17 +88,17 @@ class GifMediaView(config: MediaView.Config) : AbstractProgressMediaView(config,
8988
}
9089
}
9190

92-
override val videoProgress: Optional<AbstractProgressMediaView.ProgressInfo> get() {
91+
override val videoProgress: AbstractProgressMediaView.ProgressInfo? get() {
9392
gif?.takeIf { isPlaying }?.let { gif ->
9493
val position = gif.currentFrameIndex
9594
val duration = gif.numberOfFrames
9695

9796
if (position >= 0 && duration > 0) {
98-
return Optional.of(AbstractProgressMediaView.ProgressInfo(position / duration.toFloat(), 1f))
97+
return AbstractProgressMediaView.ProgressInfo(position / duration.toFloat(), 1f)
9998
}
10099
}
101100

102-
return Optional.absent<AbstractProgressMediaView.ProgressInfo>()
101+
return null
103102
}
104103

105104
override fun onPause() {

app/src/main/java/com/pr0gramm/app/ui/views/viewer/VideoMediaView.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import android.view.animation.AccelerateInterpolator
1616
import android.view.animation.DecelerateInterpolator
1717
import android.widget.ImageView
1818
import com.github.salomonbrys.kodein.instance
19-
import com.google.common.base.Optional
2019
import com.google.common.hash.Hashing
2120
import com.jakewharton.rxbinding.view.detaches
2221
import com.pr0gramm.app.R
@@ -211,15 +210,14 @@ class VideoMediaView(config: MediaView.Config) : AbstractProgressMediaView(confi
211210
muteButtonView.setImageDrawable(icon)
212211
}
213212

214-
override val videoProgress: Optional<AbstractProgressMediaView.ProgressInfo>
215-
get() {
216-
if (videoViewInitialized && isPlaying) {
217-
return Optional.of(AbstractProgressMediaView.ProgressInfo(videoPlayer.progress, videoPlayer.buffered))
218-
}
219-
220-
return Optional.absent<AbstractProgressMediaView.ProgressInfo>()
213+
override val videoProgress: AbstractProgressMediaView.ProgressInfo? get() {
214+
if (videoViewInitialized && isPlaying) {
215+
return AbstractProgressMediaView.ProgressInfo(videoPlayer.progress, videoPlayer.buffered)
221216
}
222217

218+
return null
219+
}
220+
223221
override fun onVideoBufferingStarts() {}
224222

225223
override fun onVideoBufferingEnds() {}

app/src/main/java/com/pr0gramm/app/util/Extensions.kt

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import android.util.Log
1515
import android.view.LayoutInflater
1616
import android.view.View
1717
import android.view.ViewGroup
18-
import com.google.common.base.Optional
1918
import com.google.common.base.Stopwatch
2019
import com.google.common.io.ByteStreams
2120
import com.google.gson.JsonObject
@@ -39,23 +38,6 @@ import kotlin.properties.Delegates
3938
import kotlin.properties.ReadWriteProperty
4039
import kotlin.reflect.KProperty
4140

42-
43-
inline fun <T, R> Optional<T>.map(fn: (T) -> R?): Optional<R> {
44-
if (isPresent) {
45-
return Optional.fromNullable(fn(get()))
46-
} else {
47-
return Optional.absent()
48-
}
49-
}
50-
51-
inline fun <T> Optional<T>.filter(fn: (T) -> Boolean): Optional<T> {
52-
if (isPresent && fn(get())) {
53-
return this
54-
} else {
55-
return Optional.absent()
56-
}
57-
}
58-
5941
inline fun <T> createObservable(mode: Emitter.BackpressureMode = Emitter.BackpressureMode.NONE,
6042
crossinline block: (emitter: Emitter<T>) -> Unit): Observable<T> {
6143

@@ -77,14 +59,14 @@ inline fun readStream(stream: InputStream, bufferSize: Int = 16 * 1042, fn: (Byt
7759
break
7860
}
7961

80-
fn(buffer, read);
62+
fn(buffer, read)
8163
}
8264
}
8365

8466
inline fun SharedPreferences.edit(fn: SharedPreferences.Editor.() -> Unit): Unit {
8567
val editor = edit()
8668
editor.fn()
87-
editor.apply();
69+
editor.apply()
8870
}
8971

9072
inline fun <R> PowerManager.WakeLock.use(timeValue: Long, timeUnit: TimeUnit, fn: () -> R): R {
@@ -129,11 +111,7 @@ inline fun <R> TypedArray.use(block: (TypedArray) -> R): R {
129111
}
130112

131113
fun arrayOfStrings(vararg args: Any): Array<String> {
132-
return Array<String>(args.size) { args[it].toString() }
133-
}
134-
135-
fun <T> T?.toOptional(): Optional<T> {
136-
return Optional.fromNullable(this)
114+
return Array(args.size) { args[it].toString() }
137115
}
138116

139117
fun JsonObject.getIfPrimitive(key: String): JsonPrimitive? {
@@ -150,7 +128,7 @@ fun JsonObject.getPrimitive(key: String): JsonPrimitive {
150128
}
151129

152130
inline fun <R, T> observeChange(def: T, crossinline onChange: () -> Unit): ReadWriteProperty<R, T> {
153-
return Delegates.observable(def) { _, old, new ->
131+
return Delegates.observable(def) { _, _, _ ->
154132
onChange()
155133
}
156134
}
@@ -163,7 +141,7 @@ inline fun <R, T> observeChangeEx(def: T, crossinline onChange: (T, T) -> Unit):
163141

164142
val View.layoutInflater: LayoutInflater get() = LayoutInflater.from(context)
165143

166-
interface CachedValue<T> {
144+
interface CachedValue<out T> {
167145
val value: T
168146

169147
fun invalidate(): Unit

0 commit comments

Comments
 (0)