Last active
October 4, 2017 03:40
-
-
Save SUPERCILEX/5c4c6f217d151b23f0734e924772fa34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val teamsQuery get() = "owners.${uid!!}".let { | |
FirebaseFirestore.getInstance().collection("teams").whereGreaterThanOrEqualTo(it, 0).orderBy(it) | |
} | |
object TeamsLiveData : LiveData<ObservableSnapshotArray<T>>(), FirebaseAuth.AuthStateListener { | |
init { | |
FirebaseAuth.getInstance().addAuthStateListener(this) | |
} | |
override fun onAuthStateChanged(auth: FirebaseAuth) { | |
value?.removeAllListeners() | |
value = if (auth.currentUser == null) null else FirestoreArray(teamsQuery) { snapshot -> | |
// I'm too lazy, but this could be optimized to not use reflection | |
snapshot.toObject(Team::class.java).apply { id = snapshot.id } | |
} | |
} | |
} | |
// In your Activity's onCreate() or your Fragment's onViewCreated() | |
TeamsLiveData.observe(this, Observer { snapshots -> | |
adapter?.stopListening() // Clean up after ourselves | |
lifecycle.removeObserver(adapter) // Prevent Architecture Components from automatically re-subscribing an adapter | |
if (snapshots == null) { | |
// Not signed-in, show emtpy state | |
} else { | |
// Signed-in, let's init our adapter | |
val options = FirestoreRecyclerOptions.Builder<Team>() | |
.setSnapshotArray(snapshots) | |
.setLifecycleOwner(this) | |
.build() | |
adapter = object : FirestoreRecyclerAdapter<Team, TeamViewHolder>(options) { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TeamViewHolder = | |
TeamViewHolder(LayoutInflater.from(parent.context) | |
.inflate(R.layout.team_list_row_layout, parent, false)) | |
override fun onBindViewHolder(teamHolder: TeamViewHolder, position: Int, team: Team) { | |
// Bind your views | |
} | |
} | |
recyclerView.adapter = adapter | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment