-
Notifications
You must be signed in to change notification settings - Fork 551
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
feat: Notification Fragment #1622
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
app/src/main/java/org/fossasia/openevent/general/notification/Notification.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.fossasia.openevent.general.notification | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategy | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming | ||
import com.github.jasminb.jsonapi.IntegerIdHandler | ||
import com.github.jasminb.jsonapi.annotations.Id | ||
import com.github.jasminb.jsonapi.annotations.Type | ||
import io.reactivex.annotations.NonNull | ||
|
||
@Type("notification") | ||
@JsonNaming(PropertyNamingStrategy.KebabCaseStrategy::class) | ||
data class Notification( | ||
@Id(IntegerIdHandler::class) | ||
@NonNull | ||
val id: Long, | ||
val message: String? = null, | ||
val receivedAt: String? = null, | ||
val isRead: Boolean? = null, | ||
val title: String? = null, | ||
val deletedAt: String? = null | ||
) |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/org/fossasia/openevent/general/notification/NotificationApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.fossasia.openevent.general.notification | ||
|
||
import io.reactivex.Completable | ||
import io.reactivex.Single | ||
import retrofit2.http.DELETE | ||
import retrofit2.http.Body | ||
import retrofit2.http.GET | ||
import retrofit2.http.PATCH | ||
import retrofit2.http.Path | ||
|
||
interface NotificationApi { | ||
|
||
@GET("users/{userId}/notifications?sort=message") | ||
fun getNotifications(@Path("userId") userId: Long): Single<List<Notification>> | ||
|
||
@PATCH("notifications/{notification_id}") | ||
fun updateNotification( | ||
@Path("notification_id") notificationId: Long, | ||
@Body notification: Notification | ||
): Single<List<Notification>> | ||
|
||
@DELETE("notifications/{notification_id}") | ||
fun deleteNotification(@Path("notification_id") notificationId: Long): Completable | ||
} |
141 changes: 141 additions & 0 deletions
141
app/src/main/java/org/fossasia/openevent/general/notification/NotificationFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package org.fossasia.openevent.general.notification | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.MenuItem | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import androidx.core.view.isVisible | ||
import androidx.lifecycle.Observer | ||
import androidx.navigation.Navigation | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import kotlinx.android.synthetic.main.content_no_internet.view.retry | ||
import kotlinx.android.synthetic.main.content_no_internet.view.noInternetCard | ||
import kotlinx.android.synthetic.main.fragment_notification.view.notificationRecycler | ||
import kotlinx.android.synthetic.main.fragment_notification.view.swiperefresh | ||
import kotlinx.android.synthetic.main.fragment_notification.view.shimmerNotifications | ||
import kotlinx.android.synthetic.main.fragment_notification.view.notificationCoordinatorLayout | ||
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.utils.Utils | ||
import org.fossasia.openevent.general.utils.Utils.setToolbar | ||
import org.fossasia.openevent.general.utils.extensions.nonNull | ||
import org.jetbrains.anko.design.snackbar | ||
import org.koin.androidx.viewmodel.ext.android.viewModel | ||
|
||
class NotificationFragment : Fragment() { | ||
private val notificationViewModel by viewModel<NotificationViewModel>() | ||
private val recyclerAdapter = NotificationsRecyclerAdapter() | ||
private lateinit var rootView: View | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
if (!notificationViewModel.isLoggedIn()) { | ||
redirectToLogin() | ||
} | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
rootView = inflater.inflate(R.layout.fragment_notification, container, false) | ||
setToolbar(activity, getString(R.string.title_notifications), true) | ||
setHasOptionsMenu(true) | ||
|
||
if (notificationViewModel.isLoggedIn()) { | ||
initObservers() | ||
if (notificationViewModel.notifications.value == null) { | ||
notificationViewModel.getNotifications() | ||
} | ||
rootView.notificationRecycler.layoutManager = LinearLayoutManager(requireContext()) | ||
rootView.notificationRecycler.adapter = recyclerAdapter | ||
} | ||
return rootView | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
rootView.retry.setOnClickListener { | ||
notificationViewModel.getNotifications() | ||
} | ||
|
||
rootView.swiperefresh.setOnRefreshListener { | ||
notificationViewModel.getNotifications() | ||
} | ||
} | ||
|
||
private fun initObservers() { | ||
notificationViewModel.notifications | ||
.nonNull() | ||
.observe(viewLifecycleOwner, Observer { | ||
showNoNotifications(it.isEmpty()) | ||
recyclerAdapter.addAll(it) | ||
recyclerAdapter.notifyDataSetChanged() | ||
}) | ||
|
||
notificationViewModel.error | ||
.nonNull() | ||
.observe(viewLifecycleOwner, Observer { | ||
rootView.swiperefresh.isRefreshing = false | ||
rootView.notificationCoordinatorLayout.snackbar(it) | ||
}) | ||
|
||
notificationViewModel.progress | ||
.nonNull() | ||
.observe(viewLifecycleOwner, Observer { | ||
if (it) { | ||
rootView.shimmerNotifications.startShimmer() | ||
rootView.noNotification.isVisible = false | ||
rootView.notificationRecycler.isVisible = false | ||
} else { | ||
rootView.shimmerNotifications.stopShimmer() | ||
rootView.swiperefresh.isRefreshing = it | ||
} | ||
rootView.shimmerNotifications.isVisible = it | ||
}) | ||
|
||
notificationViewModel.noInternet | ||
.nonNull() | ||
.observe(viewLifecycleOwner, Observer { | ||
if (it) { | ||
rootView.notificationCoordinatorLayout | ||
.snackbar(resources.getString(R.string.no_internet_connection_message)) | ||
rootView.swiperefresh.isRefreshing = !it | ||
} | ||
showNoInternet(it && notificationViewModel.notifications.value.isNullOrEmpty()) | ||
}) | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
return when (item.itemId) { | ||
android.R.id.home -> { | ||
activity?.onBackPressed() | ||
true | ||
} | ||
else -> super.onOptionsItemSelected(item) | ||
} | ||
} | ||
|
||
private fun showNoInternet(visible: Boolean) { | ||
rootView.noInternetCard.isVisible = visible | ||
rootView.notificationRecycler.isVisible = !visible | ||
} | ||
|
||
private fun showNoNotifications(visible: Boolean) { | ||
rootView.noNotification.isVisible = visible | ||
rootView.notificationRecycler.isVisible = !visible | ||
} | ||
|
||
private fun redirectToLogin() { | ||
LoginFragmentArgs(getString(R.string.log_in_first)) | ||
.toBundle() | ||
.also { | ||
Navigation.findNavController(rootView).navigate(R.id.loginFragment, it, Utils.getAnimFade()) | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/org/fossasia/openevent/general/notification/NotificationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.fossasia.openevent.general.notification | ||
|
||
import io.reactivex.Single | ||
|
||
class NotificationService( | ||
private val notificationApi: NotificationApi | ||
) { | ||
fun getNotifications(userId: Long): Single<List<Notification>> { | ||
return notificationApi.getNotifications(userId) | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
app/src/main/java/org/fossasia/openevent/general/notification/NotificationViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.fossasia.openevent.general.notification | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
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.data.Network | ||
import org.fossasia.openevent.general.data.Resource | ||
import org.fossasia.openevent.general.utils.extensions.withDefaultSchedulers | ||
import timber.log.Timber | ||
|
||
class NotificationViewModel( | ||
private val notificationService: NotificationService, | ||
private val authHolder: AuthHolder, | ||
private val network: Network, | ||
private val resource: Resource | ||
) : ViewModel() { | ||
|
||
private val compositeDisposable = CompositeDisposable() | ||
private val mutableNotifications = MutableLiveData<List<Notification>>() | ||
val notifications: LiveData<List<Notification>> = mutableNotifications | ||
|
||
private val mutableProgress = MutableLiveData<Boolean>() | ||
val progress: LiveData<Boolean> = mutableProgress | ||
|
||
private val mutableNoInternet = SingleLiveEvent<Boolean>() | ||
val noInternet: LiveData<Boolean> = mutableNoInternet | ||
|
||
private val mutableError = SingleLiveEvent<String>() | ||
val error: LiveData<String> = mutableError | ||
|
||
fun getId() = authHolder.getId() | ||
|
||
fun isLoggedIn() = authHolder.isLoggedIn() | ||
|
||
fun getNotifications() { | ||
|
||
if (!isConnected()) { | ||
return | ||
} | ||
|
||
compositeDisposable += notificationService.getNotifications(getId()) | ||
.withDefaultSchedulers() | ||
.doOnSubscribe { | ||
mutableProgress.value = true | ||
}.doFinally { | ||
mutableProgress.value = false | ||
}.subscribe({ | ||
mutableNotifications.value = it | ||
Timber.d("Notification retrieve successful") | ||
}, { | ||
mutableError.value = resource.getString(R.string.msg_failed_to_load_notification) | ||
Timber.d(it, resource.getString(R.string.msg_failed_to_load_notification)) | ||
}) | ||
} | ||
|
||
fun isConnected(): Boolean { | ||
val isConnected = network.isNetworkConnected() | ||
mutableNoInternet.value = !isConnected | ||
return isConnected | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you checking again for authentication?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent the api call if not logged in.
then in onStart, redirect to login.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But onCreateView is always called after onStart, so can't you remove onStart here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bro no, onStart is called after onActivityCreated
https://developer.android.com/guide/components/fragments