Skip to content

Commit

Permalink
[MBL-812] Update DiscoveryViewModel to RX2 (#2152)
Browse files Browse the repository at this point in the history
* initial work to get RX2 updated for discoveryviewmodel (WIP)

* WIP remove more RX2 implementation, remove RXView stuff, make discovery work

* WIP adjust tests for discoveryViewmodel

* update user to always have a value

* fix a bunch of tests, one to go

* revert changes that broke a couple tests

* fix the last test!

* remove unused variables, fix logged out container logic for being shown

* try adding import to fix circle CI

* remove unused import

* fix internal tools issue

* lint

* more lint

* STOP IT LINT

* update segment tests for new default properties
  • Loading branch information
mtgriego authored Oct 17, 2024
1 parent 675b6ea commit ee0382e
Show file tree
Hide file tree
Showing 21 changed files with 707 additions and 689 deletions.
22 changes: 0 additions & 22 deletions app/src/internal/java/com/kickstarter/libs/InternalTools.java

This file was deleted.

17 changes: 17 additions & 0 deletions app/src/internal/java/com/kickstarter/libs/InternalTools.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kickstarter.libs

import android.content.Intent
import com.kickstarter.R
import com.kickstarter.ui.activities.InternalToolsActivity
import com.kickstarter.ui.extensions.startActivityWithTransition

class InternalTools : InternalToolsType {
override fun maybeStartInternalToolsActivity(activity: android.app.Activity) {
val intent = Intent(activity, InternalToolsActivity::class.java)
activity.startActivityWithTransition(intent, R.anim.slide_in_right, R.anim.fade_out_slide_out_left)
}

override fun basicAuthorizationHeader(): String {
return "Basic ZnV6enk6d3V6enk="
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/kickstarter/libs/AnalyticEvents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ class AnalyticEvents(trackingClients: List<TrackingClientType?>) {
props[CONTEXT_LOCATION.contextName] = DISCOVER_ADVANCED.contextName
props[CONTEXT_PAGE.contextName] = DISCOVER.contextName
props[CONTEXT_TYPE.contextName] = when {
discoveryParams.recommended().isTrue() -> RECOMMENDED.contextName
discoveryParams.category()?.isRoot.isTrue() ||
discoveryParams.category() != null ||
discoveryParams.staffPicks().isTrue() ||
discoveryParams.isAllProjects.isTrue() -> RESULTS.contextName
discoveryParams.recommended().isTrue() -> RECOMMENDED.contextName
else -> ""
}

Expand Down
9 changes: 0 additions & 9 deletions app/src/main/java/com/kickstarter/libs/InternalToolsType.java

This file was deleted.

8 changes: 8 additions & 0 deletions app/src/main/java/com/kickstarter/libs/InternalToolsType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.kickstarter.libs

import android.app.Activity

interface InternalToolsType {
fun maybeStartInternalToolsActivity(activity: Activity)
fun basicAuthorizationHeader(): String?
}
14 changes: 0 additions & 14 deletions app/src/main/java/com/kickstarter/libs/NoopInternalTools.java

This file was deleted.

11 changes: 11 additions & 0 deletions app/src/main/java/com/kickstarter/libs/NoopInternalTools.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kickstarter.libs

import android.app.Activity

class NoopInternalTools : InternalToolsType {
override fun maybeStartInternalToolsActivity(activity: Activity) {}

override fun basicAuthorizationHeader(): String? {
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.kickstarter.libs.utils.RewardUtils.isTimeLimitedEnd
import com.kickstarter.libs.utils.extensions.addOnsCost
import com.kickstarter.libs.utils.extensions.bonus
import com.kickstarter.libs.utils.extensions.intValueOrZero
import com.kickstarter.libs.utils.extensions.isFalse
import com.kickstarter.libs.utils.extensions.isNonZero
import com.kickstarter.libs.utils.extensions.isTrue
import com.kickstarter.libs.utils.extensions.refTag
Expand Down Expand Up @@ -96,7 +97,7 @@ object AnalyticEventsUtils {
@JvmOverloads
fun discoveryParamsProperties(params: DiscoveryParams, discoverSort: DiscoveryParams.Sort? = params.sort(), prefix: String = "discover_"): Map<String, Any> {
val properties = HashMap<String, Any>().apply {
put("everything", params.isAllProjects.isTrue())
put("everything", params.isAllProjects.isTrue() && params.recommended()?.isFalse() ?: true)
put("pwl", params.staffPicks().isTrue())
put("recommended", params.recommended()?.isTrue() ?: false)
params.refTag()?.tag()?.let { put("ref_tag", it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.kickstarter.models.Category
import com.kickstarter.models.User
import com.kickstarter.services.DiscoveryParams
import com.kickstarter.ui.adapters.data.NavigationDrawerData
import rx.Observable
import type.ProjectSort
import java.util.TreeMap

Expand Down Expand Up @@ -61,38 +60,32 @@ fun DiscoveryParams.refTag(): RefTag {
* @param user The currently logged in user.
*/
fun DiscoveryParams.deriveNavigationDrawerData(
categories: List<Category>,
expandedCategory: Category?,
user: User?
categories: List<Category> = listOf(),
expandedCategory: Category? = null,
user: User
): NavigationDrawerData {
val builder = NavigationDrawerData.builder()

val categorySections = Observable.from(categories)
.filter {
isVisible(it, expandedCategory)
}
.flatMap {
doubleRootIfExpanded(it, expandedCategory)
}
.map {
DiscoveryParams.builder().category(it).build()
}
val visible = categories
.filter { isVisible(it, expandedCategory) }
.flatMap { doubleRootIfExpanded(it, expandedCategory) }
.map { DiscoveryParams.builder().category(it).build() }
.toList()
.map {
paramsGroupedByRootCategory(it)
}.map {
sectionsFromAllParams(it, expandedCategory)
}
.toBlocking().single()
val sectionsForVisible = paramsGroupedByRootCategory(visible)
val sectionsFromAllParamsVisible = sectionsFromAllParams(sectionsForVisible, expandedCategory)
val topSections = if (user == User.builder().build()) {
topSections(null)
} else {
topSections(user)
}

val sections = Observable
.from(categorySections)
.startWith(topSections(user))
.toList().toBlocking().single()
val allSections = topSections.toMutableList()
allSections.addAll(sectionsFromAllParamsVisible)
allSections.toList()

return builder
.sections(sections)
.user(user)
.sections(allSections)
.user(if (user == User.builder().build()) null else user)
.selectedParams(this)
.expandedCategory(expandedCategory)
.build()
Expand All @@ -104,7 +97,7 @@ fun DiscoveryParams.deriveNavigationDrawerData(
* @param expandedCategory The category that is currently expandable, possible `null`.
*/
private fun isVisible(category: Category, expandedCategory: Category?): Boolean {
if (expandedCategory == null) {
if (expandedCategory == null || category.id() == 0L) {
return category.isRoot
}
return if (category.isRoot) {
Expand All @@ -120,7 +113,7 @@ private fun sectionsFromAllParams(
sections: List<List<DiscoveryParams>>,
expandedCategory: Category?
): List<NavigationDrawerData.Section> {
return Observable.from(sections)
return sections
.map {
rowsFromParams(it)
}.map {
Expand All @@ -135,18 +128,18 @@ private fun sectionsFromAllParams(
.expanded(it.second)
.build()
}
.toList().toBlocking().single()
.toList()
}

/**
* Converts a list of params into a list of rows that the drawer can use to display rows.
*/
private fun rowsFromParams(params: List<DiscoveryParams>): List<NavigationDrawerData.Section.Row> {
return Observable.from(params)
return params
.map {
NavigationDrawerData.Section.Row.builder().params(it).build()
}
.toList().toBlocking().single()
.toList()
}

/**
Expand All @@ -157,7 +150,7 @@ private fun rowsAreExpanded(
expandedCategory: Category?
): Boolean {
val sectionCategory = rows[0].params().category()
return sectionCategory != null && expandedCategory != null && sectionCategory.rootId() == expandedCategory.rootId()
return sectionCategory != null && expandedCategory != null && expandedCategory.id() != 0L && sectionCategory.rootId() == expandedCategory.rootId()
}

/**
Expand All @@ -169,13 +162,13 @@ private fun rowsAreExpanded(
private fun doubleRootIfExpanded(
category: Category,
expandedCategory: Category?
): Observable<Category> {
): List<Category> {
if (expandedCategory == null) {
return Observable.just(category)
return listOf(category)
}
return if (category.isRoot && category.id() == expandedCategory.id()) {
Observable.just(category, category)
} else Observable.just(category)
listOf(category, category)
} else listOf(category)
}

/**
Expand All @@ -202,13 +195,13 @@ private fun topSections(user: User?): List<NavigationDrawerData.Section> {
}*/
}

return Observable.from(filters)
return filters
.map {
NavigationDrawerData.Section.Row.builder().params(it).build()
}
.map { listOf(it) }
.map { NavigationDrawerData.Section.builder().rows(it).build() }
.toList().toBlocking().single()
.toList()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,15 @@ open class MockApolloClientV2 : ApolloClientTypeV2 {
}

override fun fetchCategory(param: String): io.reactivex.Observable<Category?> {
return io.reactivex.Observable.empty()
return io.reactivex.Observable.just(CategoryFactory.musicCategory())
}

override fun getBacking(backingId: String): io.reactivex.Observable<Backing> {
return io.reactivex.Observable.just(BackingFactory.backing())
}

override fun fetchCategories(): io.reactivex.Observable<List<Category>> {
return io.reactivex.Observable.empty()
return io.reactivex.Observable.just(CategoryFactory.rootCategories())
}

override fun getProjectUpdates(
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/kickstarter/services/DiscoveryParams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.kickstarter.libs.utils.extensions.isDiscoverPlacesPath
import com.kickstarter.libs.utils.extensions.isDiscoverScopePath
import com.kickstarter.libs.utils.extensions.isDiscoverSortParam
import com.kickstarter.libs.utils.extensions.isFalse
import com.kickstarter.libs.utils.extensions.isNull
import com.kickstarter.libs.utils.extensions.isTrue
import com.kickstarter.libs.utils.extensions.toInteger
import com.kickstarter.models.Category
Expand Down Expand Up @@ -345,8 +346,9 @@ class DiscoveryParams private constructor(
*/
val isAllProjects: Boolean
get() = (
staffPicks().isFalse() && (starred() == null || starred() != 1) && (backed() == null || backed() != 1) &&
(social() == null || social() != 1) && category() == null && location() == null && recommended().isFalse() &&
(staffPicks().isFalse() || staffPicks().isNull()) &&
(starred() == null || starred() != 1) && (backed() == null || backed() != 1) &&
(social() == null || social() != 1) && category() == null && location() == null &&
tagId() == null
)

Expand Down
Loading

0 comments on commit ee0382e

Please sign in to comment.