-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Firebase SiginIn 작업
- Loading branch information
Showing
9 changed files
with
248 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/main/java/com/moyerun/moyeorun_android/common/EventLiveData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.moyerun.moyeorun_android.common | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.Observer | ||
|
||
typealias EventLiveData<T> = LiveData<Event<T>> | ||
|
||
class MutableEventLiveData<T> : MutableLiveData<Event<T>>() { | ||
|
||
var event: T? | ||
@Deprecated("getter is NOT supported!", level = DeprecationLevel.ERROR) | ||
get() = throw UnsupportedOperationException() | ||
set(value) { | ||
if (value != null) { | ||
setValue(Event(value)) | ||
} | ||
} | ||
|
||
fun postEvent(value: T?) { | ||
if (value != null) { | ||
postValue(Event(value)) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Used as a wrapper for data that is exposed via a LiveData that represents an event. | ||
*/ | ||
open class Event<out T>(private val content: T) { | ||
|
||
var hasBeenHandled = false | ||
private set // Allow external read but not write | ||
|
||
/** | ||
* Returns the content and prevents its use again. | ||
*/ | ||
fun getContentIfNotHandled(): T? { | ||
return if (hasBeenHandled) { | ||
null | ||
} else { | ||
hasBeenHandled = true | ||
content | ||
} | ||
} | ||
|
||
/** | ||
* Returns the content, even if it's already been handled. | ||
*/ | ||
fun peekContent(): T = content | ||
} | ||
|
||
/** | ||
* An [Observer] for [Event]s, simplifying the pattern of checking if the [Event]'s content has | ||
* already been handled. | ||
* | ||
* [onEventUnhandledContent] is *only* called if the [Event]'s contents has not been handled. | ||
*/ | ||
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> { | ||
override fun onChanged(event: Event<T>?) { | ||
event?.getContentIfNotHandled()?.let { value -> | ||
onEventUnhandledContent(value) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
app/src/main/java/com/moyerun/moyeorun_android/login/LoginEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.moyerun.moyeorun_android.login | ||
|
||
enum class LoginEvent { | ||
Success, NewUser, Error | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/moyerun/moyeorun_android/login/LoginModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.moyerun.moyeorun_android.login | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.components.ViewModelComponent | ||
|
||
@Module | ||
@InstallIn(ViewModelComponent::class) | ||
class LoginModule { | ||
|
||
@Provides | ||
fun providesLoginRepository(): LoginRepository = LoginRepository() | ||
} |
59 changes: 59 additions & 0 deletions
59
app/src/main/java/com/moyerun/moyeorun_android/login/LoginRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.moyerun.moyeorun_android.login | ||
|
||
import com.google.firebase.auth.FirebaseUser | ||
import com.google.firebase.auth.GoogleAuthProvider | ||
import com.google.firebase.auth.ktx.auth | ||
import com.google.firebase.ktx.Firebase | ||
import kotlinx.coroutines.* | ||
import java.lang.IllegalStateException | ||
import kotlin.coroutines.resume | ||
|
||
class LoginRepository( | ||
private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO | ||
) { | ||
|
||
// Todo: 모여런 서버 붙일 때 반환 타입 조정 | ||
suspend fun signIn(idToken: String): ApiResponse<Unit> { | ||
val firebaseSignInResult = withContext(coroutineDispatcher) { signInWithFirebaseCredential(idToken) } | ||
if (firebaseSignInResult is ApiResponse.Failure) { | ||
return firebaseSignInResult | ||
} | ||
val moyeorunSignInResult = withContext(coroutineDispatcher) { signInWithMoyeoRun(idToken) } | ||
if (firebaseSignInResult is ApiResponse.Failure) { | ||
Firebase.auth.signOut() | ||
} | ||
return withContext(coroutineDispatcher) { signInWithMoyeoRun(idToken) } | ||
} | ||
|
||
private suspend fun signInWithFirebaseCredential(idToken: String): ApiResponse<FirebaseUser> { | ||
val firebaseAuth = Firebase.auth | ||
return suspendCancellableCoroutine { | ||
val firebaseCredential = GoogleAuthProvider.getCredential(idToken, null) | ||
firebaseAuth.signInWithCredential(firebaseCredential) | ||
.addOnCompleteListener { task -> | ||
val firebaseUser = firebaseAuth.currentUser | ||
if (firebaseUser != null) { | ||
it.resume(ApiResponse.Success(firebaseUser)) | ||
} else { | ||
it.resume( | ||
ApiResponse.Failure( | ||
task.exception | ||
?: IllegalStateException("FirebaseAuth Failure: FirebaseUser and Task.exception is null") | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun signInWithMoyeoRun(idToken: String): ApiResponse<Unit> { | ||
//Todo: 모여런 서버 signIn | ||
return ApiResponse.Success(Unit) | ||
} | ||
|
||
// Todo: 임시. 네트워크 베이스 코드가 Rebase 되면 대체할 것 | ||
sealed class ApiResponse<out T> { | ||
data class Success<T>(val data: T) : ApiResponse<T>() | ||
data class Failure(val error: Throwable) : ApiResponse<Nothing>() | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/moyerun/moyeorun_android/login/LoginViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.moyerun.moyeorun_android.login | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.moyerun.moyeorun_android.common.EventLiveData | ||
import com.moyerun.moyeorun_android.common.MutableEventLiveData | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class LoginViewModel @Inject constructor( | ||
private val loginRepository: LoginRepository | ||
) : ViewModel() { | ||
|
||
private val _isLoading = MutableStateFlow(false) | ||
val isLoading: StateFlow<Boolean> | ||
get() = _isLoading | ||
|
||
private val _loginEvent = MutableEventLiveData<LoginEvent>() | ||
val loginEvent: EventLiveData<LoginEvent> | ||
get() = _loginEvent | ||
|
||
fun signIn(idToken: String) { | ||
viewModelScope.launch { | ||
_isLoading.value = true | ||
val result = loginRepository.signIn(idToken) | ||
if (result is LoginRepository.ApiResponse.Success) { | ||
// Todo: Firebase crashlytics userId 세팅 | ||
_loginEvent.event = LoginEvent.Success | ||
} else { | ||
_loginEvent.event = LoginEvent.Error | ||
} | ||
_isLoading.value = false | ||
} | ||
} | ||
} |