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
231aeda1
Commit
231aeda1
authored
Sep 26, 2018
by
Varun Patil
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use okhttp caching for feed
parent
6fdd22fb
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
127 additions
and
3 deletions
+127
-3
app/src/main/AndroidManifest.xml
app/src/main/AndroidManifest.xml
+1
-0
app/src/main/java/app/insti/activity/MainActivity.java
app/src/main/java/app/insti/activity/MainActivity.java
+8
-0
app/src/main/java/app/insti/api/ServiceGenerator.java
app/src/main/java/app/insti/api/ServiceGenerator.java
+112
-0
app/src/main/java/app/insti/fragment/FeedFragment.java
app/src/main/java/app/insti/fragment/FeedFragment.java
+6
-3
No files found.
app/src/main/AndroidManifest.xml
View file @
231aeda1
...
...
@@ -3,6 +3,7 @@
package=
"app.insti"
>
<uses-permission
android:name=
"android.permission.ACCESS_NETWORK_STATE"
/>
<uses-permission
android:name=
"android.permission.WAKE_LOCK"
/>
<uses-permission
android:name=
"android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
...
...
app/src/main/java/app/insti/activity/MainActivity.java
View file @
231aeda1
...
...
@@ -87,6 +87,11 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
private
boolean
showNotifications
=
false
;
private
BackHandledFragment
selectedFragment
;
private
Menu
menu
;
private
RetrofitInterface
retrofitInterface
;
public
RetrofitInterface
getRetrofitInterface
()
{
return
retrofitInterface
;
}
public
static
void
hideKeyboard
(
Activity
activity
)
{
InputMethodManager
imm
=
(
InputMethodManager
)
activity
.
getSystemService
(
Activity
.
INPUT_METHOD_SERVICE
);
...
...
@@ -107,6 +112,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}
catch
(
IllegalStateException
ignored
)
{
}
ServiceGenerator
serviceGenerator
=
new
ServiceGenerator
(
getApplicationContext
());
this
.
retrofitInterface
=
serviceGenerator
.
getRetrofitInterface
();
/* Make notification channel on oreo */
if
(
android
.
os
.
Build
.
VERSION
.
SDK_INT
>=
android
.
os
.
Build
.
VERSION_CODES
.
O
)
{
createNotificationChannel
();
...
...
app/src/main/java/app/insti/api/ServiceGenerator.java
View file @
231aeda1
package
app.insti.api
;
import
android.content.Context
;
import
android.net.NetworkInfo
;
import
android.util.Log
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.concurrent.TimeUnit
;
import
app.insti.Constants
;
import
butterknife.internal.Utils
;
import
okhttp3.Cache
;
import
okhttp3.CacheControl
;
import
okhttp3.Interceptor
;
import
okhttp3.OkHttpClient
;
import
okhttp3.Request
;
import
okhttp3.Response
;
import
okhttp3.logging.HttpLoggingInterceptor
;
import
retrofit2.Retrofit
;
import
retrofit2.converter.gson.GsonConverterFactory
;
public
class
ServiceGenerator
{
public
static
final
String
HEADER_CACHE_CONTROL
=
"Cache-Control"
;
public
static
final
String
HEADER_PRAGMA
=
"Pragma"
;
private
static
final
String
BASE_URL
=
"https://api.insti.app/api/"
;
private
Context
context
;
private
Interceptor
provideCacheInterceptor
=
new
Interceptor
()
{
@Override
public
Response
intercept
(
Chain
chain
)
throws
IOException
{
Response
response
=
chain
.
proceed
(
chain
.
request
());
CacheControl
cacheControl
;
if
(
isConnected
())
{
cacheControl
=
new
CacheControl
.
Builder
()
.
maxAge
(
80
,
TimeUnit
.
SECONDS
)
.
build
();
}
else
{
cacheControl
=
new
CacheControl
.
Builder
()
.
maxStale
(
7
,
TimeUnit
.
DAYS
)
.
build
();
}
return
response
.
newBuilder
()
.
removeHeader
(
HEADER_PRAGMA
)
.
removeHeader
(
HEADER_CACHE_CONTROL
)
.
header
(
HEADER_CACHE_CONTROL
,
cacheControl
.
toString
())
.
build
();
}
};
private
Interceptor
provideOfflineCacheInterceptor
=
new
Interceptor
()
{
@Override
public
Response
intercept
(
Chain
chain
)
throws
IOException
{
Request
request
=
chain
.
request
();
if
(!
isConnected
())
{
CacheControl
cacheControl
=
new
CacheControl
.
Builder
()
.
maxStale
(
7
,
TimeUnit
.
DAYS
)
.
build
();
request
=
request
.
newBuilder
()
.
removeHeader
(
HEADER_PRAGMA
)
.
removeHeader
(
HEADER_CACHE_CONTROL
)
.
cacheControl
(
cacheControl
)
.
build
();
}
return
chain
.
proceed
(
request
);
}
};
private
Cache
provideCache
()
{
Cache
cache
=
null
;
try
{
cache
=
new
Cache
(
new
File
(
context
.
getCacheDir
(),
"api-cache"
),
50
*
1024
*
1024
);
// 50 MB
}
catch
(
Exception
e
)
{
Log
.
e
(
"cache"
,
"Could not create Cache!"
);
}
return
cache
;
}
public
boolean
isConnected
()
{
Log
.
wtf
(
"cache"
,
"MEOW"
);
try
{
android
.
net
.
ConnectivityManager
e
=
(
android
.
net
.
ConnectivityManager
)
context
.
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
);
NetworkInfo
activeNetwork
=
e
.
getActiveNetworkInfo
();
Log
.
wtf
(
"cache"
,
Boolean
.
toString
(
activeNetwork
!=
null
&&
activeNetwork
.
isConnectedOrConnecting
()));
return
activeNetwork
!=
null
&&
activeNetwork
.
isConnectedOrConnecting
();
}
catch
(
Exception
e
)
{
Log
.
w
(
"cache"
,
e
.
toString
());
}
return
false
;
}
private
static
OkHttpClient
.
Builder
clientBuilder
=
new
OkHttpClient
.
Builder
();
private
static
Retrofit
.
Builder
retrofitBuilder
=
new
Retrofit
.
Builder
()
.
baseUrl
(
BASE_URL
)
.
addConverterFactory
(
GsonConverterFactory
.
create
());
...
...
@@ -17,4 +112,21 @@ public class ServiceGenerator {
retrofit
=
retrofitBuilder
.
client
(
clientBuilder
.
build
()).
build
();
return
retrofit
.
create
(
serviceClass
);
}
public
RetrofitInterface
retrofitInterface
;
public
ServiceGenerator
(
Context
mContext
)
{
context
=
mContext
;
retrofit
=
retrofitBuilder
.
client
(
clientBuilder
.
addInterceptor
(
provideOfflineCacheInterceptor
)
.
addNetworkInterceptor
(
provideCacheInterceptor
)
.
cache
(
provideCache
())
.
build
()
).
build
();
retrofitInterface
=
retrofit
.
create
(
RetrofitInterface
.
class
);
}
public
RetrofitInterface
getRetrofitInterface
()
{
return
retrofitInterface
;
}
}
app/src/main/java/app/insti/fragment/FeedFragment.java
View file @
231aeda1
...
...
@@ -31,9 +31,12 @@ import app.insti.api.ServiceGenerator;
import
app.insti.api.model.NewsFeedResponse
;
import
app.insti.data.AppDatabase
;
import
app.insti.data.Event
;
import
okhttp3.OkHttpClient
;
import
retrofit2.Call
;
import
retrofit2.Callback
;
import
retrofit2.Response
;
import
retrofit2.Retrofit
;
import
retrofit2.converter.gson.GsonConverterFactory
;
/**
* A simple {@link Fragment} subclass.
...
...
@@ -79,7 +82,7 @@ public class FeedFragment extends BaseFragment {
public
void
onStart
()
{
super
.
onStart
();
appDatabase
=
AppDatabase
.
getAppDatabase
(
getContext
());
new
showEventsFromDB
().
execute
();
//
new showEventsFromDB().execute();
fab
=
(
FloatingActionButton
)
getView
().
findViewById
(
R
.
id
.
fab
);
updateFeed
();
}
...
...
@@ -104,9 +107,9 @@ public class FeedFragment extends BaseFragment {
}
}
private
void
updateFeed
()
{
RetrofitInterface
retrofitInterface
=
ServiceGenerator
.
createService
(
RetrofitInterface
.
class
);
RetrofitInterface
retrofitInterface
=
((
MainActivity
)
getActivity
()).
getRetrofitInterface
();
retrofitInterface
.
getNewsFeed
(
"sessionid="
+
getArguments
().
getString
(
Constants
.
SESSION_ID
)).
enqueue
(
new
Callback
<
NewsFeedResponse
>()
{
@Override
public
void
onResponse
(
Call
<
NewsFeedResponse
>
call
,
Response
<
NewsFeedResponse
>
response
)
{
...
...
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