Skip to content

Commit 562025e

Browse files
committed
Guard against uninitialized view updates
1 parent 0dcaef8 commit 562025e

File tree

8 files changed

+130
-83
lines changed

8 files changed

+130
-83
lines changed

app/src/main/kotlin/com/proxerme/app/fragment/anime/AnimeFragment.kt

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,42 +67,50 @@ class AnimeFragment : SingleLoadingFragment<Pair<AnimeInput, String>, StreamInfo
6767
}
6868

6969
private val reminderSuccess = { nothing: Void? ->
70-
Snackbar.make(root, R.string.fragment_set_user_info_success, Snackbar.LENGTH_LONG).show()
70+
if (view != null) {
71+
Snackbar.make(root, R.string.fragment_set_user_info_success, Snackbar.LENGTH_LONG).show()
72+
}
7173
}
7274

7375
private val reminderException = { exception: Exception ->
74-
val action = ErrorUtils.handle(activity as MainActivity, exception)
76+
if (view != null) {
77+
val action = ErrorUtils.handle(activity as MainActivity, exception)
7578

76-
ViewUtils.makeMultilineSnackbar(root,
77-
getString(R.string.fragment_set_user_info_error, action.message),
78-
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction).show()
79+
ViewUtils.makeMultilineSnackbar(root,
80+
getString(R.string.fragment_set_user_info_error, action.message),
81+
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction).show()
82+
}
7983
}
8084

