Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions app/src/main/java/com/fightpandemics/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.ui.setupActionBarWithNavController
import com.fightpandemics.R
import com.fightpandemics.core.utils.getBooleanPreference
import com.fightpandemics.core.utils.removePreference
import com.fightpandemics.utils.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.button.MaterialButton
Expand Down Expand Up @@ -53,6 +55,7 @@ class MainActivity : AppCompatActivity(), NavController.OnDestinationChangedList
setupBottomNavigationBar()
} // Else, need to wait for onRestoreInstanceState
setUpBottomNavigationAndFab()
checkThatUserSignedInFromProfile()
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
Expand Down Expand Up @@ -100,14 +103,19 @@ class MainActivity : AppCompatActivity(), NavController.OnDestinationChangedList
arguments: Bundle?,
) {
when (destination.id) {
R.id.homeFragment, R.id.searchFragment,
R.id.inboxFragment, R.id.profileFragment,
R.id.homeFragment
-> {
showBottomBar(destination)
dot.visibility = View.VISIBLE
fab.show()
}
R.id.filterFragment, R.id.createPostFragment, R.id.settingFragment -> hideBottomBar()
R.id.searchFragment, R.id.inboxFragment, R.id.profileFragment, R.id.indivProfileSettings, R.id.profileSignedOutFragment
-> {
showBottomBar(destination)
dot.visibility = View.VISIBLE
fab.hide()
}
R.id.filterFragment, R.id.createPostFragment, R.id.settingFragment, R.id.onboardFragment -> hideBottomBar()
}
}

Expand Down Expand Up @@ -248,4 +256,18 @@ class MainActivity : AppCompatActivity(), NavController.OnDestinationChangedList
}
dot.layoutParams = layoutParams
}

private fun checkThatUserSignedInFromProfile() {
val isUserSignedInFromProfile = getBooleanPreference(applicationContext, "isUserSignedInFromProfile", false)
val isUserSignedUpFromProfile = getBooleanPreference(applicationContext, "isUserSignedUpFromProfile", false)
if (isUserSignedInFromProfile || isUserSignedUpFromProfile) {
bottomNavigationView.selectedItemId = R.id.nav_profile
}
}

override fun onDestroy() {
super.onDestroy()
removePreference(applicationContext, "isUserSignedInFromProfile")
removePreference(applicationContext, "isUserSignedUpFromProfile")
}
}
28 changes: 28 additions & 0 deletions app/src/main/res/navigation/nav_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
android:id="@+id/action_profileFragment_to_settingFragment"
app:destination="@id/settingFragment"
app:launchSingleTop="true"/>
<action
android:id="@+id/action_profileFragment_to_profileSignedOutFragment"
app:destination="@id/profileSignedOutFragment" />
</fragment>

<include app:graph="@navigation/nav_create_post" />
Expand Down Expand Up @@ -151,4 +154,29 @@
</fragment>

<include app:graph="@navigation/nav_splash_onboard" />

<fragment
android:id="@+id/profileSignedOutFragment"
android:name="com.fightpandemics.profile.ui.profile.ProfileSignedOutFragment"
app:moduleName="Fight_Pandemics.profile"
android:label="Profile"
tools:layout="@layout/profile_signed_out_fragment">

<action
android:id="@+id/action_profileSignedOutFragment_to_signInFragment"
app:destination="@id/nav_sign_in"/>

<action
android:id="@+id/action_profileSignedOutFragment_to_signUpFragment"
app:destination="@id/nav_sign_up"/>

<action
android:id="@+id/action_profileSignedOutFragment_to_indivProfileSettings"
app:destination="@id/indivProfileSettings" />

</fragment>

<include app:graph="@navigation/nav_sign_in" />

<include app:graph="@navigation/nav_sign_up" />
</navigation>
3 changes: 3 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ dependencies {
// Location
implementation 'com.google.android.gms:play-services-location:17.1.0'

// Shared Preference
implementation 'androidx.preference:preference:1.1.1'

}
repositories {
mavenCentral()
Expand Down
234 changes: 234 additions & 0 deletions core/src/main/java/com/fightpandemics/core/utils/SharedPrefUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
package com.fightpandemics.core.utils

import android.content.Context
import android.content.SharedPreferences
import android.text.TextUtils
import androidx.preference.PreferenceManager


/**
* A pack of helpful getter and setter methods for reading/writing to {@link SharedPreferences}.
*/

/**
* Helper method to retrieve a String value from [SharedPreferences].
*
* @param context a [Context] object.
* @param key
* @return The value from shared preferences, or null if the value could not be read.
*/
fun getStringPreference(context: Context?, key: String?): String? {
var value: String? = null
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences != null) {
value = preferences.getString(key, null)
}
return value
}

/**
* Helper method to write a String value to [SharedPreferences].
*
* @param context a [Context] object.
* @param key
* @param value
* @return true if the new value was successfully written to persistent storage.
*/
fun setStringPreference(
context: Context?,
key: String?,
value: String?
): Boolean {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences != null && !TextUtils.isEmpty(key)) {
val editor = preferences.edit()
editor.putString(key, value)
return editor.commit()
}
return false
}

/**
* Helper method to retrieve a float value from [SharedPreferences].
*
* @param context a [Context] object.
* @param key
* @param defaultValue A default to return if the value could not be read.
* @return The value from shared preferences, or the provided default.
*/
fun getFloatPreference(
context: Context?,
key: String?,
defaultValue: Float
): Float {
var value = defaultValue
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences != null) {
value = preferences.getFloat(key, defaultValue)
}
return value
}

/**
* Helper method to write a float value to [SharedPreferences].
*
* @param context a [Context] object.
* @param key
* @param value
* @return true if the new value was successfully written to persistent storage.
*/
fun setFloatPreference(
context: Context?,
key: String?,
value: Float
): Boolean {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences != null) {
val editor = preferences.edit()
editor.putFloat(key, value)
return editor.commit()
}
return false
}

/**
* Helper method to retrieve a long value from [SharedPreferences].
*
* @param context a [Context] object.
* @param key
* @param defaultValue A default to return if the value could not be read.
* @return The value from shared preferences, or the provided default.
*/
fun getLongPreference(
context: Context?,
key: String?,
defaultValue: Long
): Long {
var value = defaultValue
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences != null) {
value = preferences.getLong(key, defaultValue)
}
return value
}

/**
* Helper method to write a long value to [SharedPreferences].
*
* @param context a [Context] object.
* @param key
* @param value
* @return true if the new value was successfully written to persistent storage.
*/
fun setLongPreference(
context: Context?,
key: String?,
value: Long
): Boolean {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences != null) {
val editor = preferences.edit()
editor.putLong(key, value)
return editor.commit()
}
return false
}

/**
* Helper method to retrieve an integer value from [SharedPreferences].
*
* @param context a [Context] object.
* @param key
* @param defaultValue A default to return if the value could not be read.
* @return The value from shared preferences, or the provided default.
*/
fun getIntegerPreference(context: Context?, key: String?, defaultValue: Int): Int {
var value = defaultValue
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences != null) {
value = preferences.getInt(key, defaultValue)
}
return value
}

/**
* Helper method to write an integer value to [SharedPreferences].
*
* @param context a [Context] object.
* @param key
* @param value
* @return true if the new value was successfully written to persistent storage.
*/
fun setIntegerPreference(
context: Context?,
key: String?,
value: Int
): Boolean {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences != null) {
val editor = preferences.edit()
editor.putInt(key, value)
return editor.commit()
}
return false
}

/**
* Helper method to retrieve a boolean value from [SharedPreferences].
*
* @param context a [Context] object.
* @param key
* @param defaultValue A default to return if the value could not be read.
* @return The value from shared preferences, or the provided default.
*/
fun getBooleanPreference(
context: Context?,
key: String?,
defaultValue: Boolean
): Boolean {
var value = defaultValue
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences != null) {
value = preferences.getBoolean(key, defaultValue)
}
return value
}

/**
* Helper method to write a boolean value to [SharedPreferences].
*
* @param context a [Context] object.
* @param key
* @param value
* @return true if the new value was successfully written to persistent storage.
*/
fun setBooleanPreference(
context: Context?,
key: String?,
value: Boolean
): Boolean {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences != null) {
val editor = preferences.edit()
editor.putBoolean(key, value)
return editor.commit()
}
return false
}

/**
* Helper method to delete a particular key from [SharedPreferences].
*
* @param context a [Context] object.
* @param key
* @return true if the new value was successfully written to persistent storage.
*/
fun removePreference(context: Context?, key: String?): Boolean {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences != null) {
val editor = preferences.edit()
editor.remove(key)
return editor.commit()
}
return false
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fightpandemics.profile.ui.profile

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
Expand Down Expand Up @@ -53,24 +54,30 @@ class IndivProfileSettings : Fragment() {
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.profile_settings, container, false)

@SuppressLint("SetTextI18n")
@ExperimentalCoroutinesApi
private fun bindListeners() {
appVersion.text = getString(R.string.fightpandemics_app_v) + com.fightpandemics.BuildConfig.VERSION_NAME
toolbar.setOnClickListener { activity?.onBackPressed() }
updatePublicProfileContainer.setOnClickListener {
findNavController().navigate(com.fightpandemics.R.id.action_indivProfileSettings_to_editProfileFragment)
}
updateAccountInfoContainer.setOnClickListener {
findNavController().navigate(com.fightpandemics.R.id.action_indivProfileSettings_to_editAccountFragment)
}
setupNotificationSettingsContainer.setOnClickListener { TODO() }
setupNotificationSettingsContainer.setOnClickListener { } //TO DO
myAccountContainer.setOnClickListener {
findNavController().navigate(com.fightpandemics.R.id.action_indivProfileSettings_to_nav_splash_onboard)
}
aboutUsContainer.setOnClickListener { openWebView(URLs.ABOUT_US) }
privacyPolicyContainer.setOnClickListener { openWebView(URLs.PRIVACY_POLICY) }
supportContainer.setOnClickListener { openWebView(URLs.SUPPORT) }
feedbackContainer.setOnClickListener { TODO() }
signoutContainer.setOnClickListener { TODO() }
feedbackContainer.setOnClickListener { } //TO DO
signoutContainer.setOnClickListener {
hideSignedInViews()
myAccountContainer.visibility = View.VISIBLE
profileViewModel.individualProfile.value = null
}
}

private fun hideSignedInViews() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class ProfileFragment : Fragment() {
super.onStart()
bindListeners()
profileViewModel.getIndividualProfile()
if (!profileViewModel.isUserSignedIn()) {
findNavController().navigate(com.fightpandemics.R.id.action_profileFragment_to_profileSignedOutFragment)
return
}
}

@ExperimentalCoroutinesApi
Expand Down
Loading