Skip to content

Commit

Permalink
login check, swiperefresh, date format
Browse files Browse the repository at this point in the history
  • Loading branch information
iamanbansal committed Apr 21, 2019
1 parent 50a1fd1 commit 0546929
Show file tree
Hide file tree
Showing 10 changed files with 1,463 additions and 68 deletions.
1,295 changes: 1,295 additions & 0 deletions app/schemas/org.fossasia.openevent.general.OpenEventDatabase/3.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,30 @@ object EventUtils {
}
}

fun getFormattedDateWithoutWeekday(date: ZonedDateTime): String {
val dateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("MMM d, y")
return try {
dateFormat.format(date)
} catch (e: IllegalArgumentException) {
Timber.e(e, "Error formatting Date")
""
}
}

fun getFormattedWeekDay(date: ZonedDateTime): String {
val dateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("EEE")
return try {
dateFormat.format(date)
} catch (e: IllegalArgumentException) {
Timber.e(e, "Error formatting Date")
""
}
}

fun getDayDifferenceFromToday(date: String): Long {
return (System.currentTimeMillis() - EventUtils.getTimeInMilliSeconds(date, null)) / (1000 * 60 * 60 * 24)
}

/**
* share event detail along with event image
* if image loading is successful then imageView tag will be set to String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
Expand Down Expand Up @@ -77,7 +80,7 @@ class EventsFragment : Fragment(), ScrollToTop {
savedInstanceState: Bundle?
): View? {
rootView = inflater.inflate(R.layout.fragment_events, container, false)

setHasOptionsMenu(true)
if (preference.getString(SAVED_LOCATION).isNullOrEmpty()) {
findNavController(requireActivity(), R.id.frameContainer).navigate(R.id.welcomeFragment)
}
Expand Down Expand Up @@ -188,6 +191,20 @@ class EventsFragment : Fragment(), ScrollToTop {
}
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.events, menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.notifications -> {
findNavController(rootView).navigate(R.id.notificationFragment, null, getAnimSlide())
true
}
else -> super.onOptionsItemSelected(item)
}
}

private fun showNoInternetScreen(show: Boolean) {
rootView.homeScreenLL.visibility = if (!show) View.VISIBLE else View.GONE
rootView.noInternetCard.visibility = if (show) View.VISIBLE else View.GONE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package org.fossasia.openevent.general.notification

import android.os.Bundle
import androidx.fragment.app.Fragment
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.*
import kotlinx.android.synthetic.main.fragment_notification.view.*
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
Expand All @@ -20,13 +28,12 @@ import org.koin.androidx.viewmodel.ext.android.viewModel
class NotificationFragment : Fragment() {
private val notificationViewModel by viewModel<NotificationViewModel>()
private val recyclerAdapter = NotificationsRecyclerAdapter()
private lateinit var linearLayoutManager: LinearLayoutManager
private lateinit var rootView: View

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (notificationViewModel.notifications.value == null) {
notificationViewModel.getNotifications()
override fun onStart() {
super.onStart()
if (!notificationViewModel.isLoggedIn()) {
redirectToLogin()
}
}

Expand All @@ -38,11 +45,15 @@ class NotificationFragment : Fragment() {
rootView = inflater.inflate(R.layout.fragment_notification, container, false)
setToolbar(activity, getString(R.string.title_notifications), true)
setHasOptionsMenu(true)
initObservers()

rootView.notificationRecycler.layoutManager = LinearLayoutManager(requireContext())
rootView.notificationRecycler.adapter = recyclerAdapter

if (notificationViewModel.isLoggedIn()) {
initObservers()
if (notificationViewModel.notifications.value == null) {
notificationViewModel.getNotifications()
}
rootView.notificationRecycler.layoutManager = LinearLayoutManager(requireContext())
rootView.notificationRecycler.adapter = recyclerAdapter
}
return rootView
}

Expand All @@ -52,6 +63,10 @@ class NotificationFragment : Fragment() {
rootView.retry.setOnClickListener {
notificationViewModel.getNotifications()
}

rootView.swiperefresh.setOnRefreshListener {
notificationViewModel.getNotifications()
}
}

private fun initObservers() {
Expand All @@ -66,24 +81,32 @@ class NotificationFragment : Fragment() {
notificationViewModel.error
.nonNull()
.observe(viewLifecycleOwner, Observer {
rootView.snackbar(it)
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.snackbar(resources.getString(R.string.no_internet_connection_message)) }
if (it) {
rootView.notificationCoordinatorLayout
.snackbar(resources.getString(R.string.no_internet_connection_message))
rootView.swiperefresh.isRefreshing = !it
}
showNoInternet(it && notificationViewModel.notifications.value.isNullOrEmpty())
})
}
Expand All @@ -107,4 +130,14 @@ class NotificationFragment : Fragment() {
rootView.noNotification.isVisible = visible
rootView.notificationRecycler.isVisible = !visible
}

private fun redirectToLogin() {
LoginFragmentArgs.Builder()
.setSnackbarMessage(getString(R.string.log_in_first))
.build()
.toBundle()
.also { bundle ->
Navigation.findNavController(rootView).navigate(R.id.loginFragment, bundle, Utils.getAnimFade())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class NotificationViewModel(
Timber.d("Notification retrieve successful")
}, {
mutableError.value = resource.getString(R.string.msg_failed_to_load_notification)
Timber.d(it, "Notification retrieve failed")
Timber.d(it, resource.getString(R.string.msg_failed_to_load_notification))
})
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ class NotificationsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView
itemView.message.text = notification.message.stripHtml()
itemView.message.movementMethod = LinkMovementMethod.getInstance()
notification.receivedAt?.let {
val dayDiff = EventUtils.getDayDifferenceFromToday(it)
val formattedDateTime = EventUtils.getEventDateTime(it, null)
val formattedTime = EventUtils.getFormattedTime(formattedDateTime)
val formattedDate = EventUtils.getFormattedDate(formattedDateTime)
val timezone = EventUtils.getFormattedTimeZone(formattedDateTime)

itemView.time.text = "$formattedTime $formattedDate $timezone"
itemView.time.text = when (dayDiff) {
0L -> EventUtils.getFormattedTime(formattedDateTime)
in 1..6 -> EventUtils.getFormattedWeekDay(formattedDateTime)
else -> EventUtils.getFormattedDateWithoutWeekday(formattedDateTime)
}
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_notifications_white.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z"/>
</vector>
100 changes: 55 additions & 45 deletions app/src/main/res/layout/fragment_notification.xml
Original file line number Diff line number Diff line change
@@ -1,66 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swiperefresh"
tools:context="org.fossasia.openevent.general.notification.NotificationFragment">

<include layout="@layout/content_no_internet"
android:visibility="gone"/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/notificationCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmerNotifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
<include
layout="@layout/content_no_internet"
android:visibility="gone" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmerNotifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">

<include layout="@layout/placeholder_item_card_notification" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/placeholder_item_card_notification" />
<include layout="@layout/placeholder_item_card_notification" />

<include layout="@layout/placeholder_item_card_notification" />
<include layout="@layout/placeholder_item_card_notification" />

<include layout="@layout/placeholder_item_card_notification" />
<include layout="@layout/placeholder_item_card_notification" />

<include layout="@layout/placeholder_item_card_notification" />
<include layout="@layout/placeholder_item_card_notification" />

<include layout="@layout/placeholder_item_card_notification" />
<include layout="@layout/placeholder_item_card_notification" />

</LinearLayout>
<include layout="@layout/placeholder_item_card_notification" />

</LinearLayout>

</com.facebook.shimmer.ShimmerFrameLayout>

<LinearLayout
android:id="@+id/noNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">

<ImageView
android:layout_width="@dimen/item_image_view"
android:layout_height="@dimen/item_image_view"
app:srcCompat="@drawable/ic_notifications_none" />

<TextView
android:layout_width="wrap_content"
</com.facebook.shimmer.ShimmerFrameLayout>

<LinearLayout
android:id="@+id/noNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no_notification_result"
android:textSize="@dimen/text_size_medium" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/notificationRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">

<ImageView
android:layout_width="@dimen/item_image_view"
android:layout_height="@dimen/item_image_view"
app:srcCompat="@drawable/ic_notifications_none" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_notification_result"
android:textSize="@dimen/text_size_medium" />
</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/notificationRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
3 changes: 1 addition & 2 deletions app/src/main/res/layout/item_card_notification.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

android:layout_height="match_parent">

<TextView
android:id="@+id/title"
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/menu/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/notifications"
android:icon="@drawable/ic_notifications_white"
android:title="@string/share"
app:showAsAction="always"/>

</menu>

0 comments on commit 0546929

Please sign in to comment.