Skip to content

Commit cc5ebdb

Browse files
committed
Android - #17
- FAB changes function based on state
1 parent 2572c06 commit cc5ebdb

File tree

8 files changed

+190
-77
lines changed

8 files changed

+190
-77
lines changed

android/SmartIRHub/app/src/main/java/com/ms8/smartirhub/android/database/AppState.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ object AppState {
2222
val remotes : ObservableArrayMap<String, RemoteProfile> = ObservableArrayMap(),
2323
val remoteTemplates : ObservableArrayMap<String, RemoteProfileTemplate> = ObservableArrayMap(),
2424
val irSignals : ObservableArrayMap<String, IrSignal> = ObservableArrayMap(),
25-
var user : User = User()
25+
val user : User = User()
2626
) {
2727

2828
fun removeData() {
29-
user = User()
29+
user.clear()
3030
groups.clear()
3131
remotes.clear()
3232
hubs.clear()

android/SmartIRHub/app/src/main/java/com/ms8/smartirhub/android/firebase/AuthActions.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ object AuthActions {
9696
val account = task.getResult(ApiException::class.java)
9797
val credential = GoogleAuthProvider.getCredential(account!!.idToken, null)
9898
FirebaseAuth.getInstance().signInWithCredential(credential)
99-
.addOnSuccessListener {
100-
Log.d("TEST###", "Google sign in good. setting uid to ${FirebaseAuth.getInstance().currentUser!!.uid}")
101-
AppState.userData.user.uid.set(FirebaseAuth.getInstance().currentUser!!.uid)
102-
}
99+
.addOnSuccessListener { FirestoreActions.getUserFromUID() }
103100
.addOnFailureListener { e -> AppState.errorData.userSignInError.set(e) }
104101

105102
} catch (e : Exception) {

android/SmartIRHub/app/src/main/java/com/ms8/smartirhub/android/firebase/FirestoreActions.kt

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import com.ms8.smartirhub.android.database.AppState
1515
import com.ms8.smartirhub.android.models.firestore.IrSignal
1616
import com.ms8.smartirhub.android.models.firestore.*
1717
import com.ms8.smartirhub.android.remote_control.models.RemoteProfile
18-
import com.ms8.smartirhub.android.splash_login.SplashActivity3
1918
import org.jetbrains.anko.doAsync
2019
import java.lang.Exception
2120

@@ -25,6 +24,8 @@ object FirestoreActions {
2524
private var remoteProfileListeners : ArrayMap<String, ListenerRegistration> = ArrayMap()
2625
private var hubListeners : ArrayMap<String, ListenerRegistration> = ArrayMap()
2726

27+
private var bFetchingUser : Boolean = false
28+
2829
/*
2930
----------------------------------------------
3031
Retrieval Functions
@@ -37,21 +38,29 @@ object FirestoreActions {
3738

3839
@SuppressLint("LogNotTimber")
3940
fun getUserFromUID() {
40-
FirebaseFirestore.getInstance().collection("users")
41-
.whereEqualTo("uid", FirebaseAuth.getInstance().currentUser!!.uid).get()
42-
.addOnSuccessListener { snapshots ->
43-
if (snapshots.size() > 1)
44-
Log.e("getUserFromUID", "Received more than one user object from uid:" +
41+
if (!bFetchingUser) {
42+
bFetchingUser = true
43+
FirebaseFirestore.getInstance().collection("users")
44+
.whereEqualTo("uid", FirebaseAuth.getInstance().currentUser!!.uid).get()
45+
.addOnCompleteListener { bFetchingUser = false }
46+
.addOnFailureListener { e ->
47+
Log.e("getUserFromUID", "$e")
48+
AppState.errorData.userSignInError.set(e)
49+
}
50+
.addOnSuccessListener { snapshots ->
51+
if (snapshots.size() > 1)
52+
Log.e("getUserFromUID", "Received more than one user object from uid:" +
4553
" ${FirebaseAuth.getInstance().currentUser?.uid}")
4654

47-
if (snapshots.size() == 0) {
48-
AppState.userData.user.uid.set(FirebaseAuth.getInstance().currentUser!!.uid)
49-
} else {
50-
AppState.userData.user = User.fromSnapshot(snapshots.documents[0])
51-
}
55+
if (snapshots.size() == 0) {
56+
AppState.userData.user.uid.set(FirebaseAuth.getInstance().currentUser!!.uid)
57+
} else {
58+
AppState.userData.user.fromSnapshot(snapshots.documents[0])
59+
}
5260

53-
Log.d("TESTUSERUID", "user uid = ${AppState.userData.user.uid} | username = ${AppState.userData.user.username}")
54-
}
61+
Log.d("TESTUSERUID", "user uid = ${AppState.userData.user.uid} | username = ${AppState.userData.user.username}")
62+
}
63+
}
5564
}
5665

5766
@SuppressLint("LogNotTimber")
@@ -326,7 +335,7 @@ object FirestoreActions {
326335
!snapshot!!.exists() -> {Log.d("listenToUserData", "User data is null")}
327336
else -> {
328337
Log.d("TEST###", "User before: \n${AppState.userData.user}")
329-
AppState.userData.user = User.fromSnapshot(snapshot)
338+
AppState.userData.user.fromSnapshot(snapshot)
330339

331340
// listen to hubs
332341
hubListeners.clear()
@@ -620,7 +629,7 @@ object FirestoreActions {
620629
.set(User().apply {
621630
this.uid.set(uid)
622631
this.username.set(username)
623-
})
632+
}.toFirebaseObject())
624633
.addOnSuccessListener { listenToUserData2(username) }
625634
.addOnFailureListener { e -> AppState.errorData.userSignInError.set(e) }
626635
}
@@ -714,6 +723,10 @@ object FirestoreActions {
714723
return FirebaseFirestore.getInstance().collection("remotes").add(remote.toFirebaseObject())
715724
}
716725

726+
fun upateRemoteProfile() {
727+
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
728+
}
729+
717730

718731
const val TEST_REMOTE_PROFILE_TEMPLATE = "_TEST_TEMPLATE"
719732
const val TEST_USER = "_TEST_USER"

android/SmartIRHub/app/src/main/java/com/ms8/smartirhub/android/main_view/MainViewActivity.kt

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.ms8.smartirhub.android.main_view
33
import android.annotation.SuppressLint
44
import android.content.Context
55
import android.content.Intent
6+
import android.content.res.ColorStateList
67
import android.os.Bundle
78
import android.os.Parcel
89
import android.os.Parcelable
@@ -33,6 +34,7 @@ import com.ms8.smartirhub.android.utils.extensions.findNavBarHeight
3334
import android.util.TypedValue
3435
import android.view.animation.AccelerateInterpolator
3536
import android.view.animation.DecelerateInterpolator
37+
import androidx.core.content.ContextCompat
3638
import com.ms8.smartirhub.android.R
3739
import com.ms8.smartirhub.android.database.AppState
3840
import com.ms8.smartirhub.android.remote_control.views.asymmetric_gridview.Utils
@@ -203,6 +205,7 @@ class MainViewActivity : AppCompatActivity() {
203205
}
204206

205207
private fun createMockData() {
208+
AppState.tempData.tempRemoteProfile.inEditMode.set(true)
206209
AppState.tempData.tempRemoteProfile.buttons
207210
.apply {
208211
for (i in 0 until 50) {
@@ -418,8 +421,36 @@ class MainViewActivity : AppCompatActivity() {
418421
when (state.navPosition) {
419422
// My Remotes
420423
FP_MY_REMOTES -> {
421-
//binding.fab.labelText = getString(R.string.create_remote)
422-
binding.fab.setOnClickListener { createRemote() }
424+
when {
425+
state.viewPagerPosition == VP_FAV_REMOTE -> {
426+
if (AppState.userData.remotes.size == 0 && !AppState.tempData.tempRemoteProfile.inEditMode.get()) {
427+
// Creating first remote
428+
binding.fab.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.white))
429+
binding.fab.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_new_remote_icon))
430+
binding.fab.setOnClickListener { createRemote() }
431+
} else {
432+
when (AppState.tempData.tempRemoteProfile.inEditMode.get()) {
433+
// Editing current remote
434+
true -> {
435+
binding.fab.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.md_green_200))
436+
binding.fab.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_done_white_24dp))
437+
binding.fab.setOnClickListener { saveRemoteEdits() }
438+
}
439+
// Not editing current remote
440+
false -> {
441+
binding.fab.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.white))
442+
binding.fab.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_mode_edit_black_24dp))
443+
binding.fab.setOnClickListener { editRemote() }
444+
}
445+
}
446+
}
447+
}
448+
state.viewPagerPosition == VP_ALL_REMOTES -> {
449+
binding.fab.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.white))
450+
binding.fab.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_new_remote_icon))
451+
binding.fab.setOnClickListener { createRemote() }
452+
}
453+
}
423454
}
424455
// My Devices
425456
FP_MY_DEVICES -> {
@@ -528,6 +559,16 @@ class MainViewActivity : AppCompatActivity() {
528559
----------------------------------------------
529560
*/
530561

562+
private fun editRemote() {
563+
AppState.tempData.tempRemoteProfile.inEditMode.set(true)
564+
}
565+
566+
private fun saveRemoteEdits() {
567+
AppState.tempData.tempRemoteProfile.inEditMode.set(false)
568+
FirestoreActions.upateRemoteProfile()
569+
setupFab()
570+
}
571+
531572
private val remoteTemplatesSheet = RemoteTemplatesSheet().apply {
532573
templateSheetCallback = object : RemoteTemplatesSheet.RemoteTemplateSheetCallback{
533574
@SuppressLint("LogNotTimber")

android/SmartIRHub/app/src/main/java/com/ms8/smartirhub/android/models/firestore/User.kt

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.google.firebase.firestore.Exclude
1111
import com.google.firebase.firestore.IgnoreExtraProperties
1212
import java.lang.Exception
1313

14+
@Suppress("UNCHECKED_CAST")
1415
@IgnoreExtraProperties
1516
data class User(
1617
var defaultHub : String = "",
@@ -53,56 +54,55 @@ data class User(
5354
}
5455
}
5556

56-
@Suppress("UNCHECKED_CAST")
57-
companion object {
58-
@SuppressLint("LogNotTimber")
59-
fun fromSnapshot(snapshot: DocumentSnapshot) : User {
60-
val newUser = snapshot.toObject(User::class.java)
61-
?: User()
57+
fun clear() {
58+
defaultHub = ""
59+
uid.set("")
60+
username.set("")
61+
groups.clear()
62+
hubs.clear()
63+
irSignals.clear()
64+
remotes.clear()
65+
}
6266

63-
// set username
64-
newUser.username.set(snapshot.id)
67+
@SuppressLint("LogNotTimber")
68+
fun fromSnapshot(snapshot: DocumentSnapshot) {
69+
// set default hub
70+
defaultHub = snapshot["defaultHub"] as String
6571

66-
// set uid
67-
newUser.uid.set(FirebaseAuth.getInstance().currentUser?.uid ?: "")
72+
// set username
73+
username.set(snapshot.id)
6874

69-
// add hubs
70-
if (snapshot.contains("hubs")) {
71-
try {
72-
newUser.hubs = ObservableArrayList<String>()
73-
.apply {
74-
addAll(snapshot["hubs"] as Collection<String>)
75-
}
76-
} catch (e : Exception) {
77-
Log.e("User", "$e")
78-
}
79-
}
75+
// set uid
76+
uid.set(FirebaseAuth.getInstance().currentUser?.uid ?: "")
8077

81-
// add irSignals
82-
if (snapshot.contains("irSignals")) {
83-
try {
84-
newUser.irSignals = ObservableArrayList<String>()
85-
.apply {
86-
addAll(snapshot["hubs"] as Collection<String>)
87-
}
88-
} catch (e : Exception) {
89-
Log.e("User", "$e")
90-
}
78+
// add hubs
79+
if (snapshot.contains("hubs")) {
80+
try {
81+
hubs.clear()
82+
hubs.addAll(snapshot["hubs"] as Collection<String>)
83+
} catch (e : Exception) {
84+
Log.e("User", "$e")
9185
}
86+
}
9287

93-
// add remotes
94-
if (snapshot.contains("remotes")) {
95-
try {
96-
newUser.remotes = ObservableArrayList<String>()
97-
.apply {
98-
addAll(snapshot["remotes"] as Collection<String>)
99-
}
100-
} catch (e : Exception) {
101-
Log.e("user", "$e")
102-
}
88+
// add irSignals
89+
if (snapshot.contains("irSignals")) {
90+
try {
91+
irSignals.clear()
92+
irSignals.addAll(snapshot["hubs"] as Collection<String>)
93+
} catch (e : Exception) {
94+
Log.e("User", "$e")
10395
}
96+
}
10497

105-
return newUser
98+
// add remotes
99+
if (snapshot.contains("remotes")) {
100+
try {
101+
remotes.clear()
102+
remotes.addAll(snapshot["remotes"] as Collection<String>)
103+
} catch (e : Exception) {
104+
Log.e("user", "$e")
105+
}
106106
}
107107
}
108108
}

0 commit comments

Comments
 (0)