-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
265 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
24 changes: 24 additions & 0 deletions
24
app/src/main/java/com/fankes/miui/notify/data/LoginDataSource.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,24 @@ | ||
package com.fankes.miui.notify.data | ||
|
||
import com.fankes.miui.notify.data.model.LoggedInUser | ||
import java.io.IOException | ||
|
||
/** | ||
* Class that handles authentication w/ login credentials and retrieves user information. | ||
*/ | ||
class LoginDataSource { | ||
|
||
fun login(username: String, password: String): Result<LoggedInUser> { | ||
try { | ||
// TODO: handle loggedInUser authentication | ||
val fakeUser = LoggedInUser(java.util.UUID.randomUUID().toString(), "Jane Doe") | ||
return Result.Success(fakeUser) | ||
} catch (e: Throwable) { | ||
return Result.Error(IOException("Error logging in", e)) | ||
} | ||
} | ||
|
||
fun logout() { | ||
// TODO: revoke authentication | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/main/java/com/fankes/miui/notify/data/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,46 @@ | ||
package com.fankes.miui.notify.data | ||
|
||
import com.fankes.miui.notify.data.model.LoggedInUser | ||
|
||
/** | ||
* Class that requests authentication and user information from the remote data source and | ||
* maintains an in-memory cache of login status and user credentials information. | ||
*/ | ||
|
||
class LoginRepository(val dataSource: LoginDataSource) { | ||
|
||
// in-memory cache of the loggedInUser object | ||
var user: LoggedInUser? = null | ||
private set | ||
|
||
val isLoggedIn: Boolean | ||
get() = user != null | ||
|
||
init { | ||
// If user credentials will be cached in local storage, it is recommended it be encrypted | ||
// @see https://developer.android.com/training/articles/keystore | ||
user = null | ||
} | ||
|
||
fun logout() { | ||
user = null | ||
dataSource.logout() | ||
} | ||
|
||
fun login(username: String, password: String): Result<LoggedInUser> { | ||
// handle login | ||
val result = dataSource.login(username, password) | ||
|
||
if (result is Result.Success) { | ||
setLoggedInUser(result.data) | ||
} | ||
|
||
return result | ||
} | ||
|
||
private fun setLoggedInUser(loggedInUser: LoggedInUser) { | ||
this.user = loggedInUser | ||
// If user credentials will be cached in local storage, it is recommended it be encrypted | ||
// @see https://developer.android.com/training/articles/keystore | ||
} | ||
} |
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,18 @@ | ||
package com.fankes.miui.notify.data | ||
|
||
/** | ||
* A generic class that holds a value with its loading status. | ||
* @param <T> | ||
*/ | ||
sealed class Result<out T : Any> { | ||
|
||
data class Success<out T : Any>(val data: T) : Result<T>() | ||
data class Error(val exception: Exception) : Result<Nothing>() | ||
|
||
override fun toString(): String { | ||
return when (this) { | ||
is Success<*> -> "Success[data=$data]" | ||
is Error -> "Error[exception=$exception]" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/fankes/miui/notify/data/model/LoggedInUser.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,9 @@ | ||
package com.fankes.miui.notify.data.model | ||
|
||
/** | ||
* Data class that captures user information for logged in users retrieved from LoginRepository | ||
*/ | ||
data class LoggedInUser( | ||
val userId: String, | ||
val displayName: String | ||
) |
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
Oops, something went wrong.