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
85a83210
Commit
85a83210
authored
Dec 30, 2016
by
Robert Hitt
Browse files
Favorite status is now saved.
- yay SharedPreferences
parent
bb486366
Changes
2
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/srct/whatsopen/ui/FacilityListAdapter.java
View file @
85a83210
...
...
@@ -2,9 +2,10 @@ package srct.whatsopen.ui;
import
android.content.Context
;
import
android.content.SharedPreferences
;
import
android.preference.PreferenceManager
;
import
android.support.v4.content.ContextCompat
;
import
android.support.v7.widget.RecyclerView
;
import
android.util.Log
;
import
android.view.LayoutInflater
;
import
android.view.View
;
import
android.view.ViewGroup
;
...
...
@@ -22,9 +23,7 @@ import butterknife.ButterKnife;
import
butterknife.OnClick
;
import
io.realm.OrderedRealmCollection
;
import
io.realm.Realm
;
import
io.realm.RealmAsyncTask
;
import
io.realm.RealmList
;
import
io.realm.RealmQuery
;
import
io.realm.RealmRecyclerViewAdapter
;
import
io.realm.RealmResults
;
import
srct.whatsopen.R
;
...
...
@@ -39,8 +38,6 @@ import srct.whatsopen.model.OpenTimes;
public
class
FacilityListAdapter
extends
RealmRecyclerViewAdapter
<
Facility
,
FacilityListAdapter
.
ViewHolder
>
{
Context
mContext
;
public
FacilityListAdapter
(
Context
context
,
OrderedRealmCollection
<
Facility
>
data
)
{
...
...
@@ -49,7 +46,7 @@ public class FacilityListAdapter extends
@Override
public
ViewHolder
onCreateViewHolder
(
ViewGroup
parent
,
int
viewType
)
{
m
Context
=
parent
.
getContext
();
C
ontext
c
ontext
=
parent
.
getContext
();
LayoutInflater
inflater
=
LayoutInflater
.
from
(
context
);
View
facilityView
=
inflater
.
inflate
(
R
.
layout
.
item_facility
,
parent
,
false
);
...
...
@@ -68,10 +65,10 @@ public class FacilityListAdapter extends
if
(
isOpen
)
{
// set the RV cell to be highlighted
holder
.
itemView
.
setBackgroundColor
(
ContextCompat
.
getColor
(
mC
ontext
,
R
.
color
.
facilityOpen
));
.
getColor
(
c
ontext
,
R
.
color
.
facilityOpen
));
}
else
{
holder
.
itemView
.
setBackgroundColor
(
ContextCompat
.
getColor
(
mC
ontext
,
R
.
color
.
facilityClosed
));
.
getColor
(
c
ontext
,
R
.
color
.
facilityClosed
));
}
if
(
facility
.
isFavorited
())
{
...
...
@@ -150,11 +147,15 @@ public class FacilityListAdapter extends
}
}
// Asynchronously updates the favorite status
// Would block the favorite button redrawing otherwise
// Asynchronously updates the Realm object's favorite status
// and updates the favorite status in SharedPreferences
// Would block the favorite button redrawing if done on the UI thread
void
toggleFavoriteAsync
(
final
boolean
status
)
{
Realm
realm
=
Realm
.
getDefaultInstance
();
SharedPreferences
pref
=
PreferenceManager
.
getDefaultSharedPreferences
(
context
);
final
SharedPreferences
.
Editor
editor
=
pref
.
edit
();
final
String
facilityName
=
data
.
getName
();
realm
.
executeTransactionAsync
(
new
Realm
.
Transaction
()
{
@Override
...
...
@@ -164,6 +165,8 @@ public class FacilityListAdapter extends
.
equalTo
(
"mName"
,
facilityName
).
findFirst
();
facility
.
setFavorited
(
status
);
editor
.
putBoolean
(
facilityName
,
status
);
editor
.
apply
();
}
},
null
,
null
);
...
...
app/src/main/java/srct/whatsopen/ui/MainActivity.java
View file @
85a83210
package
srct.whatsopen.ui
;
import
android.content.SharedPreferences
;
import
android.graphics.drawable.Drawable
;
import
android.preference.PreferenceManager
;
import
android.support.v4.content.ContextCompat
;
import
android.support.v7.app.AppCompatActivity
;
import
android.os.Bundle
;
...
...
@@ -67,11 +69,19 @@ public class MainActivity extends AppCompatActivity {
// 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
)
{
final
SharedPreferences
pref
=
PreferenceManager
.
getDefaultSharedPreferences
(
this
);
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
();
// Query SharedReferences for each Facility's favorite status. defaults to false
for
(
Facility
facility
:
facilities
)
{
facility
.
setFavorited
(
pref
.
getBoolean
(
facility
.
getName
(),
false
));
}
mRealm
.
beginTransaction
();
mRealm
.
copyToRealmOrUpdate
(
facilities
);
mRealm
.
commitTransaction
();
...
...
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