Skip to content
This repository was archived by the owner on Feb 17, 2020. It is now read-only.

Commit f52ebec

Browse files
authored
Merge pull request #559 from squanchy-dev/add_favourites_in_schedule
Add favourites in schedule
2 parents 177fd15 + c1c3a65 commit f52ebec

26 files changed

Lines changed: 256 additions & 187 deletions

app/src/debug/java/net/squanchy/support/debug/DebugActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class DebugActivity : AppCompatActivity() {
7575
experienceLevel = Option(ExperienceLevel.ADVANCED),
7676
speakers = createTalkSpeakers(),
7777
type = Event.Type.TALK,
78-
favorited = true,
78+
favorite = true,
7979
description = Option.empty(),
8080
track = Option(createTrack()),
8181
timeZone = DateTimeZone.forID("Europe/Rome")

app/src/main/java/net/squanchy/eventdetails/EventDetailsService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal class EventDetailsService(
3333
}
3434

3535
private fun toggleFavoriteOn(event: Event): Completable {
36-
return if (event.favorited) {
36+
return if (event.favorite) {
3737
removeFavorite(event.id)
3838
} else {
3939
favorite(event.id)

app/src/main/java/net/squanchy/eventdetails/widget/EventDetailsCoordinatorLayout.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class EventDetailsCoordinatorLayout @JvmOverloads constructor(
2222

2323
if (canBeFavorited(event)) {
2424
favoriteFab.setImageResource(
25-
if (event.favorited)
25+
if (event.favorite)
2626
R.drawable.ic_favorite_filled
2727
else R.drawable.ic_favorite_empty
2828
)

app/src/main/java/net/squanchy/favorites/FavoritesComponent.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import net.squanchy.injection.ApplicationComponent
99
import net.squanchy.injection.applicationComponent
1010
import net.squanchy.navigation.NavigationModule
1111
import net.squanchy.navigation.Navigator
12+
import net.squanchy.remoteconfig.FeatureFlags
1213

1314
@ActivityLifecycle
1415
@Component(modules = [FavoritesModule::class, NavigationModule::class], dependencies = [ApplicationComponent::class])
@@ -19,6 +20,8 @@ internal interface FavoritesComponent {
1920
fun navigator(): Navigator
2021

2122
fun analytics(): Analytics
23+
24+
fun featureFlags(): FeatureFlags
2225
}
2326

2427
internal fun favoritesComponent(activity: AppCompatActivity): FavoritesComponent {

app/src/main/java/net/squanchy/favorites/FavoritesPageView.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import androidx.view.isVisible
88
import io.reactivex.Observable
99
import io.reactivex.android.schedulers.AndroidSchedulers
1010
import io.reactivex.disposables.CompositeDisposable
11-
import io.reactivex.functions.BiFunction
11+
import io.reactivex.functions.Function3
1212
import io.reactivex.schedulers.Schedulers
1313
import kotlinx.android.synthetic.main.merge_no_favorites_view.view.*
1414
import kotlinx.android.synthetic.main.view_page_favorites.view.*
@@ -19,6 +19,7 @@ import net.squanchy.favorites.view.FavoritesItem
1919
import net.squanchy.home.HomeActivity
2020
import net.squanchy.home.Loadable
2121
import net.squanchy.navigation.Navigator
22+
import net.squanchy.remoteconfig.FeatureFlags
2223
import net.squanchy.schedule.domain.view.Event
2324
import net.squanchy.support.unwrapToActivityContext
2425
import timber.log.Timber
@@ -32,6 +33,7 @@ class FavoritesPageView @JvmOverloads constructor(
3233
private lateinit var favoritesService: FavoritesService
3334
private lateinit var navigator: Navigator
3435
private lateinit var analytics: Analytics
36+
private lateinit var featureFlags: FeatureFlags
3537

3638
private val disposable = CompositeDisposable()
3739

@@ -41,6 +43,7 @@ class FavoritesPageView @JvmOverloads constructor(
4143
favoritesService = favoritesService()
4244
navigator = navigator()
4345
analytics = analytics()
46+
featureFlags = featureFlags()
4447
}
4548
}
4649
}
@@ -66,7 +69,8 @@ class FavoritesPageView @JvmOverloads constructor(
6669
Observable.combineLatest(
6770
favoritesService.favorites(),
6871
favoritesService.currentUserIsSignedIn(),
69-
BiFunction<List<FavoritesItem>, Boolean, LoadResult>(::LoadResult)
72+
featureFlags.showEventRoomInSchedule.toObservable(),
73+
Function3<List<FavoritesItem>, Boolean, Boolean, LoadResult>(::LoadResult)
7074
)
7175
.subscribeOn(Schedulers.io())
7276
.observeOn(AndroidSchedulers.mainThread())
@@ -92,14 +96,14 @@ class FavoritesPageView @JvmOverloads constructor(
9296

9397
private fun handleLoadSchedule(result: LoadResult) {
9498
when {
95-
result.favoriteItems.isNotEmpty() -> showFavorites(result.favoriteItems)
99+
result.favoriteItems.isNotEmpty() -> showFavorites(result.favoriteItems, result.showRoom)
96100
result.signedIn -> promptToFavorite()
97101
else -> promptToSign()
98102
}
99103
}
100104

101-
private fun showFavorites(favorites: List<FavoritesItem>) {
102-
favoritesListView.updateWith(favorites, ::navigateToEventDetails)
105+
private fun showFavorites(favorites: List<FavoritesItem>, showRoom: Boolean) {
106+
favoritesListView.updateWith(favorites, showRoom, ::navigateToEventDetails)
103107
favoritesListView.isVisible = true
104108
progressBar.isVisible = false
105109
emptyViewSignedOut.isVisible = false
@@ -137,5 +141,5 @@ class FavoritesPageView @JvmOverloads constructor(
137141

138142
private fun showSettings() = navigator.toSettings()
139143

140-
private data class LoadResult(val favoriteItems: List<FavoritesItem>, val signedIn: Boolean)
144+
private data class LoadResult(val favoriteItems: List<FavoritesItem>, val signedIn: Boolean, val showRoom: Boolean)
141145
}

app/src/main/java/net/squanchy/favorites/FavoritesService.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.reactivex.Observable
44
import net.squanchy.favorites.view.FavoritesItem
55
import net.squanchy.schedule.ScheduleService
66
import net.squanchy.schedule.domain.view.Event
7+
import net.squanchy.schedule.domain.view.SchedulePage
78
import net.squanchy.service.repository.AuthService
89
import net.squanchy.support.lang.or
910

@@ -20,23 +21,28 @@ internal class FirestoreFavoritesService(
2021
) : FavoritesService {
2122

2223
override fun favorites(): Observable<List<FavoritesItem>> {
23-
return scheduleService.schedule(onlyFavorites = true)
24+
return scheduleService.schedule()
2425
.map { schedule -> schedule.pages }
2526
.flatMap { pages ->
26-
val flattenedItems = pages.map { page ->
27-
val eventsAsFavoriteItems = page.events.map { it.toFavoriteItem() }
28-
29-
if (eventsAsFavoriteItems.isNotEmpty()) {
30-
return@map listOf(FavoritesItem.Header(page.date)) + eventsAsFavoriteItems
31-
} else {
32-
return@map emptyList<FavoritesItem>()
33-
}
34-
}.flatten()
27+
val flattenedItems = pages.map { page -> page.asFavouritesDayViewModel() }
28+
.flatten()
3529

3630
return@flatMap Observable.just(flattenedItems)
3731
}
3832
}
3933

34+
private fun SchedulePage.asFavouritesDayViewModel(): List<FavoritesItem> {
35+
val eventsAsFavoriteItems = this.events
36+
.filter { it.favorite }
37+
.map { it.toFavoriteItem() }
38+
39+
if (eventsAsFavoriteItems.isNotEmpty()) {
40+
return listOf(FavoritesItem.Header(date)) + eventsAsFavoriteItems
41+
} else {
42+
return emptyList()
43+
}
44+
}
45+
4046
override fun currentUserIsSignedIn(): Observable<Boolean> {
4147
return authService.currentUser()
4248
.map { optionalUser -> optionalUser.map { user -> !user.isAnonymous }.or(false) }

app/src/main/java/net/squanchy/favorites/view/FavoritesAdapter.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ internal class FavoritesAdapter(
1717
private const val VIEW_TYPE_HEADER: Int = 2
1818
}
1919

20-
lateinit var favoriteClickListener: OnFavoriteClickListener
20+
private var favoriteClickListener: OnFavoriteClickListener? = null
21+
private var showRoom = false
2122

2223
private val layoutInflater = LayoutInflater.from(context)
2324

@@ -50,10 +51,25 @@ internal class FavoritesAdapter(
5051

5152
override fun onBindViewHolder(holder: FavoritesViewHolder<*>, position: Int) {
5253
when (holder) {
53-
is EventViewHolder -> holder.updateWith((getItem(position) as FavoritesItem.Favorite).event, favoriteClickListener)
54+
is EventViewHolder -> holder.updateWith((getItem(position) as FavoritesItem.Favorite).event, showRoom, favoriteClickListener)
5455
is HeaderViewHolder -> holder.updateWith((getItem(position) as FavoritesItem.Header).date)
5556
}
5657
}
58+
59+
fun updateWith(list: List<FavoritesItem>, showRoom: Boolean, listener: OnFavoriteClickListener) {
60+
this.showRoom = showRoom
61+
this.favoriteClickListener = listener
62+
super.submitList(list)
63+
}
64+
65+
@Deprecated(
66+
message = "Use updateWith() instead",
67+
replaceWith = ReplaceWith("updateWith(list, showRoom, eventClickListener)"),
68+
level = DeprecationLevel.ERROR
69+
)
70+
override fun submitList(list: MutableList<FavoritesItem>?) {
71+
throw UnsupportedOperationException("Use updateWith() instead")
72+
}
5773
}
5874

5975
private object DiffCallback : DiffUtil.ItemCallback<FavoritesItem>() {

app/src/main/java/net/squanchy/favorites/view/FavoritesListView.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ internal class FavoritesListView @JvmOverloads constructor(
2727
addItemDecoration(CardSpacingItemDecorator(horizontalSpacing, verticalSpacing))
2828
}
2929

30-
fun updateWith(newData: List<FavoritesItem>, listener: OnFavoriteClickListener) {
31-
adapter.favoriteClickListener = listener
30+
fun updateWith(newData: List<FavoritesItem>, showRoom: Boolean, listener: OnFavoriteClickListener) {
3231
setAdapterIfNone(adapter)
33-
34-
adapter.submitList(newData)
32+
adapter.updateWith(newData, showRoom, listener)
3533
}
3634
}

app/src/main/java/net/squanchy/favorites/view/FavoritesViewHolders.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ sealed class FavoritesViewHolder<T : View>(itemView: T) : RecyclerView.ViewHolde
1818

1919
class EventViewHolder(itemView: EventItemView) : FavoritesViewHolder<EventItemView>(itemView) {
2020

21-
fun updateWith(event: Event, listener: OnEventClickListener?) {
22-
(itemView as EventItemView).updateWith(event, false) // TODO import the feature flag here as well
21+
fun updateWith(event: Event, showRoom: Boolean, listener: OnEventClickListener?) {
22+
(itemView as EventItemView).updateWith(event, showRoom, showFavorite = false)
2323
itemView.setOnClickListener { listener?.invoke(event) }
2424
}
2525
}

app/src/main/java/net/squanchy/notification/NotificationService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal class NotificationService(private val authService: AuthService, private
1111
fun sortedFavourites(): Observable<List<Event>> {
1212
return authService.ifUserSignedInThenObservableFrom { userId ->
1313
eventRepository.events(userId)
14-
.map { it.filter { it.favorited } }
14+
.map { it.filter { it.favorite } }
1515
.map { it.sortedBy { it.startTime } }
1616
.take(1)
1717
.subscribeOn(Schedulers.io())

0 commit comments

Comments
 (0)