Skip to content

Commit

Permalink
[NoTicket] various RXjava cleanup (#2149)
Browse files Browse the repository at this point in the history
* various RXjava cleanup

* lint
  • Loading branch information
mtgriego authored Oct 9, 2024
1 parent b891f29 commit 852dae9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 283 deletions.
79 changes: 0 additions & 79 deletions app/src/main/java/com/kickstarter/ui/adapters/MessagesAdapter.java

This file was deleted.

77 changes: 77 additions & 0 deletions app/src/main/java/com/kickstarter/ui/adapters/MessagesAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.kickstarter.ui.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.kickstarter.R
import com.kickstarter.databinding.MessageCenterTimestampLayoutBinding
import com.kickstarter.databinding.MessageViewBinding
import com.kickstarter.models.Message
import com.kickstarter.ui.viewholders.KSViewHolder
import com.kickstarter.ui.viewholders.MessageCenterTimestampViewHolder
import com.kickstarter.ui.viewholders.MessageViewHolder
import org.joda.time.DateTime

class MessagesAdapter : KSAdapter() {
private fun getLayoutId(sectionRow: SectionRow): Int {
if (objectFromSectionRow(sectionRow) is DateTime) {
return R.layout.message_center_timestamp_layout
} else if (objectFromSectionRow(sectionRow) is Message) {
return R.layout.message_view
}
return R.layout.empty_view
}

fun messages(messages: List<Message>) {
clearSections()

// Group messages by start of day.
messages
.groupBy { it.createdAt().withTimeAtStartOfDay() }
.forEach { dateAndMessages ->
addSection(listOf(dateAndMessages.key))
dateAndMessages.value.forEach { message -> addSection(listOf(message)) }
}

notifyDataSetChanged()
}

override fun layout(sectionRow: SectionRow): Int {
return getLayoutId(sectionRow)
}

override fun onBindViewHolder(
holder: KSViewHolder,
position: Int,
payloads: List<Any>
) {
super.onBindViewHolder(holder, position, payloads)

if (holder is MessageViewHolder) {
// Let the MessageViewHolder know if it is the last position in the RecyclerView.
holder.isLastPosition(position == itemCount - 1)
}
}

override fun viewHolder(@LayoutRes layout: Int, viewGroup: ViewGroup): KSViewHolder {
return when (layout) {
R.layout.message_center_timestamp_layout -> MessageCenterTimestampViewHolder(
MessageCenterTimestampLayoutBinding.inflate(
LayoutInflater.from(viewGroup.context),
viewGroup,
false
)
)

R.layout.message_view -> MessageViewHolder(
MessageViewBinding.inflate(
LayoutInflater.from(viewGroup.context),
viewGroup,
false
)
)

else -> throw IllegalStateException("Invalid layout.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ import com.kickstarter.libs.RefTag
import com.kickstarter.libs.utils.KsOptional
import com.kickstarter.libs.utils.extensions.query
import com.kickstarter.models.Project
import com.kickstarter.services.ApiClientType
import com.kickstarter.services.ApiClientTypeV2
import com.kickstarter.services.ApolloClientType
import com.kickstarter.services.ApolloClientTypeV2
import com.kickstarter.services.apiresponses.PushNotificationEnvelope
import com.kickstarter.ui.IntentKey
import rx.Observable
import java.util.regex.Pattern

object ProjectIntentMapper {
Expand Down Expand Up @@ -48,50 +44,6 @@ object ProjectIntentMapper {
.mergeWith(projectFromParceledParam)
}

fun project(intent: Intent, apolloClient: ApolloClientType): Observable<Project> {
val intentProject = projectFromIntent(intent)
val projectFromParceledProject =
if (intentProject == null) Observable.empty() else Observable.just(intentProject)
.switchMap { project: Project? ->
project?.let { apolloClient.getProject(it) }
}
.startWith(intentProject)
.retry(3)

val projectFromParceledParam = Observable.just(paramFromIntent(intent) ?: "")
.filter { it.isNotEmpty() }
.switchMap { slug: String? ->
slug?.let { apolloClient.getProject(it) }
}
.retry(3)
return projectFromParceledProject
.mergeWith(projectFromParceledParam).last()
}

/**
* Returns an observable of projects retrieved from intent data. May hit the API if the intent only contains a project
* param rather than a parceled project.
*/
fun project(intent: Intent, client: ApiClientType): Observable<Project> {
val intentProject = projectFromIntent(intent)
val projectFromParceledProject =
if (intentProject == null) Observable.empty() else Observable.just(intentProject)
.flatMap { project: Project? ->
project?.let { client.fetchProject(it) }
}
.startWith(intentProject)
.retry(3)

val projectFromParceledParam = Observable.just(paramFromIntent(intent) ?: "")
.filter { it.isNotEmpty() }
.flatMap { param: String? ->
param?.let { client.fetchProject(it) }
}
.retry(3)
return projectFromParceledProject
.mergeWith(projectFromParceledParam)
}

/**
* Returns an observable of projects retrieved from intent data. May hit the API if the intent only contains a project
* param rather than a parceled project.
Expand Down Expand Up @@ -130,15 +82,6 @@ object ProjectIntentMapper {
return io.reactivex.Observable.just(intent.getBooleanExtra(IntentKey.SAVE_FLAG_VALUE, false))
}

/**
* Returns an observable of push notification envelopes from the intent data. This will emit only when the project
* is launched from a push notification.
*/
fun pushNotificationEnvelope(intent: Intent): Observable<PushNotificationEnvelope> {
return Observable.just<Any?>(intent.getParcelableExtra(IntentKey.PUSH_NOTIFICATION_ENVELOPE))
.ofType(PushNotificationEnvelope::class.java)
}

/**
* Gets a parceled project from the intent data, may return `null`.
*/
Expand Down

This file was deleted.

Loading

0 comments on commit 852dae9

Please sign in to comment.