Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
whats-open-android
Commits
5b82c670
Commit
5b82c670
authored
Dec 26, 2016
by
Robert Hitt
Browse files
Added some comments
parent
25e31b0a
Changes
8
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/srct/whatsopen/MyApplication.java
View file @
5b82c670
...
...
@@ -10,6 +10,8 @@ public class MyApplication extends Application {
@Override
public
void
onCreate
()
{
super
.
onCreate
();
// Default Realm config. Might want to change this later
Realm
.
init
(
this
);
RealmConfiguration
realmConfig
=
new
RealmConfiguration
.
Builder
().
build
();
Realm
.
deleteRealm
(
realmConfig
);
...
...
app/src/main/java/srct/whatsopen/service/WhatsOpenClient.java
View file @
5b82c670
...
...
@@ -28,6 +28,7 @@ public class WhatsOpenClient {
public
WhatsOpenClient
()
{
}
// Returns a singleton of WhatsOpenService
public
static
WhatsOpenService
getInstance
()
{
if
(
mService
==
null
)
{
synchronized
(
WhatsOpenClient
.
class
)
{
...
...
@@ -39,11 +40,13 @@ public class WhatsOpenClient {
return
mService
;
}
// Configures Retrofit's JSON parsing logic
private
synchronized
static
Retrofit
getRetrofit
()
{
if
(
sRetrofit
==
null
)
{
synchronized
(
WhatsOpenClient
.
class
)
{
if
(
sRetrofit
==
null
)
{
Gson
gson
=
new
GsonBuilder
()
// Ensures Retrofit plays nicely with Realm
.
setExclusionStrategies
(
new
ExclusionStrategy
()
{
@Override
public
boolean
shouldSkipField
(
FieldAttributes
f
)
{
...
...
app/src/main/java/srct/whatsopen/service/WhatsOpenService.java
View file @
5b82c670
...
...
@@ -7,6 +7,7 @@ import retrofit2.http.GET;
import
srct.whatsopen.model.Facility
;
// Interface for Retrofit's Http request
public
interface
WhatsOpenService
{
@GET
(
"schedules"
)
...
...
app/src/main/java/srct/whatsopen/ui/DividerItemDecoration.java
View file @
5b82c670
...
...
@@ -25,6 +25,10 @@ import android.support.v7.widget.LinearLayoutManager;
import
android.support.v7.widget.RecyclerView
;
import
android.view.View
;
/**
* Used for creating the dividers in the Recycler View
*/
public
class
DividerItemDecoration
extends
RecyclerView
.
ItemDecoration
{
private
static
final
int
[]
ATTRS
=
new
int
[]{
...
...
app/src/main/java/srct/whatsopen/ui/FacilityListAdapter.java
View file @
5b82c670
...
...
@@ -17,6 +17,10 @@ import io.realm.RealmRecyclerViewAdapter;
import
srct.whatsopen.R
;
import
srct.whatsopen.model.Facility
;
/**
* Basic RecyclerView boilerplate, with some added Realm stuff
*/
public
class
FacilityListAdapter
extends
RealmRecyclerViewAdapter
<
Facility
,
FacilityListAdapter
.
ViewHolder
>
{
...
...
@@ -46,6 +50,7 @@ public class FacilityListAdapter extends
textView
.
setText
(
facility
.
getName
());
}
// Set up for the Recycler View cells
public
class
ViewHolder
extends
RecyclerView
.
ViewHolder
{
@BindView
(
R
.
id
.
facility_name
)
TextView
nameTextView
;
...
...
app/src/main/java/srct/whatsopen/ui/MainActivity.java
View file @
5b82c670
...
...
@@ -30,25 +30,14 @@ public class MainActivity extends AppCompatActivity {
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
activity_main
);
// Get Realm singleton
mRealm
=
Realm
.
getDefaultInstance
();
// Get WhatsOpenClient singleton
WhatsOpenService
service
=
WhatsOpenClient
.
getInstance
();
Call
<
List
<
Facility
>>
call
=
service
.
facilityList
();
call
.
enqueue
(
new
Callback
<
List
<
Facility
>>()
{
@Override
public
void
onResponse
(
Call
<
List
<
Facility
>>
call
,
Response
<
List
<
Facility
>>
response
)
{
List
<
Facility
>
facilities
=
response
.
body
();
mRealm
.
beginTransaction
();
mRealm
.
copyToRealmOrUpdate
(
facilities
);
mRealm
.
commitTransaction
();
}
@Override
public
void
onFailure
(
Call
<
List
<
Facility
>>
call
,
Throwable
t
)
{
// do some stuff
}
});
callWhatsOpenAPI
(
service
);
// Set up view
mRecyclerView
=
ButterKnife
.
findById
(
this
,
R
.
id
.
rvFacilities
);
setUpRecyclerView
();
}
...
...
@@ -59,13 +48,38 @@ public class MainActivity extends AppCompatActivity {
mRealm
.
close
();
}
// Handles set up for the Recycler View
private
void
setUpRecyclerView
()
{
mRecyclerView
.
setLayoutManager
(
new
LinearLayoutManager
(
this
));
mRecyclerView
.
setAdapter
(
new
FacilityListAdapter
(
this
,
mRealm
.
where
(
Facility
.
class
).
findAllAsync
()));
// Speeds things up for static lists
mRecyclerView
.
setHasFixedSize
(
true
);
// Adds dividers between items
mRecyclerView
.
addItemDecoration
(
new
DividerItemDecoration
(
this
,
DividerItemDecoration
.
VERTICAL_LIST
));
}
// Gets a Call from the given Retrofit service, then asynchronously executes it
// On success, copies the resulting facility list to the Realm DB
private
void
callWhatsOpenAPI
(
WhatsOpenService
service
)
{
Call
<
List
<
Facility
>>
call
=
service
.
facilityList
();
call
.
enqueue
(
new
Callback
<
List
<
Facility
>>()
{
@Override
public
void
onResponse
(
Call
<
List
<
Facility
>>
call
,
Response
<
List
<
Facility
>>
response
)
{
List
<
Facility
>
facilities
=
response
.
body
();
mRealm
.
beginTransaction
();
mRealm
.
copyToRealmOrUpdate
(
facilities
);
mRealm
.
commitTransaction
();
}
@Override
public
void
onFailure
(
Call
<
List
<
Facility
>>
call
,
Throwable
t
)
{
// do some stuff
}
});
}
}
app/src/main/java/srct/whatsopen/util/OpenTimesDeserializer.java
View file @
5b82c670
...
...
@@ -11,6 +11,7 @@ import java.lang.reflect.Type;
import
srct.whatsopen.model.OpenTimes
;
// Deserializer for a nested Json object, OpenTimes
public
class
OpenTimesDeserializer
implements
JsonDeserializer
<
OpenTimes
>
{
@Override
...
...
app/src/main/java/srct/whatsopen/util/OpenTimesListDeserializer.java
View file @
5b82c670
...
...
@@ -12,6 +12,7 @@ import io.realm.RealmList;
import
srct.whatsopen.model.OpenTimes
;
// Json Deserializer for a list of OpenTimes
public
class
OpenTimesListDeserializer
implements
JsonDeserializer
<
RealmList
<
OpenTimes
>>
{
@Override
...
...
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