Skip to content

Commit 7c4f5cf

Browse files
codedsuniamareebjamal
authored andcommitted
feat: convert accounts mvp to mvvm architecture (#2890)
1 parent db79b39 commit 7c4f5cf

10 files changed

+174
-441
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ dependencies {
162162
//exoplayer
163163
implementation 'com.google.android.exoplayer:exoplayer:r1.5.7'
164164

165+
implementation "androidx.lifecycle:lifecycle-extensions:$rootProject.lifecycleVersion"
166+
165167
//utils
166168
implementation 'com.github.deano2390:MaterialShowcaseView:1.2.0@aar'
167169
implementation "com.jakewharton:butterknife:$rootProject.butterknifeVersion"

app/src/main/java/org/fossasia/phimpme/accounts/AccountActivity.java

Lines changed: 55 additions & 253 deletions
Large diffs are not rendered by default.

app/src/main/java/org/fossasia/phimpme/accounts/AccountContract.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

app/src/main/java/org/fossasia/phimpme/accounts/AccountPresenter.java

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.fossasia.phimpme.accounts;
2+
3+
import io.realm.Realm;
4+
import io.realm.RealmQuery;
5+
import org.fossasia.phimpme.data.local.AccountDatabase;
6+
import org.fossasia.phimpme.data.local.DatabaseHelper;
7+
8+
/** Created by @codedsun on 09/Oct/2019 */
9+
class AccountRepository {
10+
11+
private Realm realm = Realm.getDefaultInstance();
12+
private DatabaseHelper databaseHelper = new DatabaseHelper(realm);
13+
14+
// Fetches the details of all accounts
15+
RealmQuery<AccountDatabase> fetchAllAccounts() {
16+
return databaseHelper.fetchAccountDetails();
17+
}
18+
19+
// saves username, password and serverUrl for an account in database
20+
void saveUsernamePasswordServerUrlForAccount(
21+
AccountDatabase.AccountName accountName, String serverUrl, String username, String password) {
22+
realm.beginTransaction();
23+
AccountDatabase account = realm.createObject(AccountDatabase.class, accountName.toString());
24+
account.setServerUrl(serverUrl);
25+
account.setUsername(username);
26+
account.setPassword(password);
27+
realm.commitTransaction();
28+
}
29+
30+
// saves username and token for an account in database
31+
void saveUsernameAndToken(
32+
AccountDatabase.AccountName accountName, String username, String token) {
33+
realm.beginTransaction();
34+
AccountDatabase account = realm.createObject(AccountDatabase.class, accountName.toString());
35+
account.setUsername(username);
36+
account.setToken(token);
37+
realm.commitTransaction();
38+
}
39+
40+
// deletes an account from database
41+
void deleteAccount(String accountName) {
42+
databaseHelper.deleteSignedOutAccount(accountName);
43+
}
44+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.fossasia.phimpme.accounts;
2+
3+
import androidx.lifecycle.MutableLiveData;
4+
import androidx.lifecycle.ViewModel;
5+
import io.realm.RealmQuery;
6+
import org.fossasia.phimpme.data.local.AccountDatabase;
7+
8+
/** Created by @codedsun on 09/Oct/2019 */
9+
public class AccountViewModel extends ViewModel {
10+
11+
final int NEXTCLOUD_REQUEST_CODE = 3;
12+
final int OWNCLOUD_REQUEST_CODE = 9;
13+
final int RESULT_OK = 1;
14+
15+
private AccountRepository accountRepository = new AccountRepository();
16+
MutableLiveData<Boolean> error = new MutableLiveData<>();
17+
MutableLiveData<RealmQuery<AccountDatabase>> accountDetails = new MutableLiveData<>();
18+
19+
public AccountViewModel() {}
20+
21+
// Used to fetch all the current logged in accounts
22+
void fetchAccountDetails() {
23+
RealmQuery<AccountDatabase> accountDetails = accountRepository.fetchAllAccounts();
24+
if (accountDetails.findAll().size() > 0) {
25+
this.accountDetails.postValue(accountDetails);
26+
} else {
27+
error.postValue(true);
28+
}
29+
}
30+
31+
// used to save nextcloud or owncloud details
32+
void saveOwnCloudOrNextCloudToken(
33+
int requestCode, String serverUrl, String userName, String password) {
34+
switch (requestCode) {
35+
case NEXTCLOUD_REQUEST_CODE:
36+
accountRepository.saveUsernamePasswordServerUrlForAccount(
37+
AccountDatabase.AccountName.NEXTCLOUD, serverUrl, userName, password);
38+
break;
39+
40+
case OWNCLOUD_REQUEST_CODE:
41+
accountRepository.saveUsernamePasswordServerUrlForAccount(
42+
AccountDatabase.AccountName.OWNCLOUD, serverUrl, userName, password);
43+
break;
44+
}
45+
}
46+
47+
// to save dropbox details
48+
void saveDropboxToken(String token) {
49+
accountRepository.saveUsernameAndToken(
50+
AccountDatabase.AccountName.DROPBOX, AccountDatabase.AccountName.DROPBOX.toString(), token);
51+
}
52+
53+
// to save box details
54+
void saveBoxToken(String username, String token) {
55+
accountRepository.saveUsernameAndToken(AccountDatabase.AccountName.BOX, username, token);
56+
}
57+
58+
// to save pintrest details
59+
void savePinterestToken(String username) {
60+
accountRepository.saveUsernameAndToken(AccountDatabase.AccountName.PINTEREST, username, "");
61+
}
62+
63+
// to delete a specific account from database
64+
void deleteAccountFromDatabase(String accountName) {
65+
accountRepository.deleteAccount(accountName);
66+
}
67+
68+
// to save imgur account details
69+
void saveImgurAccount(String username, String token) {
70+
accountRepository.saveUsernameAndToken(AccountDatabase.AccountName.IMGUR, username, token);
71+
}
72+
}

app/src/main/java/org/fossasia/phimpme/base/BasePresenter.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

app/src/main/java/org/fossasia/phimpme/base/MvpView.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/src/main/java/org/fossasia/phimpme/base/Presenter.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ ext {
5252
stethoVersion = "1.5.0"
5353
showcaseVersion = "5.4.3"
5454
nextCloudVersion = "1.5.0"
55+
lifecycleVersion = "2.1.0"
5556
glideVersion = "4.10.0"
5657
}
5758

0 commit comments

Comments
 (0)