Skip to content

Commit 57b6ceb

Browse files
committed
Various code style improvements and refactorings
1 parent ef2cf07 commit 57b6ceb

File tree

9 files changed

+176
-153
lines changed

9 files changed

+176
-153
lines changed

config/detekt/detekt.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,27 @@ potential-bugs:
55
active: false
66

77
complexity:
8+
StringLiteralDuplication:
9+
threshold: 5
810
LongParameterList:
911
active: false
1012
LargeClass:
1113
active: false
12-
ComplexMethod:
13-
active: false
1414
TooManyFunctions:
1515
active: false
1616
ComplexCondition:
1717
active: false
1818
NestedBlockDepth:
1919
active: false
20-
StringLiteralDuplication:
21-
threshold: 5
2220

2321
style:
24-
SafeCast:
25-
active: false
2622
FunctionMaxLength:
27-
active: false
28-
MagicNumber:
29-
active: false
23+
maximumFunctionNameLength: 35
3024
VariableMinLength:
3125
minimumVariableNameLength: 2
3226
VariableMaxLength:
27+
maximumVariableNameLength: 35
28+
MagicNumber:
3329
active: false
3430
DataClassContainsFunctions:
3531
active: false

src/main/kotlin/me/proxer/app/auth/LoginDialog.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ class LoginDialog : BaseDialog() {
5656
override fun onActivityCreated(savedInstanceState: Bundle?) {
5757
super.onActivityCreated(savedInstanceState)
5858

59+
setupViews()
60+
setupViewModels()
61+
62+
if (savedInstanceState == null) {
63+
username.requestFocus()
64+
65+
dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
66+
}
67+
}
68+
69+
private fun setupViews() {
5970
listOf(password, secret).forEach {
6071
it.editorActionEvents(Predicate { event -> event.actionId() == EditorInfo.IME_ACTION_GO })
6172
.filter { event -> event.actionId() == EditorInfo.IME_ACTION_GO }
@@ -71,7 +82,9 @@ class LoginDialog : BaseDialog() {
7182
}
7283

7384
secret.transformationMethod = null
85+
}
7486

87+
private fun setupViewModels() {
7588
viewModel.data.observe(this, Observer {
7689
it?.let {
7790
StorageHelper.user = LocalUser(it.loginToken, it.id, username.text.trim().toString(), it.image)
@@ -100,12 +113,6 @@ class LoginDialog : BaseDialog() {
100113
secret.imeOptions = if (it == true) EditorInfo.IME_ACTION_GO else EditorInfo.IME_ACTION_NEXT
101114
password.imeOptions = if (it == true) EditorInfo.IME_ACTION_NEXT else EditorInfo.IME_ACTION_GO
102115
})
103-
104-
if (savedInstanceState == null) {
105-
username.requestFocus()
106-
107-
dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
108-
}
109116
}
110117

111118
private fun validateAndLogin() {

src/main/kotlin/me/proxer/app/chat/sync/ChatJob.kt

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -115,66 +115,80 @@ class ChatJob : Job() {
115115
override fun onRunJob(params: Params): Result {
116116
if (StorageHelper.user == null) return Result.FAILURE
117117

118-
val synchronizationResult = conferenceId.let { conferenceId ->
119-
when (conferenceId) {
120-
0L -> try {
121-
val newConferencesAndMessages = synchronize()
118+
val synchronizationResult = when (conferenceId) {
119+
0L -> try {
120+
handleSynchronization()
121+
} catch (error: Throwable) {
122+
handleSynchronizationError(error)
123+
}
124+
else -> try {
125+
handleLoadMoreMessages(conferenceId)
126+
} catch (error: Throwable) {
127+
handleLoadMoreMessagesError(error)
128+
}
129+
}
122130

123-
StorageHelper.areConferencesSynchronized = true
131+
reschedule(context, synchronizationResult)
124132

125-
if (newConferencesAndMessages.isNotEmpty()) {
126-
bus.post(SynchronizationEvent())
133+
return if (synchronizationResult != ERROR) Result.SUCCESS else Result.FAILURE
134+
}
127135

128-
if (canShowNotification(context)) {
129-
showNotification(context)
130-
}
131-
}
136+
private fun handleSynchronization(): SynchronizationResult {
137+
val newConferencesAndMessages = synchronize()
132138

133-
newConferencesAndMessages.flatMap { it.value }.maxBy { it.date }?.date?.let { mostRecentDate ->
134-
if (mostRecentDate > StorageHelper.lastChatMessageDate) {
135-
StorageHelper.lastChatMessageDate = mostRecentDate
136-
}
137-
}
139+
StorageHelper.areConferencesSynchronized = true
138140

139-
if (newConferencesAndMessages.isNotEmpty()) CHANGES else NO_CHANGES
140-
} catch (error: Throwable) {
141-
when (error) {
142-
is ChatException -> bus.post(ChatErrorEvent(error))
143-
else -> bus.post(ChatErrorEvent(ChatSynchronizationException(error)))
144-
}
141+
if (newConferencesAndMessages.isNotEmpty()) {
142+
bus.post(SynchronizationEvent())
145143

146-
if (ErrorUtils.isIpBlockedError(error)) {
147-
ChatNotifications.showError(context, error)
144+
if (canShowNotification(context)) {
145+
showNotification(context)
146+
}
147+
}
148148

149-
ERROR
150-
} else {
151-
NO_CHANGES
152-
}
153-
}
154-
else -> try {
155-
val fetchedMessages = loadMoreMessages(conferenceId)
149+
newConferencesAndMessages.flatMap { it.value }.maxBy { it.date }?.date?.let { mostRecentDate ->
150+
if (mostRecentDate > StorageHelper.lastChatMessageDate) {
151+
StorageHelper.lastChatMessageDate = mostRecentDate
152+
}
153+
}
156154

157-
fetchedMessages.maxBy { it.date }?.date?.let { mostRecentDate ->
158-
if (mostRecentDate > StorageHelper.lastChatMessageDate) {
159-
StorageHelper.lastChatMessageDate = mostRecentDate
160-
}
161-
}
155+
return if (newConferencesAndMessages.isNotEmpty()) CHANGES else NO_CHANGES
156+
}
162157

163-
CHANGES
164-
} catch (error: Throwable) {
165-
when (error) {
166-
is ChatException -> bus.post(ChatErrorEvent(error))
167-
else -> bus.post(ChatErrorEvent(ChatMessageException(error)))
168-
}
158+
private fun handleSynchronizationError(error: Throwable): SynchronizationResult {
159+
when (error) {
160+
is ChatException -> bus.post(ChatErrorEvent(error))
161+
else -> bus.post(ChatErrorEvent(ChatSynchronizationException(error)))
162+
}
169163

170-
NO_CHANGES
171-
}
172-
}
164+
return if (ErrorUtils.isIpBlockedError(error)) {
165+
ChatNotifications.showError(context, error)
166+
167+
ERROR
168+
} else {
169+
NO_CHANGES
173170
}
171+
}
174172

175-
reschedule(context, synchronizationResult)
173+
private fun handleLoadMoreMessagesError(error: Throwable): SynchronizationResult {
174+
when (error) {
175+
is ChatException -> bus.post(ChatErrorEvent(error))
176+
else -> bus.post(ChatErrorEvent(ChatMessageException(error)))
177+
}
176178

177-
return if (synchronizationResult != ERROR) Result.SUCCESS else Result.FAILURE
179+
return NO_CHANGES
180+
}
181+
182+
private fun handleLoadMoreMessages(conferenceId: Long): SynchronizationResult {
183+
val fetchedMessages = loadMoreMessages(conferenceId)
184+
185+
fetchedMessages.maxBy { it.date }?.date?.let { mostRecentDate ->
186+
if (mostRecentDate > StorageHelper.lastChatMessageDate) {
187+
StorageHelper.lastChatMessageDate = mostRecentDate
188+
}
189+
}
190+
191+
return CHANGES
178192
}
179193

180194
private fun synchronize(): Map<LocalConference, List<LocalMessage>> {
@@ -231,7 +245,6 @@ class ChatJob : Job() {
231245

232246
// Per documentation: The api may return some String in case something went wrong.
233247
if (result != null) {
234-
235248
// Delete all messages we have correctly sent already.
236249
chatDatabase.runInTransaction {
237250
for (i in 0..index) {
@@ -279,12 +292,12 @@ class ChatJob : Job() {
279292
val mostRecentMessage = chatDao.findMostRecentMessageForConference(conference.id.toLong())?.toNonLocalMessage()
280293

281294
return when (mostRecentMessage) {
282-
null -> fetchNewMessagesForEmptyConference(conference)
283-
else -> fetchNewMessagesForExistingConference(conference, mostRecentMessage)
295+
null -> fetchForEmptyConference(conference)
296+
else -> fetchForExistingConference(conference, mostRecentMessage)
284297
}
285298
}
286299

287-
private fun fetchNewMessagesForEmptyConference(conference: Conference): Pair<List<Message>, Boolean> {
300+
private fun fetchForEmptyConference(conference: Conference): Pair<List<Message>, Boolean> {
288301
val newMessages = mutableListOf<Message>()
289302

290303
var unreadAmount = 0
@@ -311,7 +324,7 @@ class ChatJob : Job() {
311324
return newMessages to false
312325
}
313326

314-
private fun fetchNewMessagesForExistingConference(conference: Conference, mostRecentMessage: Message):
327+
private fun fetchForExistingConference(conference: Conference, mostRecentMessage: Message):
315328
Pair<List<Message>, Boolean> {
316329
val mostRecentMessageIdBeforeUpdate = mostRecentMessage.id.toLong()
317330
val newMessages = mutableListOf<Message>()

0 commit comments

Comments
 (0)