Administrator
2021-11-03 de74e5ec3fbdab065e8b91240fa1944c4b3440c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.duqing.missions.ui.login.data;
 
 
import com.duqing.missions.ui.login.data.model.LoggedInUser;
 
import io.reactivex.Observable;
 
/**
 * 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 instance;
 
    private LoginDataSource dataSource;
 
    // 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 user = null;
 
    // private constructor : singleton access
    private LoginRepository(LoginDataSource dataSource) {
        this.dataSource = dataSource;
    }
 
    public static LoginRepository getInstance(LoginDataSource dataSource) {
        if (instance == null) {
            instance = new LoginRepository(dataSource);
        }
        return instance;
    }
 
    public boolean isLoggedIn() {
        return user != null;
    }
 
    public void logout() {
        user = null;
        dataSource.logout();
    }
 
    private void setLoggedInUser(LoggedInUser user) {
        this.user = 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 Observable<LoggedInUser> login(String username, String password) {
        // handle login
        return dataSource.login(username, password);
    }
}