Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
InstiApp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
RAHUL SHARMA
InstiApp
Commits
cf0ba177
Commit
cf0ba177
authored
Nov 30, 2018
by
Varun Patil
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cache the list of notifications
parent
d4a42fd9
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
47 additions
and
34 deletions
+47
-34
app/src/main/java/app/insti/UpdatableList.java
app/src/main/java/app/insti/UpdatableList.java
+8
-12
app/src/main/java/app/insti/Utils.java
app/src/main/java/app/insti/Utils.java
+3
-0
app/src/main/java/app/insti/activity/MainActivity.java
app/src/main/java/app/insti/activity/MainActivity.java
+6
-5
app/src/main/java/app/insti/fragment/FeedFragment.java
app/src/main/java/app/insti/fragment/FeedFragment.java
+5
-4
app/src/main/java/app/insti/fragment/NotificationsFragment.java
...c/main/java/app/insti/fragment/NotificationsFragment.java
+25
-13
No files found.
app/src/main/java/app/insti/UpdatableList.java
View file @
cf0ba177
...
...
@@ -4,25 +4,21 @@ import java.util.ArrayList;
import
java.util.List
;
public
class
UpdatableList
<
T
>
extends
ArrayList
<
T
>
{
private
List
<
T
>
cache
=
new
ArrayList
<>();
public
List
<
T
>
getCache
()
{
return
cache
;
}
public
void
setCache
(
List
<
T
>
mCache
)
{
cache
=
mCache
;
/** Convert a list to updatable list */
public
void
setList
(
List
<
T
>
list
)
{
this
.
clear
();
this
.
addAll
(
list
);
}
/** Update existing or add */
public
void
updateCache
(
T
t
)
{
for
(
int
i
=
0
;
i
<
cache
.
size
();
i
++)
{
T
cachedT
=
cache
.
get
(
i
);
for
(
int
i
=
0
;
i
<
this
.
size
();
i
++)
{
T
cachedT
=
this
.
get
(
i
);
if
(
cachedT
.
equals
(
t
))
{
cache
.
set
(
i
,
t
);
this
.
set
(
i
,
t
);
return
;
}
}
cache
.
add
(
t
);
this
.
add
(
t
);
}
}
app/src/main/java/app/insti/Utils.java
View file @
cf0ba177
...
...
@@ -18,6 +18,7 @@ import app.insti.activity.MainActivity;
import
app.insti.api.RetrofitInterface
;
import
app.insti.api.model.Body
;
import
app.insti.api.model.Event
;
import
app.insti.api.model.Notification
;
import
app.insti.api.model.User
;
import
app.insti.fragment.BodyFragment
;
import
app.insti.fragment.EventFragment
;
...
...
@@ -25,6 +26,8 @@ import app.insti.fragment.UserFragment;
public
final
class
Utils
{
public
static
UpdatableList
<
Event
>
eventCache
=
new
UpdatableList
<>();
public
static
UpdatableList
<
Notification
>
notificationCache
=
null
;
private
static
String
sessionId
;
private
static
RetrofitInterface
retrofitInterface
;
public
static
Gson
gson
;
...
...
app/src/main/java/app/insti/activity/MainActivity.java
View file @
cf0ba177
...
...
@@ -47,6 +47,7 @@ import java.util.List;
import
app.insti.Constants
;
import
app.insti.R
;
import
app.insti.SessionManager
;
import
app.insti.UpdatableList
;
import
app.insti.Utils
;
import
app.insti.api.EmptyCallback
;
import
app.insti.api.RetrofitInterface
;
...
...
@@ -72,6 +73,7 @@ import app.insti.fragment.QuickLinksFragment;
import
app.insti.fragment.SettingsFragment
;
import
app.insti.fragment.TrainingBlogFragment
;
import
app.insti.fragment.UserFragment
;
import
okhttp3.internal.Util
;
import
retrofit2.Call
;
import
retrofit2.Response
;
...
...
@@ -95,8 +97,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
private
User
currentUser
;
private
BackHandledFragment
selectedFragment
;
private
Menu
menu
;
private
RetrofitInterface
retrofitInterface
;
private
List
<
Notification
>
notifications
=
null
;
/**
* which menu item should be checked on activity start
...
...
@@ -186,7 +186,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
*/
private
void
fetchNotifications
()
{
// Try memory cache
if
(
notifications
!=
null
)
{
if
(
Utils
.
notificationCache
!=
null
)
{
showNotifications
();
return
;
}
...
...
@@ -197,7 +197,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
@Override
public
void
onResponse
(
Call
<
List
<
Notification
>>
call
,
Response
<
List
<
Notification
>>
response
)
{
if
(
response
.
isSuccessful
())
{
notifications
=
response
.
body
();
Utils
.
notificationCache
=
new
UpdatableList
<>();
Utils
.
notificationCache
.
setList
(
response
.
body
());
showNotifications
();
}
}
...
...
@@ -208,7 +209,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
* Show the right notification icon
*/
private
void
showNotifications
()
{
if
(
notifications
!=
null
&&
!
notifications
.
isEmpty
())
{
if
(
Utils
.
notificationCache
!=
null
&&
!
Utils
.
notificationCache
.
isEmpty
())
{
menu
.
findItem
(
R
.
id
.
action_notifications
).
setIcon
(
R
.
drawable
.
baseline_notifications_active_white_24
);
}
else
{
menu
.
findItem
(
R
.
id
.
action_notifications
).
setIcon
(
R
.
drawable
.
ic_notifications_white_24dp
);
...
...
app/src/main/java/app/insti/fragment/FeedFragment.java
View file @
cf0ba177
...
...
@@ -17,6 +17,7 @@ import java.util.List;
import
app.insti.ActivityBuffer
;
import
app.insti.R
;
import
app.insti.UpdatableList
;
import
app.insti.Utils
;
import
app.insti.activity.MainActivity
;
import
app.insti.adapter.FeedAdapter
;
...
...
@@ -72,10 +73,10 @@ public class FeedFragment extends BaseFragment {
fab
=
getView
().
findViewById
(
R
.
id
.
fab
);
// Initialize the feed
if
(
Utils
.
eventCache
.
getCache
()
==
null
||
Utils
.
eventCache
.
getCache
().
size
()
==
0
)
{
if
(
Utils
.
eventCache
==
null
||
Utils
.
eventCache
.
isEmpty
()
)
{
updateFeed
();
}
else
{
displayEvents
(
Utils
.
eventCache
.
getCache
()
);
displayEvents
(
Utils
.
eventCache
);
}
}
...
...
@@ -105,8 +106,8 @@ public class FeedFragment extends BaseFragment {
@Override
public
void
onResponse
(
Call
<
NewsFeedResponse
>
call
,
Response
<
NewsFeedResponse
>
response
)
{
if
(
response
.
isSuccessful
())
{
Utils
.
eventCache
.
set
Cache
(
response
.
body
().
getEvents
());
displayEvents
(
Utils
.
eventCache
.
getCache
()
);
Utils
.
eventCache
.
set
List
(
response
.
body
().
getEvents
());
displayEvents
(
Utils
.
eventCache
);
}
//Server Error
feedSwipeRefreshLayout
.
setRefreshing
(
false
);
...
...
app/src/main/java/app/insti/fragment/NotificationsFragment.java
View file @
cf0ba177
...
...
@@ -14,10 +14,13 @@ import android.view.ViewGroup;
import
java.util.List
;
import
app.insti.R
;
import
app.insti.UpdatableList
;
import
app.insti.Utils
;
import
app.insti.adapter.NotificationsAdapter
;
import
app.insti.api.EmptyCallback
;
import
app.insti.api.RetrofitInterface
;
import
app.insti.api.model.Notification
;
import
okhttp3.internal.Util
;
import
retrofit2.Call
;
import
retrofit2.Callback
;
import
retrofit2.Response
;
...
...
@@ -28,8 +31,7 @@ import retrofit2.Response;
public
class
NotificationsFragment
extends
BottomSheetDialogFragment
{
RecyclerView
notificationsRecyclerView
;
List
<
Notification
>
notifications
;
NotificationsAdapter
notificationsAdapter
=
null
;
public
NotificationsFragment
()
{
// Required empty public constructor
...
...
@@ -47,19 +49,23 @@ public class NotificationsFragment extends BottomSheetDialogFragment {
public
void
onStart
()
{
super
.
onStart
();
/* Show cached notifications */
if
(
Utils
.
notificationCache
!=
null
)
{
showNotifications
(
Utils
.
notificationCache
);
}
else
{
Utils
.
notificationCache
=
new
UpdatableList
<>();
}
/* Update notifications */
RetrofitInterface
retrofitInterface
=
Utils
.
getRetrofitInterface
();
retrofitInterface
.
getNotifications
(
Utils
.
getSessionIDHeader
()).
enqueue
(
new
Callback
<
List
<
Notification
>>()
{
retrofitInterface
.
getNotifications
(
Utils
.
getSessionIDHeader
()).
enqueue
(
new
Empty
Callback
<
List
<
Notification
>>()
{
@Override
public
void
onResponse
(
Call
<
List
<
Notification
>>
call
,
Response
<
List
<
Notification
>>
response
)
{
if
(
response
.
isSuccessful
())
{
notifications
=
response
.
body
(
);
showNotifications
(
notifications
);
Utils
.
notificationCache
.
setList
(
response
.
body
()
);
showNotifications
(
Utils
.
notificationCache
);
}
}
@Override
public
void
onFailure
(
Call
<
List
<
Notification
>>
call
,
Throwable
t
)
{
}
});
}
...
...
@@ -70,9 +76,15 @@ public class NotificationsFragment extends BottomSheetDialogFragment {
/* Hide loader */
getView
().
findViewById
(
R
.
id
.
loadingPanel
).
setVisibility
(
View
.
GONE
);
NotificationsAdapter
notificationsAdapter
=
new
NotificationsAdapter
(
notifications
,
this
);
notificationsRecyclerView
=
(
RecyclerView
)
getView
().
findViewById
(
R
.
id
.
notifications_recycler_view
);
notificationsRecyclerView
.
setAdapter
(
notificationsAdapter
);
notificationsRecyclerView
.
setLayoutManager
(
new
LinearLayoutManager
(
getContext
()));
/* Initialize */
if
(
notificationsAdapter
==
null
)
{
notificationsAdapter
=
new
NotificationsAdapter
(
notifications
,
this
);
notificationsRecyclerView
=
(
RecyclerView
)
getView
().
findViewById
(
R
.
id
.
notifications_recycler_view
);
notificationsRecyclerView
.
setAdapter
(
notificationsAdapter
);
notificationsRecyclerView
.
setLayoutManager
(
new
LinearLayoutManager
(
getContext
()));
}
else
{
notificationsAdapter
.
setList
(
notifications
);
notificationsAdapter
.
notifyDataSetChanged
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment