├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── raw
│ │ │ │ ├── home.png
│ │ │ │ ├── signin.png
│ │ │ │ ├── signup.png
│ │ │ │ ├── email_sent.jpg
│ │ │ │ ├── reset_password.png
│ │ │ │ ├── home_after_signin.png
│ │ │ │ └── home_after_signup.png
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── themes.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── drawable
│ │ │ │ ├── ic_error.xml
│ │ │ │ ├── ic_mail.xml
│ │ │ │ ├── ic_person_grey.xml
│ │ │ │ ├── ic_password.xml
│ │ │ │ ├── ic_username.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── fragment_home.xml
│ │ │ │ ├── fragment_password_reset.xml
│ │ │ │ ├── fragment_signin.xml
│ │ │ │ └── fragment_signup.xml
│ │ │ ├── values-night
│ │ │ │ └── themes.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── navigation
│ │ │ │ └── nav_graph.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── dev
│ │ │ │ └── james
│ │ │ │ └── firebasemvvm
│ │ │ │ ├── BaseApplication.kt
│ │ │ │ ├── repository
│ │ │ │ ├── firebase
│ │ │ │ │ ├── BaseAuthenticator.kt
│ │ │ │ │ └── FirebaseAuthenticator.kt
│ │ │ │ ├── BaseAuthRepository.kt
│ │ │ │ └── AuthRepository.kt
│ │ │ │ ├── di
│ │ │ │ └── AppModule.kt
│ │ │ │ ├── MainActivity.kt
│ │ │ │ ├── ui
│ │ │ │ ├── home
│ │ │ │ │ └── HomeFragment.kt
│ │ │ │ └── authentication
│ │ │ │ │ ├── ResetPasswordFragment.kt
│ │ │ │ │ ├── SignInFragment.kt
│ │ │ │ │ └── SignUpFragment.kt
│ │ │ │ └── viewmodels
│ │ │ │ └── MainViewModel.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── dev
│ │ │ └── james
│ │ │ └── firebasemvvm
│ │ │ └── ExampleUnitTest.kt
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── dev
│ │ └── james
│ │ └── firebasemvvm
│ │ └── ExampleInstrumentedTest.kt
├── proguard-rules.pro
├── google-services.json
└── build.gradle
├── .idea
├── .gitignore
├── compiler.xml
├── vcs.xml
├── deploymentTargetDropDown.xml
├── gradle.xml
└── misc.xml
├── home.png
├── signup.png
├── email_sent.jpg
├── reset_password.png
├── home_after_signin.png
├── home_after_signup.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── settings.gradle
├── gradle.properties
├── README.md
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/home.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/home.png
--------------------------------------------------------------------------------
/signup.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/signup.png
--------------------------------------------------------------------------------
/email_sent.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/email_sent.jpg
--------------------------------------------------------------------------------
/reset_password.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/reset_password.png
--------------------------------------------------------------------------------
/home_after_signin.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/home_after_signin.png
--------------------------------------------------------------------------------
/home_after_signup.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/home_after_signup.png
--------------------------------------------------------------------------------
/app/src/main/res/raw/home.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/raw/home.png
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | FirebaseMVVM
3 |
--------------------------------------------------------------------------------
/app/src/main/res/raw/signin.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/raw/signin.png
--------------------------------------------------------------------------------
/app/src/main/res/raw/signup.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/raw/signup.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/raw/email_sent.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/raw/email_sent.jpg
--------------------------------------------------------------------------------
/app/src/main/res/raw/reset_password.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/raw/reset_password.png
--------------------------------------------------------------------------------
/app/src/main/res/raw/home_after_signin.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/raw/home_after_signin.png
--------------------------------------------------------------------------------
/app/src/main/res/raw/home_after_signup.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/raw/home_after_signup.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JayExtra/FirebaseMVVMApp/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/BaseApplication.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm
2 |
3 | import android.app.Application
4 | import dagger.hilt.android.HiltAndroidApp
5 |
6 | @HiltAndroidApp
7 | class BaseApplication : Application()
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Aug 18 16:53:03 EAT 2021
2 | distributionBase=GRADLE_USER_HOME
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
4 | distributionPath=wrapper/dists
5 | zipStorePath=wrapper/dists
6 | zipStoreBase=GRADLE_USER_HOME
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 | local.properties
16 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | dependencyResolutionManagement {
2 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
3 | repositories {
4 | google()
5 | mavenCentral()
6 | jcenter() // Warning: this repository is going to shut down soon
7 | }
8 | }
9 | rootProject.name = "FirebaseMVVM"
10 | include ':app'
11 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_error.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_mail.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_person_grey.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/test/java/com/dev/james/firebasemvvm/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_password.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/deploymentTargetDropDown.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_username.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
19 |
20 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/dev/james/firebasemvvm/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.dev.james.firebasemvvm", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
13 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/repository/firebase/BaseAuthenticator.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm.repository.firebase
2 |
3 | import com.google.firebase.auth.FirebaseUser
4 |
5 | interface BaseAuthenticator {
6 |
7 | //this class will implement all the basic authentication api calls. Using this method of abstraction
8 | //will allow us to implement any type of authentication api may it be
9 | //that you have built your own or Firebase auth sdks. This makes it easy to swap
10 | //in the future because all authentication classes you will use will inherit behaviour from this base class.
11 | //Also in testing it will make it easy to swap your actual sdks with fake ones
12 |
13 | suspend fun signUpWithEmailPassword(email:String , password:String) : FirebaseUser?
14 |
15 | suspend fun signInWithEmailPassword(email: String , password: String):FirebaseUser?
16 |
17 | fun signOut() : FirebaseUser?
18 |
19 | fun getUser() : FirebaseUser?
20 |
21 | suspend fun sendPasswordReset(email :String)
22 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/repository/BaseAuthRepository.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm.repository
2 |
3 | import com.google.firebase.auth.FirebaseUser
4 |
5 | interface BaseAuthRepository {
6 |
7 | //this is an interface that will implement all common authentication
8 | //functions. That is sign in , sign up , logout. Taking this approach will allow us
9 | // to rely on abstractions rather than a concrete authentication repository class. This
10 | //will make it easy for us to test and maintain in that whatever form of repository class
11 | //we will use it won't matter since all will inherit from this class. So swapping
12 | //of repositories will be easy.
13 |
14 | suspend fun signInWithEmailPassword(email:String , password:String): FirebaseUser?
15 |
16 | suspend fun signUpWithEmailPassword(email: String , password: String): FirebaseUser?
17 |
18 | fun signOut() : FirebaseUser?
19 |
20 | fun getCurrentUser() : FirebaseUser?
21 |
22 | suspend fun sendResetPassword(email : String) : Boolean
23 | }
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/repository/AuthRepository.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm.repository
2 |
3 | import com.dev.james.firebasemvvm.repository.firebase.BaseAuthenticator
4 | import com.google.firebase.auth.FirebaseUser
5 | import javax.inject.Inject
6 |
7 | class AuthRepository @Inject constructor(
8 | private val authenticator : BaseAuthenticator
9 | ) : BaseAuthRepository {
10 | override suspend fun signInWithEmailPassword(email: String, password: String): FirebaseUser? {
11 | return authenticator.signInWithEmailPassword(email , password)
12 | }
13 |
14 | override suspend fun signUpWithEmailPassword(email: String, password: String): FirebaseUser? {
15 | return authenticator.signUpWithEmailPassword(email , password)
16 | }
17 |
18 | override fun signOut(): FirebaseUser? {
19 | return authenticator.signOut()
20 | }
21 |
22 | override fun getCurrentUser(): FirebaseUser? {
23 | return authenticator.getUser()
24 | }
25 |
26 | override suspend fun sendResetPassword(email: String): Boolean {
27 | authenticator.sendPasswordReset(email)
28 | return true
29 | }
30 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/repository/firebase/FirebaseAuthenticator.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm.repository.firebase
2 |
3 | import com.google.firebase.auth.FirebaseUser
4 | import com.google.firebase.auth.ktx.auth
5 | import com.google.firebase.ktx.Firebase
6 | import kotlinx.coroutines.tasks.await
7 |
8 | class FirebaseAuthenticator : BaseAuthenticator {
9 | override suspend fun signUpWithEmailPassword(email: String, password: String): FirebaseUser? {
10 | Firebase.auth.createUserWithEmailAndPassword(email,password).await()
11 | return Firebase.auth.currentUser
12 | }
13 |
14 | override suspend fun signInWithEmailPassword(email: String, password: String): FirebaseUser? {
15 | Firebase.auth.signInWithEmailAndPassword(email , password).await()
16 | return Firebase.auth.currentUser
17 | }
18 |
19 | override fun signOut(): FirebaseUser? {
20 | Firebase.auth.signOut()
21 | return Firebase.auth.currentUser
22 | }
23 |
24 | override fun getUser(): FirebaseUser? {
25 | return Firebase.auth.currentUser
26 | }
27 |
28 | override suspend fun sendPasswordReset(email: String) {
29 | Firebase.auth.sendPasswordResetEmail(email).await()
30 | }
31 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/di/AppModule.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm.di
2 |
3 | import com.dev.james.firebasemvvm.repository.AuthRepository
4 | import com.dev.james.firebasemvvm.repository.BaseAuthRepository
5 | import com.dev.james.firebasemvvm.repository.firebase.BaseAuthenticator
6 | import com.dev.james.firebasemvvm.repository.firebase.FirebaseAuthenticator
7 | import dagger.Module
8 | import dagger.Provides
9 | import dagger.hilt.InstallIn
10 | import dagger.hilt.components.SingletonComponent
11 | import javax.inject.Singleton
12 |
13 | @Module
14 | @InstallIn(SingletonComponent::class)
15 | object AppModule {
16 |
17 | /**All of our application dependencies shall be provided here*/
18 |
19 | //this means that anytime we need an authenticator Dagger will provide a Firebase authenticator.
20 | //in future if you want to swap out Firebase authentication for your own custom authenticator
21 | //you will simply come and swap here.
22 | @Singleton
23 | @Provides
24 | fun provideAuthenticator() : BaseAuthenticator{
25 | return FirebaseAuthenticator()
26 | }
27 |
28 | //this just takes the same idea as the authenticator. If we create another repository class
29 | //we can simply just swap here
30 | @Singleton
31 | @Provides
32 | fun provideRepository(
33 | authenticator : BaseAuthenticator
34 | ) : BaseAuthRepository {
35 | return AuthRepository(authenticator)
36 | }
37 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_home.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
21 |
22 |
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import androidx.navigation.NavController
6 | import androidx.navigation.fragment.NavHostFragment
7 | import androidx.navigation.fragment.findNavController
8 | import androidx.navigation.ui.setupActionBarWithNavController
9 | import dagger.hilt.EntryPoint
10 | import dagger.hilt.android.AndroidEntryPoint
11 |
12 | @AndroidEntryPoint
13 | class MainActivity : AppCompatActivity() {
14 | private lateinit var navController: NavController
15 | override fun onCreate(savedInstanceState: Bundle?) {
16 | super.onCreate(savedInstanceState)
17 | setContentView(R.layout.activity_main)
18 |
19 | /**
20 | * This class will mainly hold both the
21 | * sign in and sign up fragments and navigation
22 | * will be handled by the navigation component*/
23 |
24 | //here we will simply setup the nav host fragment which is responsible
25 | //for holding all the other fragments that will be used in the app
26 | //the onSupportNavigationUp creates the back arrow in the toolbar.
27 | val navHostFragment =
28 | supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
29 | navController = navHostFragment.findNavController()
30 |
31 | setupActionBarWithNavController(navController)
32 |
33 | }
34 |
35 | override fun onSupportNavigateUp(): Boolean {
36 | return navController.navigateUp() || super.onSupportNavigateUp()
37 | }
38 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FirebaseMVVMApp
2 | A simple aplication that demonstrates Firebase authentication sdks. Sign up , sign in and sign out functionalities.
3 |
4 | Features:
5 | 1. MVVM design pattern.
6 | 2. Firebase auth sdks.
7 | 3. Hilt dependency injection.
8 | 4. Kotlin coroutines , Flow and channels.
9 | 5. Jetpack Navigation component.
10 | 6. View binding.
11 |
12 | The application contains one main ctivity and three frgments namely sign in , sign up and forgot password fragment.
13 |
14 | Sign in fragment:
15 | Responsible for signing in the user if an account of the user already exists.
16 |
17 | Sign up fragmnet:
18 | For signing up a new user.
19 |
20 | Forgot password fragment:
21 | Used for sending a password reset emil incase a user forgets the password.
22 |
23 | Follow the tutorial in hashnode : https://jaydroid254.hashnode.dev/firebase-authentication-with-mvvm-and-hilt-part-i
24 |
25 | Screenshots:
26 |
27 | 
28 | 
29 | 
30 | 
31 | 
32 | 
33 | 
34 |
35 |
36 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/ui/home/HomeFragment.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm.ui.home
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import android.widget.Toast
8 | import androidx.core.view.isVisible
9 | import androidx.fragment.app.Fragment
10 | import androidx.fragment.app.activityViewModels
11 | import androidx.lifecycle.lifecycleScope
12 | import androidx.navigation.fragment.findNavController
13 | import com.dev.james.firebasemvvm.R
14 | import com.dev.james.firebasemvvm.databinding.FragmentHomeBinding
15 | import com.dev.james.firebasemvvm.viewmodels.MainViewModel
16 | import dagger.hilt.android.AndroidEntryPoint
17 | import kotlinx.coroutines.flow.collect
18 | import kotlinx.coroutines.launch
19 |
20 | @AndroidEntryPoint
21 | class HomeFragment : Fragment(R.layout.fragment_home) {
22 |
23 | private var _binding : FragmentHomeBinding? = null
24 | private val binding get() = _binding
25 | private val viewModel : MainViewModel by activityViewModels()
26 | override fun onCreateView(
27 | inflater: LayoutInflater,
28 | container: ViewGroup?,
29 | savedInstanceState: Bundle?
30 | ): View? {
31 | _binding = FragmentHomeBinding.inflate(inflater, container, false)
32 | getUser()
33 | registerObserver()
34 | listenToChannels()
35 | return binding?.root
36 | }
37 |
38 |
39 |
40 | private fun getUser() {
41 | viewModel.getCurrentUser()
42 | }
43 |
44 | private fun listenToChannels() {
45 | viewLifecycleOwner.lifecycleScope.launch {
46 | viewModel.allEventsFlow.collect { event ->
47 | when(event){
48 | is MainViewModel.AllEvents.Message ->{
49 | Toast.makeText(requireContext(), event.message, Toast.LENGTH_SHORT).show()
50 | }
51 | }
52 | }
53 | }
54 | }
55 |
56 | private fun registerObserver() {
57 | viewModel.currentUser.observe(viewLifecycleOwner,{ user ->
58 | user?.let {
59 | binding?.apply{
60 | welcomeTxt.text = "welcome ${it.email}"
61 | signinButton.text = "sign out"
62 | signinButton.setOnClickListener {
63 | viewModel.signOut()
64 | }
65 | }
66 | }?: binding?.apply {
67 | welcomeTxt.isVisible = false
68 | signinButton.text = "sign in"
69 | signinButton.setOnClickListener {
70 | findNavController().navigate(R.id.action_homeFragment_to_signInFragment)
71 | }
72 | }
73 | })
74 | }
75 |
76 | override fun onDestroy() {
77 | super.onDestroy()
78 | _binding = null
79 | }
80 | }
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/app/src/main/res/navigation/nav_graph.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
18 |
19 |
23 |
29 |
34 |
37 |
38 |
42 |
48 |
54 |
55 |
59 |
64 |
65 |
--------------------------------------------------------------------------------
/app/google-services.json:
--------------------------------------------------------------------------------
1 | {
2 | "project_info": {
3 | "project_number": "864379212186",
4 | "firebase_url": "https://test-application-8db8c.firebaseio.com",
5 | "project_id": "test-application-8db8c",
6 | "storage_bucket": "test-application-8db8c.appspot.com"
7 | },
8 | "client": [
9 | {
10 | "client_info": {
11 | "mobilesdk_app_id": "1:864379212186:android:afaf3e04922b4fb2e4aa23",
12 | "android_client_info": {
13 | "package_name": "com.dev.james.firebasemvvm"
14 | }
15 | },
16 | "oauth_client": [
17 | {
18 | "client_id": "864379212186-dkunfakbt8c63tt6qutusrmnu3jei99f.apps.googleusercontent.com",
19 | "client_type": 3
20 | }
21 | ],
22 | "api_key": [
23 | {
24 | "current_key": "AIzaSyAJseVMGXTqbcJB-_Rltf49l1rr2qyb60o"
25 | }
26 | ],
27 | "services": {
28 | "appinvite_service": {
29 | "other_platform_oauth_client": [
30 | {
31 | "client_id": "864379212186-dkunfakbt8c63tt6qutusrmnu3jei99f.apps.googleusercontent.com",
32 | "client_type": 3
33 | }
34 | ]
35 | }
36 | }
37 | },
38 | {
39 | "client_info": {
40 | "mobilesdk_app_id": "1:864379212186:android:731b16596614d2f9e4aa23",
41 | "android_client_info": {
42 | "package_name": "com.example.mypetstore"
43 | }
44 | },
45 | "oauth_client": [
46 | {
47 | "client_id": "864379212186-dkunfakbt8c63tt6qutusrmnu3jei99f.apps.googleusercontent.com",
48 | "client_type": 3
49 | }
50 | ],
51 | "api_key": [
52 | {
53 | "current_key": "AIzaSyAJseVMGXTqbcJB-_Rltf49l1rr2qyb60o"
54 | }
55 | ],
56 | "services": {
57 | "appinvite_service": {
58 | "other_platform_oauth_client": [
59 | {
60 | "client_id": "864379212186-dkunfakbt8c63tt6qutusrmnu3jei99f.apps.googleusercontent.com",
61 | "client_type": 3
62 | }
63 | ]
64 | }
65 | }
66 | },
67 | {
68 | "client_info": {
69 | "mobilesdk_app_id": "1:864379212186:android:ff84b29d6166576ee4aa23",
70 | "android_client_info": {
71 | "package_name": "com.example.sayari"
72 | }
73 | },
74 | "oauth_client": [
75 | {
76 | "client_id": "864379212186-dkunfakbt8c63tt6qutusrmnu3jei99f.apps.googleusercontent.com",
77 | "client_type": 3
78 | }
79 | ],
80 | "api_key": [
81 | {
82 | "current_key": "AIzaSyAJseVMGXTqbcJB-_Rltf49l1rr2qyb60o"
83 | }
84 | ],
85 | "services": {
86 | "appinvite_service": {
87 | "other_platform_oauth_client": [
88 | {
89 | "client_id": "864379212186-dkunfakbt8c63tt6qutusrmnu3jei99f.apps.googleusercontent.com",
90 | "client_type": 3
91 | }
92 | ]
93 | }
94 | }
95 | }
96 | ],
97 | "configuration_version": "1"
98 | }
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'kotlin-android'
4 | id 'kotlin-kapt'
5 | id 'dagger.hilt.android.plugin'
6 | }
7 |
8 | android {
9 | compileSdk 30
10 |
11 | defaultConfig {
12 | applicationId "com.dev.james.firebasemvvm"
13 | minSdk 24
14 | targetSdk 30
15 | versionCode 1
16 | versionName "1.0"
17 |
18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19 | }
20 |
21 | buildTypes {
22 | release {
23 | minifyEnabled false
24 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
25 | }
26 | }
27 | buildFeatures {
28 | viewBinding = true
29 | }
30 | compileOptions {
31 | sourceCompatibility JavaVersion.VERSION_1_8
32 | targetCompatibility JavaVersion.VERSION_1_8
33 | }
34 | kotlinOptions {
35 | jvmTarget = '1.8'
36 | }
37 | }
38 |
39 | dependencies {
40 |
41 | implementation 'androidx.core:core-ktx:1.6.0'
42 | implementation 'androidx.appcompat:appcompat:1.3.1'
43 | implementation 'com.google.android.material:material:1.4.0'
44 | implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
45 | implementation 'com.google.firebase:firebase-auth-ktx:21.0.1'
46 | testImplementation 'junit:junit:4.+'
47 | androidTestImplementation 'androidx.test.ext:junit:1.1.3'
48 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
49 |
50 | //kotlin coroutines play service
51 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.3"
52 |
53 | // Material Design
54 | implementation 'com.google.android.material:material:1.5.0-alpha01'
55 |
56 | // Architectural Components
57 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
58 |
59 | // Lifecycle
60 | implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
61 | implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
62 | implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
63 |
64 | // Coroutines
65 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
66 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
67 |
68 | // Coroutine Lifecycle Scopes
69 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
70 | implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
71 |
72 | // Navigation Components
73 | implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"
74 | implementation "androidx.navigation:navigation-ui-ktx:2.3.5"
75 |
76 | // Dagger Hilt
77 | implementation "com.google.dagger:hilt-android:2.37"
78 | kapt "com.google.dagger:hilt-compiler:2.37"
79 | kapt "com.google.dagger:hilt-android-compiler:2.37"
80 | kapt "androidx.hilt:hilt-compiler:1.0.0"
81 | implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
82 | }
83 |
84 |
85 | // ADD THIS AT THE BOTTOM
86 | apply plugin: 'com.google.gms.google-services'
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/ui/authentication/ResetPasswordFragment.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm.ui.authentication
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import android.widget.Toast
8 | import androidx.core.view.isInvisible
9 | import androidx.core.view.isVisible
10 | import androidx.fragment.app.Fragment
11 | import androidx.fragment.app.activityViewModels
12 | import androidx.lifecycle.lifecycleScope
13 | import androidx.navigation.fragment.findNavController
14 | import com.dev.james.firebasemvvm.R
15 | import com.dev.james.firebasemvvm.databinding.FragmentPasswordResetBinding
16 | import com.dev.james.firebasemvvm.viewmodels.MainViewModel
17 | import dagger.hilt.android.AndroidEntryPoint
18 | import kotlinx.coroutines.flow.collect
19 | import kotlinx.coroutines.launch
20 |
21 | @AndroidEntryPoint
22 | class ResetPasswordFragment : Fragment(R.layout.fragment_password_reset) {
23 |
24 | private var _binding : FragmentPasswordResetBinding? = null
25 | private val binding get() = _binding
26 | private val viewModel : MainViewModel by activityViewModels()
27 |
28 | override fun onCreateView(
29 | inflater: LayoutInflater,
30 | container: ViewGroup?,
31 | savedInstanceState: Bundle?
32 | ): View? {
33 | _binding = FragmentPasswordResetBinding.inflate(inflater , container , false)
34 | setUpWidgets()
35 | listenToChannels()
36 | return binding?.root
37 | }
38 |
39 | private fun listenToChannels() {
40 | viewLifecycleOwner.lifecycleScope.launch {
41 | viewModel.allEventsFlow.collect { event ->
42 | when(event){
43 | is MainViewModel.AllEvents.Message -> {
44 | Toast.makeText(requireContext(), event.message, Toast.LENGTH_SHORT).show()
45 | findNavController().navigate(R.id.action_resetPasswordFragment_to_signInFragment)
46 | }
47 | is MainViewModel.AllEvents.Error -> {
48 | binding?.apply {
49 | resetPassProgressBar.isInvisible = true
50 | errorText.text = event.error
51 | }
52 | }
53 | is MainViewModel.AllEvents.ErrorCode -> {
54 | if(event.code == 1)
55 | binding?.apply {
56 | userEmailEtvl.error = "email should not be empty!"
57 | resetPassProgressBar.isInvisible = true
58 | }
59 | }
60 | }
61 |
62 | }
63 | }
64 | }
65 |
66 | private fun setUpWidgets() {
67 | binding?.apply {
68 | buttonResendPassword.setOnClickListener {
69 | resetPassProgressBar.isVisible = true
70 | val email = userEmailEtv.text.toString()
71 | viewModel.verifySendPasswordReset(email)
72 | }
73 | }
74 | }
75 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_password_reset.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
25 |
26 |
31 |
32 |
33 |
44 |
45 |
57 |
58 |
70 |
71 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/ui/authentication/SignInFragment.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm.ui.authentication
2 |
3 | import android.graphics.Color.RED
4 | import android.os.Bundle
5 | import android.util.Log
6 | import android.view.LayoutInflater
7 | import android.view.View
8 | import android.view.ViewGroup
9 | import android.widget.Toast
10 | import androidx.core.view.isInvisible
11 | import androidx.core.view.isVisible
12 | import androidx.fragment.app.Fragment
13 | import androidx.fragment.app.activityViewModels
14 | import androidx.lifecycle.lifecycleScope
15 | import androidx.navigation.fragment.findNavController
16 | import com.dev.james.firebasemvvm.R
17 | import com.dev.james.firebasemvvm.databinding.FragmentSigninBinding
18 | import com.dev.james.firebasemvvm.viewmodels.MainViewModel
19 | import dagger.hilt.android.AndroidEntryPoint
20 | import kotlinx.coroutines.flow.collect
21 | import kotlinx.coroutines.launch
22 |
23 | @AndroidEntryPoint
24 | class SignInFragment : Fragment(R.layout.fragment_signin) {
25 | private val viewModel : MainViewModel by activityViewModels()
26 | private var _binding : FragmentSigninBinding? = null
27 | private val binding get() = _binding
28 | private val TAG = "SignInFragment"
29 | override fun onCreateView(
30 | inflater: LayoutInflater,
31 | container: ViewGroup?,
32 | savedInstanceState: Bundle?
33 | ): View? {
34 | _binding = FragmentSigninBinding.inflate(inflater , container , false)
35 | listenToChannels()
36 | registerObservers()
37 | binding?.apply {
38 | signInButton.setOnClickListener {
39 | progressBarSignin.isVisible = true
40 | val email = userEmailEtv.text.toString()
41 | val password = userPasswordEtv.text.toString()
42 | viewModel.signInUser(email, password)
43 | }
44 |
45 | signUpTxt.setOnClickListener {
46 | findNavController().navigate(R.id.action_signInFragment_to_signUpFragment)
47 | }
48 |
49 | forgotPassTxt.setOnClickListener {
50 | findNavController().navigate(R.id.action_signInFragment_to_resetPasswordFragment)
51 | }
52 | }
53 | return binding?.root
54 | }
55 |
56 | private fun registerObservers() {
57 | viewModel.currentUser.observe(viewLifecycleOwner, { user ->
58 | user?.let {
59 | findNavController().navigate(R.id.action_signInFragment_to_homeFragment2)
60 | }
61 | })
62 | }
63 |
64 | private fun listenToChannels() {
65 | viewLifecycleOwner.lifecycleScope.launch {
66 | viewModel.allEventsFlow.collect { event ->
67 | when(event){
68 | is MainViewModel.AllEvents.Error -> {
69 | binding?.apply {
70 | errorTxt.text = event.error
71 | progressBarSignin.isInvisible = true
72 | }
73 | }
74 | is MainViewModel.AllEvents.Message -> {
75 | Toast.makeText(requireContext(), event.message, Toast.LENGTH_SHORT).show()
76 | }
77 | is MainViewModel.AllEvents.ErrorCode -> {
78 | if (event.code == 1)
79 | binding?.apply {
80 | userEmailEtvl.error = "email should not be empty"
81 | progressBarSignin.isInvisible = true
82 | }
83 |
84 |
85 | if(event.code == 2)
86 | binding?.apply {
87 | userPasswordEtvl.error = "password should not be empty"
88 | progressBarSignin.isInvisible = true
89 | }
90 | }
91 |
92 | else ->{
93 | Log.d(TAG, "listenToChannels: No event received so far")
94 | }
95 | }
96 |
97 | }
98 | }
99 | }
100 |
101 | override fun onDestroy() {
102 | super.onDestroy()
103 | _binding = null
104 | }
105 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/ui/authentication/SignUpFragment.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm.ui.authentication
2 |
3 | import android.os.Bundle
4 | import android.util.Log
5 | import android.view.LayoutInflater
6 | import android.view.View
7 | import android.view.ViewGroup
8 | import android.widget.Toast
9 | import androidx.core.view.isInvisible
10 | import androidx.core.view.isVisible
11 | import androidx.fragment.app.Fragment
12 | import androidx.fragment.app.activityViewModels
13 | import androidx.fragment.app.viewModels
14 | import androidx.lifecycle.lifecycleScope
15 | import androidx.navigation.fragment.findNavController
16 | import com.dev.james.firebasemvvm.R
17 | import com.dev.james.firebasemvvm.databinding.FragmentSignupBinding
18 | import com.dev.james.firebasemvvm.viewmodels.MainViewModel
19 | import dagger.hilt.android.AndroidEntryPoint
20 | import kotlinx.coroutines.flow.collect
21 | import kotlinx.coroutines.launch
22 |
23 | @AndroidEntryPoint
24 | class SignUpFragment : Fragment(R.layout.fragment_signup) {
25 | private val viewModel : MainViewModel by activityViewModels()
26 | private var _binding : FragmentSignupBinding? = null
27 | private val binding get() = _binding
28 | private val TAG = "SignUpFragment"
29 |
30 | override fun onCreateView(
31 | inflater: LayoutInflater,
32 | container: ViewGroup?,
33 | savedInstanceState: Bundle?
34 | ): View? {
35 | _binding = FragmentSignupBinding.inflate(inflater , container , false)
36 |
37 | registerObservers()
38 | listenToChannels()
39 | binding?.apply {
40 | signUpButton.setOnClickListener {
41 | progressBarSignup.isVisible = true
42 | val email = userEmailEtv.text.toString()
43 | val password = userPasswordEtv.text.toString()
44 | val confirmPass = confirmPasswordEtv.text.toString()
45 | viewModel.signUpUser(email , password , confirmPass)
46 |
47 |
48 | }
49 |
50 | signInTxt.setOnClickListener {
51 | findNavController().navigate(R.id.action_signUpFragment_to_signInFragment)
52 | }
53 |
54 | }
55 |
56 | return binding?.root
57 | }
58 |
59 | override fun onDestroy() {
60 | super.onDestroy()
61 | _binding = null
62 | }
63 |
64 |
65 | private fun registerObservers() {
66 | viewModel.currentUser.observe(viewLifecycleOwner, { user ->
67 | user?.let {
68 | findNavController().navigate(R.id.action_signUpFragment_to_homeFragment)
69 | }
70 | })
71 | }
72 |
73 | private fun listenToChannels() {
74 | viewLifecycleOwner.lifecycleScope.launch {
75 | viewModel.allEventsFlow.collect { event ->
76 | when(event){
77 | is MainViewModel.AllEvents.Error -> {
78 | binding?.apply {
79 | errorTxt.text = event.error
80 | progressBarSignup.isInvisible = true
81 | }
82 | }
83 | is MainViewModel.AllEvents.Message -> {
84 | Toast.makeText(requireContext(), event.message, Toast.LENGTH_SHORT).show()
85 | }
86 | is MainViewModel.AllEvents.ErrorCode -> {
87 | if (event.code == 1)
88 | binding?.apply {
89 | userEmailEtvl.error = "email should not be empty"
90 | progressBarSignup.isInvisible = true
91 | }
92 |
93 |
94 | if(event.code == 2)
95 | binding?.apply {
96 | userPasswordEtvl.error = "password should not be empty"
97 | progressBarSignup.isInvisible = true
98 | }
99 |
100 | if(event.code == 3)
101 | binding?.apply {
102 | confirmPasswordEtvl.error = "passwords do not match"
103 | progressBarSignup.isInvisible = true
104 | }
105 | }
106 |
107 | else ->{
108 | Log.d(TAG, "listenToChannels: No event received so far")
109 | }
110 | }
111 |
112 | }
113 | }
114 | }
115 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/dev/james/firebasemvvm/viewmodels/MainViewModel.kt:
--------------------------------------------------------------------------------
1 | package com.dev.james.firebasemvvm.viewmodels
2 |
3 | import android.util.Log
4 | import androidx.lifecycle.MutableLiveData
5 | import androidx.lifecycle.ViewModel
6 | import androidx.lifecycle.viewModelScope
7 | import com.dev.james.firebasemvvm.repository.BaseAuthRepository
8 | import com.google.firebase.auth.FirebaseUser
9 | import dagger.hilt.android.lifecycle.HiltViewModel
10 | import kotlinx.coroutines.channels.Channel
11 | import kotlinx.coroutines.flow.receiveAsFlow
12 | import kotlinx.coroutines.launch
13 | import java.lang.Exception
14 | import javax.inject.Inject
15 |
16 | @HiltViewModel
17 | class MainViewModel @Inject constructor(
18 | private val repository : BaseAuthRepository
19 | ) : ViewModel() {
20 |
21 | private val TAG = "MainViewModel"
22 |
23 | /**This is a ViewModel class and is responsible for the logic of all ui.
24 | * It shall be shared with the three fragments.
25 | * Only share ViewModels when the fragments share a feature or functionality */
26 |
27 | //create the auth state livedata object that will be passed to
28 | //the home fragment and shall be used to control the ui i.e show authentication state
29 | //control behaviour of sign in and sign up button
30 | private val _firebaseUser = MutableLiveData()
31 | val currentUser get() = _firebaseUser
32 |
33 | //create our channels that will be used to pass messages to the main ui
34 | //create event channel
35 | private val eventsChannel = Channel()
36 | //the messages passed to the channel shall be received as a Flowable
37 | //in the ui
38 | val allEventsFlow = eventsChannel.receiveAsFlow()
39 |
40 |
41 | //validate all fields first before performing any sign in operations
42 | fun signInUser(email: String , password: String) = viewModelScope.launch{
43 | when {
44 | email.isEmpty() -> {
45 | eventsChannel.send(AllEvents.ErrorCode(1))
46 | }
47 | password.isEmpty() -> {
48 | eventsChannel.send(AllEvents.ErrorCode(2))
49 | }
50 | else -> {
51 | actualSignInUser(email , password)
52 | }
53 | }
54 | }
55 |
56 | //validate all fields before performing any sign up operations
57 | fun signUpUser(email : String , password: String , confirmPass : String)= viewModelScope.launch {
58 | when{
59 | email.isEmpty() -> {
60 | eventsChannel.send(AllEvents.ErrorCode(1))
61 | }
62 | password.isEmpty() -> {
63 | eventsChannel.send(AllEvents.ErrorCode(2))
64 | }
65 | password != confirmPass ->{
66 | eventsChannel.send(AllEvents.ErrorCode(3))
67 | }
68 | else -> {
69 | actualSignUpUser(email, password)
70 | }
71 | }
72 | }
73 |
74 |
75 | private fun actualSignInUser(email:String, password: String) = viewModelScope.launch {
76 | try {
77 | val user = repository.signInWithEmailPassword(email, password)
78 | user?.let {
79 | _firebaseUser.postValue(it)
80 | eventsChannel.send(AllEvents.Message("login success"))
81 | }
82 | }catch(e:Exception){
83 | val error = e.toString().split(":").toTypedArray()
84 | Log.d(TAG, "signInUser: ${error[1]}")
85 | eventsChannel.send(AllEvents.Error(error[1]))
86 | }
87 | }
88 |
89 | private fun actualSignUpUser(email:String , password: String) = viewModelScope.launch {
90 | try {
91 | val user = repository.signUpWithEmailPassword(email, password)
92 | user?.let {
93 | _firebaseUser.postValue(it)
94 | eventsChannel.send(AllEvents.Message("sign up success"))
95 | }
96 | }catch(e:Exception){
97 | val error = e.toString().split(":").toTypedArray()
98 | Log.d(TAG, "signInUser: ${error[1]}")
99 | eventsChannel.send(AllEvents.Error(error[1]))
100 | }
101 | }
102 |
103 | fun signOut() = viewModelScope.launch {
104 | try {
105 | val user = repository.signOut()
106 | user?.let {
107 | eventsChannel.send(AllEvents.Message("logout failure"))
108 | }?: eventsChannel.send(AllEvents.Message("sign out successful"))
109 |
110 | getCurrentUser()
111 |
112 | }catch(e:Exception){
113 | val error = e.toString().split(":").toTypedArray()
114 | Log.d(TAG, "signInUser: ${error[1]}")
115 | eventsChannel.send(AllEvents.Error(error[1]))
116 | }
117 | }
118 |
119 | fun getCurrentUser() = viewModelScope.launch {
120 | val user = repository.getCurrentUser()
121 | _firebaseUser.postValue(user)
122 | }
123 |
124 | fun verifySendPasswordReset(email: String){
125 | if(email.isEmpty()){
126 | viewModelScope.launch {
127 | eventsChannel.send(AllEvents.ErrorCode(1))
128 | }
129 | }else{
130 | sendPasswordResetEmail(email)
131 | }
132 |
133 | }
134 |
135 | private fun sendPasswordResetEmail(email: String) = viewModelScope.launch {
136 | try {
137 | val result = repository.sendResetPassword(email)
138 | if (result){
139 | eventsChannel.send(AllEvents.Message("reset email sent"))
140 | }else{
141 | eventsChannel.send(AllEvents.Error("could not send password reset"))
142 | }
143 | }catch (e : Exception){
144 | val error = e.toString().split(":").toTypedArray()
145 | Log.d(TAG, "signInUser: ${error[1]}")
146 | eventsChannel.send(AllEvents.Error(error[1]))
147 | }
148 | }
149 |
150 |
151 |
152 | sealed class AllEvents {
153 | data class Message(val message : String) : AllEvents()
154 | data class ErrorCode(val code : Int):AllEvents()
155 | data class Error(val error : String) : AllEvents()
156 | }
157 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_signin.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
22 |
23 |
34 |
35 |
51 |
52 |
57 |
58 |
59 |
76 |
77 |
82 |
83 |
84 |
95 |
96 |
109 |
110 |
123 |
124 |
134 |
135 |
149 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_signup.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
22 |
23 |
34 |
35 |
51 |
52 |
57 |
58 |
59 |
76 |
77 |
82 |
83 |
84 |
101 |
102 |
107 |
108 |
109 |
120 |
121 |
134 |
135 |
145 |
146 |
160 |
--------------------------------------------------------------------------------