From 37b7081a65b04c975af3b933023841f2f4d64e03 Mon Sep 17 00:00:00 2001 From: manbradcalf Date: Sat, 17 Jul 2021 18:42:51 -0400 Subject: [PATCH 1/7] trying to figure why i used flowables over observables --- .../data/contacts/ContactsDataSource.java | 3 ++- .../data/contacts/ContactsRepoRxJava.kt | 15 ++++++++++----- .../bookyrself/data/profile/ProfileRepo.kt | 17 ++++++++--------- .../bookyrself/services/FirebaseService.kt | 4 +++- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsDataSource.java b/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsDataSource.java index c95bf1e..1d592c4 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsDataSource.java +++ b/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsDataSource.java @@ -5,9 +5,10 @@ import java.util.Map; import io.reactivex.Flowable; +import io.reactivex.Observable; interface ContactsDataSource { - Flowable> getContactsForUser(String userId); + Observable> getContactsForUser(String userId); } diff --git a/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsRepoRxJava.kt b/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsRepoRxJava.kt index c6b5fbc..086dc60 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsRepoRxJava.kt +++ b/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsRepoRxJava.kt @@ -6,8 +6,12 @@ import com.bookyrself.bookyrself.services.FirebaseService import com.google.firebase.auth.FirebaseAuth import com.google.firebase.database.* import io.reactivex.Flowable +import io.reactivex.Observable +import io.reactivex.Single import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.schedulers.Schedulers +import io.reactivex.subjects.PublishSubject +import io.reactivex.subjects.Subject import java.util.* class ContactsRepoRxJava : ContactsDataSource { @@ -62,7 +66,7 @@ class ContactsRepoRxJava : ContactsDataSource { /** * Methods */ - override fun getContactsForUser(userId: String): Flowable> { + override fun getContactsForUser(userId: String): Observable> { if (cacheIsDirty!!) { // Cache is dirty, get from network @@ -72,14 +76,15 @@ class ContactsRepoRxJava : ContactsDataSource { .observeOn(AndroidSchedulers.mainThread()) .map { it.entries } .firstOrError() - .toFlowable() - .flatMapIterable { entries -> entries } + .flattenAsObservable { it } .flatMap { entry -> FirebaseService.instance + // Get each contacts user info .getUserDetails(entry.key) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .map { + // add the contact to our user's contacts repository contactsMap[entry.key] = it cacheIsDirty = false AbstractMap.SimpleEntry(entry.key, it) @@ -87,8 +92,8 @@ class ContactsRepoRxJava : ContactsDataSource { } } else { // Cache is clean, get local copy - return Flowable.fromIterable(contactsMap.entries) + return Observable.fromIterable(contactsMap.entries) .map { AbstractMap.SimpleEntry(it.key, it.value) } } } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/bookyrself/bookyrself/data/profile/ProfileRepo.kt b/app/src/main/java/com/bookyrself/bookyrself/data/profile/ProfileRepo.kt index 4f55545..731dd11 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/data/profile/ProfileRepo.kt +++ b/app/src/main/java/com/bookyrself/bookyrself/data/profile/ProfileRepo.kt @@ -5,7 +5,7 @@ import com.bookyrself.bookyrself.services.FirebaseService import com.bookyrself.bookyrself.services.FirebaseServiceCoroutines import com.google.firebase.auth.FirebaseAuth import com.google.firebase.database.* -import io.reactivex.Flowable +import io.reactivex.Single import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.schedulers.Schedulers @@ -19,8 +19,8 @@ class ProfileRepo { val userId = FirebaseAuth.getInstance().uid this.db = FirebaseDatabase.getInstance().reference - .child("users") - .child(userId!!) + .child("users") + .child(userId!!) this.db!!.addChildEventListener(object : ChildEventListener { override fun onChildAdded(dataSnapshot: DataSnapshot, s: String?) { @@ -48,13 +48,12 @@ class ProfileRepo { } - fun getProfileInfo(userId: String): Flowable { + fun getProfileInfo(userId: String): Single { return FirebaseService.instance - .getUserDetails(userId) - .firstOrError() - .toFlowable() - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) + .getUserDetails(userId) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .singleOrError() } suspend fun updateProfileInfo(userId: String, user: User): ProfileRepoResponse { diff --git a/app/src/main/java/com/bookyrself/bookyrself/services/FirebaseService.kt b/app/src/main/java/com/bookyrself/bookyrself/services/FirebaseService.kt index 311a3d7..02f9cf7 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/services/FirebaseService.kt +++ b/app/src/main/java/com/bookyrself/bookyrself/services/FirebaseService.kt @@ -6,6 +6,8 @@ import com.bookyrself.bookyrself.data.serverModels.EventDetail.Host import com.bookyrself.bookyrself.data.serverModels.User.EventInviteInfo import com.bookyrself.bookyrself.data.serverModels.User.User import io.reactivex.Flowable +import io.reactivex.Observable +import io.reactivex.Single import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Retrofit @@ -38,7 +40,7 @@ object FirebaseService { interface FirebaseApi { @GET("/users/{id}.json") - fun getUserDetails(@Path("id") userId: String): Flowable + fun getUserDetails(@Path("id") userId: String): Observable @GET("/users/{id}/events.json") fun getUsersEventInvites(@Path("id") userId: String): Flowable> From 26bbad8ba8ae898436a119f53d13f52c91a00233 Mon Sep 17 00:00:00 2001 From: manbradcalf Date: Sat, 17 Jul 2021 19:46:57 -0400 Subject: [PATCH 2/7] begin transition of profile to MVVM away from MVP --- .../data/contacts/ContactsRepoRxJava.kt | 52 ++++++++-------- .../data/events/EventsRepository.kt | 2 +- .../bookyrself/data/profile/ProfileRepo.kt | 60 +++++++++++-------- .../viewmodels/EventsFragmentViewModel.kt | 5 +- .../viewmodels/ProfileFragmentViewModel.kt | 23 +++++++ 5 files changed, 84 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsRepoRxJava.kt b/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsRepoRxJava.kt index 086dc60..0c6f50f 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsRepoRxJava.kt +++ b/app/src/main/java/com/bookyrself/bookyrself/data/contacts/ContactsRepoRxJava.kt @@ -5,13 +5,9 @@ import com.bookyrself.bookyrself.data.serverModels.User.User import com.bookyrself.bookyrself.services.FirebaseService import com.google.firebase.auth.FirebaseAuth import com.google.firebase.database.* -import io.reactivex.Flowable import io.reactivex.Observable -import io.reactivex.Single import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.schedulers.Schedulers -import io.reactivex.subjects.PublishSubject -import io.reactivex.subjects.Subject import java.util.* class ContactsRepoRxJava : ContactsDataSource { @@ -28,13 +24,13 @@ class ContactsRepoRxJava : ContactsDataSource { cacheIsDirty = true } - if (FirebaseAuth.getInstance().uid != null) { + FirebaseAuth.getInstance().uid?.let { this.db = FirebaseDatabase.getInstance().reference - .child("users") - .child(FirebaseAuth.getInstance().uid!!) - .child("contacts") + .child("users") + .child(it) + .child("contacts") - this.db!!.addChildEventListener(object : ChildEventListener { + this.db?.addChildEventListener(object : ChildEventListener { override fun onChildAdded(dataSnapshot: DataSnapshot, s: String?) { cacheIsDirty = true Log.e("Contacts Repo: ", "Child added") @@ -71,29 +67,29 @@ class ContactsRepoRxJava : ContactsDataSource { if (cacheIsDirty!!) { // Cache is dirty, get from network return FirebaseService.instance - .getUserContacts(userId) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .map { it.entries } - .firstOrError() - .flattenAsObservable { it } - .flatMap { entry -> - FirebaseService.instance + .getUserContacts(userId) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .map { it.entries } + .firstOrError() + .flattenAsObservable { it } + .flatMap { entry -> + FirebaseService.instance // Get each contacts user info - .getUserDetails(entry.key) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .map { - // add the contact to our user's contacts repository - contactsMap[entry.key] = it - cacheIsDirty = false - AbstractMap.SimpleEntry(entry.key, it) - } - } + .getUserDetails(entry.key) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .map { + // add the contact to our user's contacts repository + contactsMap[entry.key] = it + cacheIsDirty = false + AbstractMap.SimpleEntry(entry.key, it) + } + } } else { // Cache is clean, get local copy return Observable.fromIterable(contactsMap.entries) - .map { AbstractMap.SimpleEntry(it.key, it.value) } + .map { AbstractMap.SimpleEntry(it.key, it.value) } } } } \ No newline at end of file diff --git a/app/src/main/java/com/bookyrself/bookyrself/data/events/EventsRepository.kt b/app/src/main/java/com/bookyrself/bookyrself/data/events/EventsRepository.kt index eb71276..1783bef 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/data/events/EventsRepository.kt +++ b/app/src/main/java/com/bookyrself/bookyrself/data/events/EventsRepository.kt @@ -35,7 +35,7 @@ class EventsRepository private constructor(context: Context) { .child(FirebaseAuth.getInstance().uid!!) .child("events") - this.db!!.addChildEventListener(object : ChildEventListener { + this.db?.addChildEventListener(object : ChildEventListener { override fun onChildAdded(dataSnapshot: DataSnapshot, s: String?) { cacheIsDirty = true } diff --git a/app/src/main/java/com/bookyrself/bookyrself/data/profile/ProfileRepo.kt b/app/src/main/java/com/bookyrself/bookyrself/data/profile/ProfileRepo.kt index 731dd11..15546d8 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/data/profile/ProfileRepo.kt +++ b/app/src/main/java/com/bookyrself/bookyrself/data/profile/ProfileRepo.kt @@ -1,59 +1,66 @@ package com.bookyrself.bookyrself.data.profile +import android.content.Context +import com.bookyrself.bookyrself.data.SingletonHolder +import com.bookyrself.bookyrself.data.profile.ProfileRepoResponse.Success +import com.bookyrself.bookyrself.data.profile.ProfileRepoResponse.Failure import com.bookyrself.bookyrself.data.serverModels.User.User -import com.bookyrself.bookyrself.services.FirebaseService import com.bookyrself.bookyrself.services.FirebaseServiceCoroutines import com.google.firebase.auth.FirebaseAuth import com.google.firebase.database.* -import io.reactivex.Single -import io.reactivex.android.schedulers.AndroidSchedulers -import io.reactivex.schedulers.Schedulers -class ProfileRepo { +class ProfileRepo private constructor(context: Context) { private var db: DatabaseReference? = null + private var cacheIsDirty: Boolean = true + private var user: User? = null - init { - if (FirebaseAuth.getInstance().uid != null) { - - val userId = FirebaseAuth.getInstance().uid + companion object : SingletonHolder(::ProfileRepo) + init { + FirebaseAuth.getInstance().uid?.let { userId -> this.db = FirebaseDatabase.getInstance().reference .child("users") - .child(userId!!) + .child(userId) - this.db!!.addChildEventListener(object : ChildEventListener { + this.db?.addChildEventListener(object : ChildEventListener { override fun onChildAdded(dataSnapshot: DataSnapshot, s: String?) { // Update repos observable to fetch new data - getProfileInfo(userId) + cacheIsDirty = true } override fun onChildChanged(dataSnapshot: DataSnapshot, s: String?) { - getProfileInfo(userId) + cacheIsDirty = true } override fun onChildRemoved(dataSnapshot: DataSnapshot) { - getProfileInfo(userId) + cacheIsDirty = true } override fun onChildMoved(dataSnapshot: DataSnapshot, s: String?) { - getProfileInfo(userId) + cacheIsDirty = true } override fun onCancelled(databaseError: DatabaseError) { - getProfileInfo(userId) + cacheIsDirty = true } }) } } - fun getProfileInfo(userId: String): Single { - return FirebaseService.instance - .getUserDetails(userId) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .singleOrError() + suspend fun getProfileInfo(userId: String): ProfileRepoResponse { + return if (cacheIsDirty) { + val profileResponse = FirebaseServiceCoroutines.instance.getUserDetails(userId) + if (profileResponse.isSuccessful) { + user = profileResponse.body() + Success(user = profileResponse.body()) + } else { + Failure(errorMessage = profileResponse.message()) + } + } else { + Success(user) + } } suspend fun updateProfileInfo(userId: String, user: User): ProfileRepoResponse { @@ -64,9 +71,10 @@ class ProfileRepo { ProfileRepoResponse.Failure(response.message()) } } +} - sealed class ProfileRepoResponse { - class Success(val user: User?) : ProfileRepoResponse() - class Failure(val errorMessage: String) : ProfileRepoResponse() - } + +sealed class ProfileRepoResponse { + class Success(val user: User?) : ProfileRepoResponse() + class Failure(val errorMessage: String) : ProfileRepoResponse() } diff --git a/app/src/main/java/com/bookyrself/bookyrself/viewmodels/EventsFragmentViewModel.kt b/app/src/main/java/com/bookyrself/bookyrself/viewmodels/EventsFragmentViewModel.kt index dc2edcd..4bb7049 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/viewmodels/EventsFragmentViewModel.kt +++ b/app/src/main/java/com/bookyrself/bookyrself/viewmodels/EventsFragmentViewModel.kt @@ -17,13 +17,12 @@ class EventsFragmentViewModel(application: Application) : BaseViewModel(applicat var eventDetails = MutableLiveData>() override fun load() { - val userId = FirebaseAuth.getInstance().uid - if (userId != null) { + FirebaseAuth.getInstance().uid?.let { CoroutineScope(Dispatchers.IO).launch { when (val response = EventsRepository .getInstance(getApplication()) - .getAllEventsForUser(userId)) { + .getAllEventsForUser(it)) { is Success -> { eventDetails.postValue(response.events) } diff --git a/app/src/main/java/com/bookyrself/bookyrself/viewmodels/ProfileFragmentViewModel.kt b/app/src/main/java/com/bookyrself/bookyrself/viewmodels/ProfileFragmentViewModel.kt index e141cbe..5928d9e 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/viewmodels/ProfileFragmentViewModel.kt +++ b/app/src/main/java/com/bookyrself/bookyrself/viewmodels/ProfileFragmentViewModel.kt @@ -2,11 +2,34 @@ package com.bookyrself.bookyrself.viewmodels import android.app.Application import androidx.lifecycle.MutableLiveData +import com.bookyrself.bookyrself.data.profile.ProfileRepo import com.bookyrself.bookyrself.data.serverModels.EventDetail.EventDetail import com.bookyrself.bookyrself.data.serverModels.User.User +import com.google.firebase.auth.FirebaseAuth +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import com.bookyrself.bookyrself.data.profile.ProfileRepoResponse.Success +import com.bookyrself.bookyrself.data.profile.ProfileRepoResponse.Failure + class ProfileFragmentViewModel(application: Application) : BaseViewModel(application) { var user = MutableLiveData() var events = MutableLiveData>() + + override fun load() { + FirebaseAuth.getInstance().uid?.let { + CoroutineScope(Dispatchers.IO).launch { + when (val response = ProfileRepo.getInstance(getApplication()).getProfileInfo(it)) { + is Success -> { + user.postValue(response.user) + } + is Failure -> { + errorMessage.postValue(response.errorMessage) + } + } + } + } + } } \ No newline at end of file From 028cc19be9d34f8fa2c380643a62e4907d42c97f Mon Sep 17 00:00:00 2001 From: manbradcalf Date: Sun, 18 Jul 2021 12:58:29 -0400 Subject: [PATCH 3/7] New ProfileFragment in Kotlin and renaming the old Java one to keep it around for reference --- .../views/fragments/ProfileFragment.kt | 135 ++++++++++++++++++ ...Fragment.java => ProfileFragmentJava.java} | 8 +- 2 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/bookyrself/bookyrself/views/fragments/ProfileFragment.kt rename app/src/main/java/com/bookyrself/bookyrself/views/fragments/{ProfileFragment.java => ProfileFragmentJava.java} (97%) diff --git a/app/src/main/java/com/bookyrself/bookyrself/views/fragments/ProfileFragment.kt b/app/src/main/java/com/bookyrself/bookyrself/views/fragments/ProfileFragment.kt new file mode 100644 index 0000000..2f31318 --- /dev/null +++ b/app/src/main/java/com/bookyrself/bookyrself/views/fragments/ProfileFragment.kt @@ -0,0 +1,135 @@ +package com.bookyrself.bookyrself.views.fragments + +import android.os.Bundle +import android.text.Html +import android.text.method.LinkMovementMethod +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.lifecycle.ViewModelProviders +import androidx.lifecycle.observe +import com.bookyrself.bookyrself.R +import com.bookyrself.bookyrself.data.serverModels.EventDetail.EventDetail +import com.bookyrself.bookyrself.data.serverModels.User.User +import com.bookyrself.bookyrself.viewmodels.ProfileFragmentViewModel +import com.google.firebase.auth.FirebaseAuth +import kotlinx.android.synthetic.main.empty_state_template.* +import kotlinx.android.synthetic.main.fragment_profile.* +import java.util.* + +class ProfileFragment : BaseFragment() { + + lateinit var model: ProfileFragmentViewModel + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + model = ViewModelProviders.of( + this, + ProfileFragmentViewModel.ProfileFragmentViewModelFactory(activity!!.application) + ).get(ProfileFragmentViewModel::class.java) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + return inflater.inflate(R.layout.fragment_profile, container, false) + } + + override fun onResume() { + super.onResume() + if (FirebaseAuth.getInstance().uid != null) { + showContent(false) + showLoadingState(true) + setHasOptionsMenu(true) + setListeners() + model.load() + } else { + showSignedOutEmptyState() + } + } + + private fun setListeners() { + model.events.observe(this) { events -> + eventsReturned(events) + } + + model.user.observe(this) { user -> + userReturned(user) + } + } + + private fun eventsReturned(events: HashMap?) { + events?.let { + it.forEach { event -> + // TODO set drawable on Calendar for event.value.date + // TODO implement OnDateSelectedListener for the date + } + } + } + + private fun userReturned(user: User) { + resetView() + showLoadingState(false) + profile_empty_state.visibility = View.GONE + + user.username?.let { username_profile_fragment.text = it } + user.citystate?.let { city_state_profile_activity.text = it } + user.bio?.let { bio_body_profile_activity.text = it } + + user.tags?.let { + val tagsString = it.toString().replace("\\[|]|, $".toRegex(), "") + tags_profile_activity.text = tagsString + } + + user.url?.let { + user_url_profile_activity.isClickable = true + user_url_profile_activity.movementMethod = LinkMovementMethod.getInstance() + user_url_profile_activity.text = Html.fromHtml( + String.format( + "%s ", + "http://$it", it + ) + ) + } + + user.bio?.let { bio_body_profile_activity.text = it } + + showContent(true) + } + + override fun showContent(show: Boolean) { + if (show) { + profile_content.visibility = View.VISIBLE + } else { + profile_content.visibility = View.GONE + } + } + + fun showSignedOutEmptyState() { + showEmptyState( + getString(R.string.auth_val_prop_header), + getString(R.string.auth_val_prop_subheader), + activity!!.getDrawable(R.drawable.ic_no_auth_profile), + getString(R.string.sign_in) + ) + toolbar_profile.title = getString(R.string.profile_toolbar_placeholder) + setMenuVisibility(false) + } + + override fun showLoadingState(show: Boolean) { + if (show) { + profile_fragment_progressbar.visibility = View.VISIBLE + } else { + profile_fragment_progressbar.visibility = View.GONE + } + } + + private fun resetView() { + tags_profile_activity.text = getString(R.string.profile_tags_placeholder) + bio_body_profile_activity.text = getString(R.string.profile_bio_placeholder) + city_state_profile_activity.text = getString(R.string.profile_citystate_placeholder) + user_url_profile_activity.text = getString(R.string.profile_url_placeholder) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/bookyrself/bookyrself/views/fragments/ProfileFragment.java b/app/src/main/java/com/bookyrself/bookyrself/views/fragments/ProfileFragmentJava.java similarity index 97% rename from app/src/main/java/com/bookyrself/bookyrself/views/fragments/ProfileFragment.java rename to app/src/main/java/com/bookyrself/bookyrself/views/fragments/ProfileFragmentJava.java index f27b792..d9ab399 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/views/fragments/ProfileFragment.java +++ b/app/src/main/java/com/bookyrself/bookyrself/views/fragments/ProfileFragmentJava.java @@ -60,7 +60,7 @@ import static com.bookyrself.bookyrself.views.activities.AuthenticationActivity.USERNAME_KEY; import static java.util.Objects.requireNonNull; -public class ProfileFragment extends BaseFragment implements OnDateSelectedListener, ProfileFragmentPresenter.ProfilePresenterListener { +public class ProfileFragmentJava extends BaseFragment implements OnDateSelectedListener, ProfileFragmentPresenter.ProfilePresenterListener { private static final int RC_SIGN_IN = 123; private static final int RC_PROFILE_EDIT = 789; @@ -105,9 +105,9 @@ public class ProfileFragment extends BaseFragment implements OnDateSelectedListe private StorageReference storageReference; private HashMap calendarDaysWithEventIds; private User user; - private List acceptedEventsCalendarDays = new ArrayList<>(); - private List pendingEventsCalendarDays = new ArrayList<>(); - private List unavailableDates = new ArrayList<>(); + private final List acceptedEventsCalendarDays = new ArrayList<>(); + private final List pendingEventsCalendarDays = new ArrayList<>(); + private final List unavailableDates = new ArrayList<>(); @Override public void onCreate(Bundle savedInstanceState) { From 4f0fd56840260ce1b313dcfd3eb5cd5bccdfa604 Mon Sep 17 00:00:00 2001 From: manbradcalf Date: Sun, 18 Jul 2021 12:59:01 -0400 Subject: [PATCH 4/7] add VMFactory for ProfileFragmentViewModel --- .../viewmodels/ProfileFragmentViewModel.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/bookyrself/bookyrself/viewmodels/ProfileFragmentViewModel.kt b/app/src/main/java/com/bookyrself/bookyrself/viewmodels/ProfileFragmentViewModel.kt index 5928d9e..f768968 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/viewmodels/ProfileFragmentViewModel.kt +++ b/app/src/main/java/com/bookyrself/bookyrself/viewmodels/ProfileFragmentViewModel.kt @@ -2,15 +2,17 @@ package com.bookyrself.bookyrself.viewmodels import android.app.Application import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider import com.bookyrself.bookyrself.data.profile.ProfileRepo +import com.bookyrself.bookyrself.data.profile.ProfileRepoResponse.Failure +import com.bookyrself.bookyrself.data.profile.ProfileRepoResponse.Success import com.bookyrself.bookyrself.data.serverModels.EventDetail.EventDetail import com.bookyrself.bookyrself.data.serverModels.User.User import com.google.firebase.auth.FirebaseAuth import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import com.bookyrself.bookyrself.data.profile.ProfileRepoResponse.Success -import com.bookyrself.bookyrself.data.profile.ProfileRepoResponse.Failure class ProfileFragmentViewModel(application: Application) : BaseViewModel(application) { @@ -32,4 +34,11 @@ class ProfileFragmentViewModel(application: Application) : BaseViewModel(applica } } } + + class ProfileFragmentViewModelFactory(private val application: Application) : + ViewModelProvider.Factory { + override fun create(modelClass: Class): T { + return ProfileFragmentViewModel(application) as T + } + } } \ No newline at end of file From 544ed4ceafe64ec80cc9f58bf82dc41d471324a2 Mon Sep 17 00:00:00 2001 From: manbradcalf Date: Sun, 18 Jul 2021 12:59:36 -0400 Subject: [PATCH 5/7] Remove ProfileRepo from MainActivity --- .../bookyrself/views/activities/MainActivity.kt | 9 --------- 1 file changed, 9 deletions(-) diff --git a/app/src/main/java/com/bookyrself/bookyrself/views/activities/MainActivity.kt b/app/src/main/java/com/bookyrself/bookyrself/views/activities/MainActivity.kt index c041b1f..4e3762c 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/views/activities/MainActivity.kt +++ b/app/src/main/java/com/bookyrself/bookyrself/views/activities/MainActivity.kt @@ -69,7 +69,6 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS private const val PROFILE_FRAGMENT_INDEX = 4 private var CONTACTS_REPO: ContactsRepoRxJava? = null - private var PROFILE_REPO: ProfileRepo? = null //TODO: Fix all these !! and find a better way to serve up these repos val contactsRepo: ContactsRepoRxJava @@ -79,13 +78,5 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS } return CONTACTS_REPO!! } - - val profileRepo: ProfileRepo - get() { - if (PROFILE_REPO == null) { - PROFILE_REPO = ProfileRepo() - } - return PROFILE_REPO!! - } } } From db2abc847cec5f9515e72eebd275efecd6bc19bb Mon Sep 17 00:00:00 2001 From: manbradcalf Date: Sun, 18 Jul 2021 13:01:44 -0400 Subject: [PATCH 6/7] wip ProfileEditActivity updates to consume updated ProfileRepo --- .../views/activities/ProfileEditActivity.kt | 60 ++++++++++++++----- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/bookyrself/bookyrself/views/activities/ProfileEditActivity.kt b/app/src/main/java/com/bookyrself/bookyrself/views/activities/ProfileEditActivity.kt index 060cc0c..27e455b 100644 --- a/app/src/main/java/com/bookyrself/bookyrself/views/activities/ProfileEditActivity.kt +++ b/app/src/main/java/com/bookyrself/bookyrself/views/activities/ProfileEditActivity.kt @@ -10,8 +10,8 @@ import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.bookyrself.bookyrself.R import com.bookyrself.bookyrself.data.profile.ProfileRepo -import com.bookyrself.bookyrself.data.profile.ProfileRepo.ProfileRepoResponse.Failure -import com.bookyrself.bookyrself.data.profile.ProfileRepo.ProfileRepoResponse.Success +import com.bookyrself.bookyrself.data.profile.ProfileRepoResponse.Success +import com.bookyrself.bookyrself.data.profile.ProfileRepoResponse.Failure import com.bookyrself.bookyrself.data.serverModels.EventDetail.Host import com.bookyrself.bookyrself.data.serverModels.User.User import com.bookyrself.bookyrself.services.FirebaseServiceCoroutines @@ -39,7 +39,7 @@ class ProfileEditActivity : AppCompatActivity() { public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) //TODO: Move to viewmodel - this.profileRepo = MainActivity.profileRepo + this.profileRepo = ProfileRepo.getInstance(this) setContentView(R.layout.activity_profile_edit) // Set any existing data @@ -51,10 +51,17 @@ class ProfileEditActivity : AppCompatActivity() { // TODO: The following code is mostly duplicated from eventcreationactivity // Initialize the AutocompleteSupportFragment. - val autocompleteFragment = supportFragmentManager.findFragmentById(R.id.autocomplete_fragment_profile_edit) as AutocompleteSupportFragment? + val autocompleteFragment = + supportFragmentManager.findFragmentById(R.id.autocomplete_fragment_profile_edit) as AutocompleteSupportFragment? // Specify the types of place data to return. - autocompleteFragment!!.setPlaceFields(listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG)) + autocompleteFragment!!.setPlaceFields( + listOf( + Place.Field.ID, + Place.Field.NAME, + Place.Field.LAT_LNG + ) + ) autocompleteFragment.setHint(intent.getStringExtra("Location")) autocompleteFragment.setTypeFilter(TypeFilter.CITIES) val geocoder = Geocoder(this, Locale.getDefault()) @@ -64,10 +71,15 @@ class ProfileEditActivity : AppCompatActivity() { override fun onPlaceSelected(place: Place) { // TODO: Get info about the selected place. try { - val addresses = geocoder.getFromLocation(place.latLng!!.latitude, place.latLng!!.longitude, 1) + val addresses = geocoder.getFromLocation( + place.latLng!!.latitude, + place.latLng!!.longitude, + 1 + ) if (addresses != null && addresses.size > 0) { val cityState = addresses[0].locality + ", " + addresses[0].adminArea - val etPlace = autocompleteFragment.view?.findViewById(R.id.places_autocomplete_search_input) as EditText + val etPlace = + autocompleteFragment.view?.findViewById(R.id.places_autocomplete_search_input) as EditText etPlace.hint = cityState user.citystate = cityState } @@ -84,7 +96,8 @@ class ProfileEditActivity : AppCompatActivity() { }) if (intent.getStringExtra("Tags") != null) { - intent.getStringExtra("Tags").let { profile_edit_tags.setText(it.replace("\\[|]|, $".toRegex(), "")) } + intent.getStringExtra("Tags") + .let { profile_edit_tags.setText(it.replace("\\[|]|, $".toRegex(), "")) } } profile_edit_fab.setOnClickListener { @@ -95,7 +108,10 @@ class ProfileEditActivity : AppCompatActivity() { user.url = profile_edit_url.text.toString() val tagsString = profile_edit_tags.text.toString() - val tagsList = Arrays.asList(*tagsString.split("\\s*,\\s*".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) + val tagsList = Arrays.asList( + *tagsString.split("\\s*,\\s*".toRegex()).dropLastWhile { it.isEmpty() } + .toTypedArray() + ) user.tags = tagsList // Update the user @@ -104,8 +120,8 @@ class ProfileEditActivity : AppCompatActivity() { when (profileRepo!!.updateProfileInfo(FirebaseAuth.getInstance().uid!!, user)) { is Success -> { val userEventsResponse = FirebaseServiceCoroutines - .instance - .getUsersEventInvites(FirebaseAuth.getInstance().uid!!) + .instance + .getUsersEventInvites(FirebaseAuth.getInstance().uid!!) if (userEventsResponse.isSuccessful) { if (userEventsResponse.body() != null) { @@ -116,10 +132,18 @@ class ProfileEditActivity : AppCompatActivity() { host.url = user.url host.citystate = user.citystate userEventsResponse.body()!!.filter { it.value.isHost }.forEach { - val updateEventHostResponse = FirebaseServiceCoroutines.instance.updateEventHost(host, it.key) + val updateEventHostResponse = + FirebaseServiceCoroutines.instance.updateEventHost( + host, + it.key + ) if (updateEventHostResponse.errorBody() != null) { withContext(Dispatchers.Main) { - Toast.makeText(applicationContext, "Oh no! Something went wrong updating your events", Toast.LENGTH_LONG).show() + Toast.makeText( + applicationContext, + "Oh no! Something went wrong updating your events", + Toast.LENGTH_LONG + ).show() setResult(Activity.RESULT_OK) finish() } @@ -128,8 +152,8 @@ class ProfileEditActivity : AppCompatActivity() { } } val profileUpdate = UserProfileChangeRequest.Builder() - .setDisplayName(profile_edit_username.text.toString()) - .build() + .setDisplayName(profile_edit_username.text.toString()) + .build() FirebaseAuth.getInstance().currentUser!!.updateProfile(profileUpdate) // Finish the activity with a success @@ -137,7 +161,11 @@ class ProfileEditActivity : AppCompatActivity() { finish() } is Failure -> { - Toast.makeText(applicationContext, "Oh no! Something went wrong updating your profile", Toast.LENGTH_LONG).show() + Toast.makeText( + applicationContext, + "Oh no! Something went wrong updating your profile", + Toast.LENGTH_LONG + ).show() setResult(Activity.RESULT_OK) finish() } From 84eb8233b55ab39e952883d8e7e4e42aa7a66f35 Mon Sep 17 00:00:00 2001 From: manbradcalf Date: Sun, 18 Jul 2021 13:02:03 -0400 Subject: [PATCH 7/7] add string resources for text placeholders on ProfileFragment --- app/src/main/res/values/strings.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dd594d0..b50ff64 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -85,6 +85,11 @@ Email User Image City, State + Add some tags! + Tell us about yourself! + Add your city! + Add your website! + Your Profile