-
Notifications
You must be signed in to change notification settings - Fork 26
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
60 changed files
with
1,422 additions
and
1,061 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
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
45 changes: 45 additions & 0 deletions
45
...o-autotrack/src/main/java/com/gio/test/three/autotrack/activity/data/LoginDataSource.java
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,45 @@ | ||
/* | ||
* Copyright (C) 2020 Beijing Yishu Technology Co., Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.gio.test.three.autotrack.activity.data; | ||
|
||
import com.gio.test.three.autotrack.activity.data.model.LoggedInUser; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Class that handles authentication w/ login credentials and retrieves user information. | ||
*/ | ||
public class LoginDataSource { | ||
|
||
public Result<LoggedInUser> login(String username, String password) { | ||
|
||
try { | ||
// TODO: handle loggedInUser authentication | ||
LoggedInUser fakeUser = | ||
new LoggedInUser( | ||
java.util.UUID.randomUUID().toString(), | ||
"Jane Doe"); | ||
return new Result.Success<>(fakeUser); | ||
} catch (Exception e) { | ||
return new Result.Error(new IOException("Error logging in", e)); | ||
} | ||
} | ||
|
||
public void logout() { | ||
// TODO: revoke authentication | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...o-autotrack/src/main/java/com/gio/test/three/autotrack/activity/data/LoginRepository.java
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,70 @@ | ||
/* | ||
* Copyright (C) 2020 Beijing Yishu Technology Co., Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.gio.test.three.autotrack.activity.data; | ||
|
||
import com.gio.test.three.autotrack.activity.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. | ||
*/ | ||
public class LoginRepository { | ||
|
||
private static volatile LoginRepository sInstance; | ||
|
||
private LoginDataSource mDataSource; | ||
|
||
// If user credentials will be cached in local storage, it is recommended it be encrypted | ||
// @see https://developer.android.com/training/articles/keystore | ||
private LoggedInUser mUser = null; | ||
|
||
// private constructor : singleton access | ||
private LoginRepository(LoginDataSource dataSource) { | ||
this.mDataSource = dataSource; | ||
} | ||
|
||
public static LoginRepository getInstance(LoginDataSource dataSource) { | ||
if (sInstance == null) { | ||
sInstance = new LoginRepository(dataSource); | ||
} | ||
return sInstance; | ||
} | ||
|
||
public boolean isLoggedIn() { | ||
return mUser != null; | ||
} | ||
|
||
public void logout() { | ||
mUser = null; | ||
mDataSource.logout(); | ||
} | ||
|
||
private void setLoggedInUser(LoggedInUser user) { | ||
this.mUser = user; | ||
// If user credentials will be cached in local storage, it is recommended it be encrypted | ||
// @see https://developer.android.com/training/articles/keystore | ||
} | ||
|
||
public Result<LoggedInUser> login(String username, String password) { | ||
// handle login | ||
Result<LoggedInUser> result = mDataSource.login(username, password); | ||
if (result instanceof Result.Success) { | ||
setLoggedInUser(((Result.Success<LoggedInUser>) result).getData()); | ||
} | ||
return result; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
demos/demo-autotrack/src/main/java/com/gio/test/three/autotrack/activity/data/Result.java
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,64 @@ | ||
/* | ||
* Copyright (C) 2020 Beijing Yishu Technology Co., Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.gio.test.three.autotrack.activity.data; | ||
|
||
/** | ||
* A generic class that holds a result success w/ data or an error exception. | ||
*/ | ||
public class Result<T> { | ||
// hide the private constructor to limit subclass types (Success, Error) | ||
private Result() { | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (this instanceof Result.Success) { | ||
Result.Success success = (Result.Success) this; | ||
return "Success[data=" + success.getData().toString() + "]"; | ||
} else if (this instanceof Result.Error) { | ||
Result.Error error = (Result.Error) this; | ||
return "Error[exception=" + error.getError().toString() + "]"; | ||
} | ||
return ""; | ||
} | ||
|
||
// Success sub-class | ||
public final static class Success<T> extends Result { | ||
private T mData; | ||
|
||
public Success(T data) { | ||
this.mData = data; | ||
} | ||
|
||
public T getData() { | ||
return this.mData; | ||
} | ||
} | ||
|
||
// Error sub-class | ||
public final static class Error extends Result { | ||
private Exception mError; | ||
|
||
public Error(Exception error) { | ||
this.mError = error; | ||
} | ||
|
||
public Exception getError() { | ||
return this.mError; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...utotrack/src/main/java/com/gio/test/three/autotrack/activity/data/model/LoggedInUser.java
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 @@ | ||
/* | ||
* Copyright (C) 2020 Beijing Yishu Technology Co., Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.gio.test.three.autotrack.activity.data.model; | ||
|
||
/** | ||
* Data class that captures user information for logged in users retrieved from LoginRepository | ||
*/ | ||
public class LoggedInUser { | ||
|
||
private String mUserId; | ||
private String mDisplayName; | ||
|
||
public LoggedInUser(String userId, String displayName) { | ||
this.mUserId = userId; | ||
this.mDisplayName = displayName; | ||
} | ||
|
||
public String getUserId() { | ||
return mUserId; | ||
} | ||
|
||
public String getDisplayName() { | ||
return mDisplayName; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...otrack/src/main/java/com/gio/test/three/autotrack/activity/ui/login/LoggedInUserView.java
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,33 @@ | ||
/* | ||
* Copyright (C) 2020 Beijing Yishu Technology Co., Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.gio.test.three.autotrack.activity.ui.login; | ||
|
||
/** | ||
* Class exposing authenticated user details to the UI. | ||
*/ | ||
class LoggedInUserView { | ||
private String mDisplayName; | ||
//... other data fields that may be accessible to the UI | ||
|
||
LoggedInUserView(String displayName) { | ||
this.mDisplayName = displayName; | ||
} | ||
|
||
String getDisplayName() { | ||
return mDisplayName; | ||
} | ||
} |
Oops, something went wrong.