Commit 5b82c670 authored by Robert Hitt's avatar Robert Hitt
Browse files

Added some comments

parent 25e31b0a
......@@ -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);
......
......@@ -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) {
......
......@@ -7,6 +7,7 @@ import retrofit2.http.GET;
import srct.whatsopen.model.Facility;
// Interface for Retrofit's Http request
public interface WhatsOpenService {
@GET("schedules")
......
......@@ -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[]{
......
......@@ -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;
......
......@@ -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
}
});
}
}
......@@ -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
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment