Skip to content

feat: Check for new notifications #1988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,815 changes: 1,815 additions & 0 deletions app/schemas/org.fossasia.openevent.general.OpenEventDatabase/7.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import org.fossasia.openevent.general.event.topic.EventTopicsDao
import org.fossasia.openevent.general.event.types.EventTypeConverter
import org.fossasia.openevent.general.feedback.Feedback
import org.fossasia.openevent.general.feedback.FeedbackDao
import org.fossasia.openevent.general.notification.Notification
import org.fossasia.openevent.general.notification.NotificationDao
import org.fossasia.openevent.general.order.Order
import org.fossasia.openevent.general.order.OrderDao
import org.fossasia.openevent.general.sessions.Session
Expand All @@ -47,7 +49,7 @@ import org.fossasia.openevent.general.ticket.TicketIdConverter

@Database(entities = [Event::class, User::class, SocialLink::class, Ticket::class, Attendee::class,
EventTopic::class, Order::class, CustomForm::class, Speaker::class, SpeakerWithEvent::class, Sponsor::class,
SponsorWithEvent::class, Session::class, SpeakersCall::class, Feedback::class], version = 6)
SponsorWithEvent::class, Session::class, SpeakersCall::class, Feedback::class, Notification::class], version = 7)
@TypeConverters(EventIdConverter::class, EventTopicConverter::class, EventTypeConverter::class,
EventSubTopicConverter::class, TicketIdConverter::class, MicroLocationConverter::class, UserIdConverter::class,
AttendeeIdConverter::class, ListAttendeeIdConverter::class, SessionTypeConverter::class, TrackConverter::class,
Expand Down Expand Up @@ -81,4 +83,6 @@ abstract class OpenEventDatabase : RoomDatabase() {
abstract fun speakersCallDao(): SpeakersCallDao

abstract fun feedbackDao(): FeedbackDao

abstract fun notificationDao(): NotificationDao
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ val apiModule = module {
factory { AttendeeService(get(), get(), get()) }
factory { OrderService(get(), get(), get()) }
factory { SessionService(get(), get()) }
factory { NotificationService(get()) }
factory { NotificationService(get(), get()) }
factory { FeedbackService(get(), get()) }
}

val viewModelModule = module {
viewModel { LoginViewModel(get(), get(), get()) }
viewModel { EventsViewModel(get(), get(), get(), get()) }
viewModel { EventsViewModel(get(), get(), get(), get(), get(), get()) }
viewModel { ProfileViewModel(get(), get()) }
viewModel { SignUpViewModel(get(), get(), get()) }
viewModel { EventDetailsViewModel(get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
Expand Down Expand Up @@ -363,4 +363,9 @@ val databaseModule = module {
val database: OpenEventDatabase = get()
database.speakersCallDao()
}

factory {
val database: OpenEventDatabase = get()
database.notificationDao()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.android.synthetic.main.fragment_events.view.emptyEventsText
import kotlinx.android.synthetic.main.fragment_events.view.scrollView
import kotlinx.android.synthetic.main.fragment_events.view.notification
import kotlinx.android.synthetic.main.fragment_events.view.swiperefresh
import kotlinx.android.synthetic.main.fragment_events.view.newNotificationDot
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.BottomIconDoubleClick
import org.fossasia.openevent.general.common.EventClickListener
Expand Down Expand Up @@ -67,6 +68,9 @@ class EventsFragment : Fragment(), BottomIconDoubleClick {
rootView.eventsRecycler.adapter = eventsListAdapter
rootView.eventsRecycler.isNestedScrollingEnabled = false

eventsViewModel.getNotifications()
rootView.newNotificationDot.isVisible = preference.getBoolean(NEW_NOTIFICATIONS, false)

eventsViewModel.showShimmerEvents
.nonNull()
.observe(viewLifecycleOwner, Observer { shouldShowShimmer ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.auth.AuthHolder
import org.fossasia.openevent.general.common.SingleLiveEvent
import org.fossasia.openevent.general.connectivity.MutableConnectionLiveData
import org.fossasia.openevent.general.data.Preference
import org.fossasia.openevent.general.data.Resource
import org.fossasia.openevent.general.notification.Notification
import org.fossasia.openevent.general.notification.NotificationService
import org.fossasia.openevent.general.search.SAVED_LOCATION
import org.fossasia.openevent.general.utils.extensions.withDefaultSchedulers
import timber.log.Timber

const val NEW_NOTIFICATIONS = "newNotifications"

class EventsViewModel(
private val eventService: EventService,
private val preference: Preference,
private val resource: Resource,
private val mutableConnectionLiveData: MutableConnectionLiveData
private val mutableConnectionLiveData: MutableConnectionLiveData,
private val authHolder: AuthHolder,
private val notificationService: NotificationService
) : ViewModel() {

private val compositeDisposable = CompositeDisposable()
Expand All @@ -35,6 +42,11 @@ class EventsViewModel(
var lastSearch = ""
private val mutableSavedLocation = MutableLiveData<String>()
val savedLocation: LiveData<String> = mutableSavedLocation
private var oldNotifications: List<Notification>? = null

fun isLoggedIn() = authHolder.isLoggedIn()

fun getId() = authHolder.getId()

fun loadLocation() {
mutableSavedLocation.value = preference.getString(SAVED_LOCATION)
Expand Down Expand Up @@ -107,6 +119,36 @@ class EventsViewModel(
})
}

fun getNotifications() {
if (!isLoggedIn())
return

compositeDisposable += notificationService.getNotifications(getId())
.withDefaultSchedulers()
.subscribe({ list ->
oldNotifications = list
syncNotifications()
}, {
Timber.e(it, "Error fetching notifications")
})
}

private fun syncNotifications() {
compositeDisposable += notificationService.syncNotifications(getId())
.withDefaultSchedulers()
.subscribe({ list ->
list?.let { checkNewNotifications(it) }
}, {
Timber.e(it, "Error fetching notifications")
})
}

private fun checkNewNotifications(newNotifications: List<Notification>) {
if (newNotifications.size != oldNotifications?.size) {
preference.putBoolean(NEW_NOTIFICATIONS, true)
}
}

override fun onCleared() {
super.onCleared()
compositeDisposable.clear()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.fossasia.openevent.general.notification

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.fasterxml.jackson.databind.PropertyNamingStrategy
import com.fasterxml.jackson.databind.annotation.JsonNaming
import com.github.jasminb.jsonapi.IntegerIdHandler
Expand All @@ -9,9 +11,11 @@ import io.reactivex.annotations.NonNull

@Type("notification")
@JsonNaming(PropertyNamingStrategy.KebabCaseStrategy::class)
@Entity
data class Notification(
@Id(IntegerIdHandler::class)
@NonNull
@PrimaryKey
val id: Long,
val message: String? = null,
val receivedAt: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import retrofit2.http.Path

interface NotificationApi {

@GET("users/{userId}/notifications?sort=message")
@GET("users/{userId}/notifications?sort=-received-at")
fun getNotifications(@Path("userId") userId: Long): Single<List<Notification>>

@PATCH("notifications/{notification_id}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.fossasia.openevent.general.notification

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy.REPLACE
import androidx.room.Query
import io.reactivex.Single

@Dao
interface NotificationDao {
@Insert(onConflict = REPLACE)
fun insertNotifications(notifications: List<Notification>)

@Query("SELECT * FROM Notification")
fun getNotifications(): Single<List<Notification>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import kotlinx.android.synthetic.main.fragment_notification.view.notificationCoo
import kotlinx.android.synthetic.main.fragment_notification.view.noNotification
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.auth.LoginFragmentArgs
import org.fossasia.openevent.general.data.Preference
import org.fossasia.openevent.general.event.NEW_NOTIFICATIONS
import org.fossasia.openevent.general.utils.Utils.setToolbar
import org.fossasia.openevent.general.utils.extensions.nonNull
import org.jetbrains.anko.design.snackbar
Expand All @@ -30,6 +32,7 @@ class NotificationFragment : Fragment() {
private val notificationViewModel by viewModel<NotificationViewModel>()
private val recyclerAdapter = NotificationsRecyclerAdapter()
private lateinit var rootView: View
private val preference = Preference()

override fun onStart() {
super.onStart()
Expand All @@ -54,6 +57,7 @@ class NotificationFragment : Fragment() {
}
rootView.notificationRecycler.layoutManager = LinearLayoutManager(requireContext())
rootView.notificationRecycler.adapter = recyclerAdapter
preference.putBoolean(NEW_NOTIFICATIONS, false)
}
return rootView
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@ package org.fossasia.openevent.general.notification
import io.reactivex.Single

class NotificationService(
private val notificationApi: NotificationApi
private val notificationApi: NotificationApi,
private val notificationDao: NotificationDao
) {

fun getNotifications(userId: Long): Single<List<Notification>> {
return notificationApi.getNotifications(userId)
return notificationDao.getNotifications()
.onErrorResumeNext {
notificationApi.getNotifications(userId).map {
notificationDao.insertNotifications(it)
it
}
}
}

fun syncNotifications(userId: Long): Single<List<Notification>> {
return notificationApi.getNotifications(userId).map {
notificationDao.insertNotifications(it)
it
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/ic_circle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<vector android:height="24dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/holo_red_light" android:pathData="M256,0C114.837,0 0,114.837 0,256s114.837,256 256,256s256,-114.837 256,-256S397.163,0 256,0z"/>
</vector>
21 changes: 16 additions & 5 deletions app/src/main/res/layout/fragment_events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,26 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/notification"

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_large"
android:background="?selectableItemBackground"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@drawable/ic_notifications_white"/>
android:padding="@dimen/padding_large">
<ImageView
android:id="@+id/notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:src="@drawable/ic_notifications_white"/>
<ImageView
android:id="@+id/newNotificationDot"
android:layout_width="@dimen/text_size_small"
android:layout_height="@dimen/text_size_small"
android:src="@drawable/ic_circle"/>
</RelativeLayout>

<ImageView
android:id="@+id/eventIcon"
android:layout_width="60dp"
Expand Down