8185
private val streamResolverSuccess = { result: StreamResolutionResult ->
82-
if (result.intent.type == "text/html") {
83-
showPage(HttpUrl.parse(result.intent.data.toString()))
84-
} else {
85-
try {
86-
context.startActivity(result.intent)
87-
} catch (exception: ActivityNotFoundException) {
88-
result.notFoundAction.invoke(activity as AppCompatActivity)
86+
if (view != null) {
87+
if (result.intent.type == "text/html") {
88+
showPage(HttpUrl.parse(result.intent.data.toString()))
89+
} else {
90+
try {
91+
context.startActivity(result.intent)
92+
} catch (exception: ActivityNotFoundException) {
93+
result.notFoundAction.invoke(activity as AppCompatActivity)
94+
}
8995
}
9096
}
9197
}
9298

9399
private val streamResolverException = { exception: Exception ->
94-
when (exception) {
95-
is NoResolverException -> {
96-
Snackbar.make(root, R.string.error_unsupported_hoster, Snackbar.LENGTH_LONG).show()
97-
}
98-
is StreamResolutionException -> {
99-
Snackbar.make(root, R.string.error_stream_resolution, Snackbar.LENGTH_LONG).show()
100-
}
101-
else -> {
102-
val action = ErrorUtils.handle(activity as MainActivity, exception)
100+
if (view != null) {
101+
when (exception) {
102+
is NoResolverException -> {
103+
Snackbar.make(root, R.string.error_unsupported_hoster, Snackbar.LENGTH_LONG).show()
104+
}
105+
is StreamResolutionException -> {
106+
Snackbar.make(root, R.string.error_stream_resolution, Snackbar.LENGTH_LONG).show()
107+
}
108+
else -> {
109+
val action = ErrorUtils.handle(activity as MainActivity, exception)
103110

104-
Snackbar.make(root, action.message, Snackbar.LENGTH_LONG)
105-
.setAction(action.buttonMessage, action.buttonAction).show()
111+
Snackbar.make(root, action.message, Snackbar.LENGTH_LONG)
112+
.setAction(action.buttonMessage, action.buttonAction).show()
113+
}
106114
}
107115
}
108116
}

app/src/main/kotlin/com/proxerme/app/fragment/chat/NewChatFragment.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,27 @@ class NewChatFragment : MainFragment() {
7272
}
7373

7474
private val exception = { exception: Exception ->
75-
context?.let {
76-
when (exception) {
77-
is InvalidInputException -> {
78-
exception.message?.let {
79-
Snackbar.make(root, it, Snackbar.LENGTH_LONG).show()
75+
if (view != null) {
76+
context?.let {
77+
when (exception) {
78+
is InvalidInputException -> {
79+
exception.message?.let {
80+
Snackbar.make(root, it, Snackbar.LENGTH_LONG).show()
81+
}
8082
}
81-
}
82-
is TopicEmptyException -> {
83-
topicInputContainer.isErrorEnabled = true
84-
topicInputContainer.error = context.getString(R.string.error_input_empty)
85-
}
86-
else -> {
87-
val action = ErrorUtils.handle(activity as MainActivity, exception)
83+
is TopicEmptyException -> {
84+
topicInputContainer.isErrorEnabled = true
85+
topicInputContainer.error = context.getString(R.string.error_input_empty)
86+
}
87+
else -> {
88+
val action = ErrorUtils.handle(activity as MainActivity, exception)
8889

89-
Snackbar.make(root, action.message, Snackbar.LENGTH_LONG)
90-
.setAction(action.buttonMessage, action.buttonAction).show()
90+
Snackbar.make(root, action.message, Snackbar.LENGTH_LONG)
91+
.setAction(action.buttonMessage, action.buttonAction).show()
92+
}
9193
}
9294
}
9395
}
94-
95-
Unit
9696
}
9797

9898
override val section = Section.NEW_CHAT

app/src/main/kotlin/com/proxerme/app/fragment/framework/PagedLoadingFragment.kt

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,44 @@ import org.jetbrains.anko.find
3737
abstract class PagedLoadingFragment<I, T> : MainFragment() where I : PagedInput {
3838

3939
protected val successCallback = { data: Array<T> ->
40-
hasReachedEnd = data.size < itemsOnPage
40+
if (view != null) {
41+
hasReachedEnd = data.size < itemsOnPage
4142

42-
adapter.append(data)
43+
adapter.append(data)
4344

44-
showEmptyIfAppropriate()
45-
onItemsInserted(data)
45+
showEmptyIfAppropriate()
46+
onItemsInserted(data)
47+
}
4648
}
4749

4850
protected val exceptionCallback = { exception: Exception ->
49-
val action = ErrorUtils.handle(activity as MainActivity, exception)
51+
if (view != null) {
52+
val action = ErrorUtils.handle(activity as MainActivity, exception)
5053

51-
showError(action.message, action.buttonMessage, action.buttonAction)
54+
showError(action.message, action.buttonMessage, action.buttonAction)
55+
}
5256
}
5357

5458
protected val refreshSuccessCallback = { data: Array<T> ->
55-
adapter.updateAndScrollUpIfNecessary(list.layoutManager, list, when (replaceOnRefresh) {
56-
true -> { it: PagingAdapter<T> -> it.replace(data) }
57-
false -> { it: PagingAdapter<T> -> it.insert(data) }
58-
})
59-
60-
showEmptyIfAppropriate()
61-
onItemsInserted(data)
59+
if (view != null) {
60+
adapter.updateAndScrollUpIfNecessary(list.layoutManager, list, when (replaceOnRefresh) {
61+
true -> { it: PagingAdapter<T> -> it.replace(data) }
62+
false -> { it: PagingAdapter<T> -> it.insert(data) }
63+
})
64+
65+
showEmptyIfAppropriate()
66+
onItemsInserted(data)
67+
}
6268
}
6369

6470
protected val refreshExceptionCallback = { exception: Exception ->
65-
val action = ErrorUtils.handle(activity as MainActivity, exception)
71+
if (view != null) {
72+
val action = ErrorUtils.handle(activity as MainActivity, exception)
6673

67-
ViewUtils.makeMultilineSnackbar(root,
68-
getString(R.string.error_refresh, action.message),
69-
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction).show()
74+
ViewUtils.makeMultilineSnackbar(root,
75+
getString(R.string.error_refresh, action.message),
76+
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction).show()
77+
}
7078
}
7179

7280
open protected val isSwipeToRefreshEnabled = true
@@ -280,8 +288,10 @@ abstract class PagedLoadingFragment<I, T> : MainFragment() where I : PagedInput
280288
}
281289

282290
protected fun setRefreshing(enable: Boolean) {
283-
progress.isEnabled = if (!enable) isSwipeToRefreshEnabled else true
284-
progress.isRefreshing = enable
291+
if (view != null) {
292+
progress.isEnabled = if (!enable) isSwipeToRefreshEnabled else true
293+
progress.isRefreshing = enable
294+
}
285295
}
286296

287297
protected fun updateRefreshing() {

app/src/main/kotlin/com/proxerme/app/fragment/framework/SingleLoadingFragment.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ import org.greenrobot.eventbus.ThreadMode
3131
abstract class SingleLoadingFragment<I, T> : MainFragment() {
3232

3333
private val successCallback = { data: T ->
34-
present(data)
34+
if (view != null) {
35+
present(data)
36+
}
3537
}
3638

3739
private val exceptionCallback = { exception: Exception ->
38-
val action = ErrorUtils.handle(activity as MainActivity, exception)
40+
if (view != null) {
41+
val action = ErrorUtils.handle(activity as MainActivity, exception)
3942

40-
showError(action.message, action.buttonMessage, action.buttonAction)
43+
showError(action.message, action.buttonMessage, action.buttonAction)
44+
}
4145
}
4246

4347
open protected val isSwipeToRefreshEnabled = false
@@ -78,7 +82,9 @@ abstract class SingleLoadingFragment<I, T> : MainFragment() {
7882
contentContainer.visibility = View.GONE
7983
errorContainer.visibility = View.GONE
8084
}.onSuccess {
81-
contentContainer.visibility = View.VISIBLE
85+
if (view != null) {
86+
contentContainer.visibility = View.VISIBLE
87+
}
8288
}.onFinish {
8389
updateRefreshing()
8490
}
@@ -192,8 +198,10 @@ abstract class SingleLoadingFragment<I, T> : MainFragment() {
192198
}
193199

194200
protected fun setRefreshing(enable: Boolean) {
195-
progress.isEnabled = if (!enable) isSwipeToRefreshEnabled else true
196-
progress.isRefreshing = enable
201+
if (view != null) {
202+
progress.isEnabled = if (!enable) isSwipeToRefreshEnabled else true
203+
progress.isRefreshing = enable
204+
}
197205
}
198206

199207
abstract fun present(data: T)

app/src/main/kotlin/com/proxerme/app/fragment/manga/MangaFragment.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,21 @@ class MangaFragment : SingleLoadingFragment<Pair<MangaInput, String>, ChapterInf
7272
}
7373

7474
private val reminderSuccess = { nothing: Void? ->
75-
Snackbar.make(root, R.string.fragment_set_user_info_success, Snackbar.LENGTH_LONG).show()
75+
if (view != null) {
76+
Snackbar.make(root, R.string.fragment_set_user_info_success, Snackbar.LENGTH_LONG)
77+
.show()
78+
}
7679
}
7780

7881
private val reminderException = { exception: Exception ->
79-
val action = ErrorUtils.handle(activity as MainActivity, exception)
82+
if (view != null) {
83+
val action = ErrorUtils.handle(activity as MainActivity, exception)
8084

81-
ViewUtils.makeMultilineSnackbar(root,
82-
getString(R.string.fragment_set_user_info_error, action.message),
83-
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction).show()
85+
ViewUtils.makeMultilineSnackbar(root,
86+
getString(R.string.fragment_set_user_info_error, action.message),
87+
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction)
88+
.show()
89+
}
8490
}
8591

8692
override val section = SectionManager.Section.MANGA

app/src/main/kotlin/com/proxerme/app/fragment/media/MediaInfoFragment.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,21 @@ class MediaInfoFragment : SingleLoadingFragment<String, Entry>() {
5252
}
5353

5454
private val userInfoSuccess = { nothing: Void? ->
55-
Snackbar.make(root, R.string.fragment_set_user_info_success, Snackbar.LENGTH_LONG).show()
55+
if (view != null) {
56+
Snackbar.make(root, R.string.fragment_set_user_info_success, Snackbar.LENGTH_LONG)
57+
.show()
58+
}
5659
}
5760

5861
private val userInfoException = { exception: Exception ->
59-
val action = ErrorUtils.handle(context as MainActivity, exception)
62+
if (view != null) {
63+
val action = ErrorUtils.handle(context as MainActivity, exception)
6064

61-
ViewUtils.makeMultilineSnackbar(root,
62-
getString(R.string.fragment_set_user_info_error, action.message),
63-
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction).show()
65+
ViewUtils.makeMultilineSnackbar(root,
66+
getString(R.string.fragment_set_user_info_error, action.message),
67+
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction)
68+
.show()
69+
}
6470
}
6571

6672
override val section = Section.MEDIA_INFO

app/src/main/kotlin/com/proxerme/app/fragment/ucp/ReminderFragment.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ class ReminderFragment : PagedLoadingFragment<ReminderInput, Reminder>() {
4949
private val removalException = { exception: Exception ->
5050
itemToRemove = null
5151

52-
val action = ErrorUtils.handle(activity as MainActivity, exception)
52+
if (view != null) {
53+
val action = ErrorUtils.handle(activity as MainActivity, exception)
5354

54-
ViewUtils.makeMultilineSnackbar(root,
55-
context.getString(R.string.error_reminder_removal, action.message),
56-
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction).show()
55+
ViewUtils.makeMultilineSnackbar(root,
56+
context.getString(R.string.error_reminder_removal, action.message),
57+
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction)
58+
.show()
59+
}
5760
}
5861

5962
override val section = Section.REMINDER

app/src/main/kotlin/com/proxerme/app/fragment/ucp/UcpToptenFragment.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,23 @@ class UcpToptenFragment : SingleLoadingFragment<Unit, ZippedUcpToptenResult>() {
4444
)
4545

4646
cache.mutate { newResult }
47-
present(newResult)
47+
48+
if (view != null) {
49+
present(newResult)
50+
}
4851
}
4952

5053
private val removalException = { exception: Exception ->
5154
itemToRemove = null
5255

53-
val action = ErrorUtils.handle(activity as MainActivity, exception)
56+
if (view != null) {
57+
val action = ErrorUtils.handle(activity as MainActivity, exception)
5458

55-
ViewUtils.makeMultilineSnackbar(root,
56-
context.getString(R.string.error_topten_removal, action.message),
57-
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction).show()
59+
ViewUtils.makeMultilineSnackbar(root,
60+
context.getString(R.string.error_topten_removal, action.message),
61+
Snackbar.LENGTH_LONG).setAction(action.buttonMessage, action.buttonAction)
62+
.show()
63+
}
5864
}
5965

6066
override val section = Section.TOPTEN

0 commit comments

Comments
 (0)