Skip to content

Commit

Permalink
#230 implemented SyncManager.syncEverything()
Browse files Browse the repository at this point in the history
  • Loading branch information
desperateCoder committed Dec 27, 2019
1 parent 2956f0f commit b47340d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 46 deletions.
41 changes: 25 additions & 16 deletions app/src/main/java/it/niedermann/nextcloud/deck/api/ApiProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import android.content.Context;

import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.api.NextcloudAPI;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.exceptions.SSOException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;

Expand All @@ -25,27 +25,36 @@ public class ApiProvider {
private NextcloudServerAPI nextcloudAPI;
private Context context;
private SingleSignOnAccount ssoAccount;
private String ssoAccountName;

public ApiProvider(Context context) {
this(context, null);
}

public ApiProvider(Context context, String ssoAccountName) {
this.context = context;
this.ssoAccountName = ssoAccountName;
setAccount();
}

public void initSsoApi(final NextcloudAPI.ApiConnectedListener callback) {
NextcloudAPI nextcloudAPI = new NextcloudAPI(context, ssoAccount, GsonConfig.getGson(), callback);
deckAPI = new NextcloudRetrofitApiBuilder(nextcloudAPI, DECK_API_ENDPOINT).create(DeckAPI.class);
this.nextcloudAPI = new NextcloudRetrofitApiBuilder(nextcloudAPI, NC_API_ENDPOINT).create(NextcloudServerAPI.class);
}

private void setAccount() {
try {
setAccount();
NextcloudAPI nextcloudAPI = new NextcloudAPI(context, ssoAccount, GsonConfig.getGson(), callback);
deckAPI = new NextcloudRetrofitApiBuilder(nextcloudAPI, DECK_API_ENDPOINT).create(DeckAPI.class);
this.nextcloudAPI = new NextcloudRetrofitApiBuilder(nextcloudAPI, NC_API_ENDPOINT).create(NextcloudServerAPI.class);
} catch (SSOException e) {
if (ssoAccountName == null) {
this.ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
} else {
this.ssoAccount = AccountImporter.getSingleSignOnAccount(context, ssoAccountName);
}
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
DeckLog.logError(e);
callback.onError(e);
}
}

private void setAccount() throws NextcloudFilesAppAccountNotFoundException, NoCurrentAccountSelectedException {
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
}

public DeckAPI getDeckAPI() {
return deckAPI;
}
Expand All @@ -54,18 +63,18 @@ public NextcloudServerAPI getNextcloudAPI() {
return nextcloudAPI;
}

public String getServerUrl() throws NextcloudFilesAppAccountNotFoundException, NoCurrentAccountSelectedException {
if (ssoAccount==null){
public String getServerUrl(){
if (ssoAccount == null) {
setAccount();
}
return ssoAccount.url;
}

public String getApiPath(){
public String getApiPath() {
return DECK_API_ENDPOINT;
}

public String getApiUrl() throws NextcloudFilesAppAccountNotFoundException, NoCurrentAccountSelectedException {
return getServerUrl()+getApiPath();
public String getApiUrl() {
return getServerUrl() + getApiPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.api.IResponseCallback;
Expand Down Expand Up @@ -52,37 +53,60 @@

public class SyncManager {


private DataBaseAdapter dataBaseAdapter;
private ServerAdapter serverAdapter;

public SyncManager(Activity sourceActivity) {
this(sourceActivity, sourceActivity);
}

public SyncManager(Context context, @Nullable Activity sourceActivity) {
this(context, sourceActivity, null);
}
public SyncManager(Context context, @Nullable Activity sourceActivity, String ssoAccountName) {
if (context == null) {
throw new IllegalArgumentException("Provide a valid context.");
}
Context applicationContext = context.getApplicationContext();
LastSyncUtil.init(applicationContext);
dataBaseAdapter = new DataBaseAdapter(applicationContext);
this.serverAdapter = new ServerAdapter(applicationContext, sourceActivity);
}

public SyncManager(Activity sourceActivity) {
this(sourceActivity, sourceActivity);
this.serverAdapter = new ServerAdapter(applicationContext, sourceActivity, ssoAccountName);
}

private void doAsync(Runnable r) {
new Thread(r).start();
}

boolean synchronizeEverything() {
// TODO do some magic here
try {
Thread.sleep(2000);
return true;
} catch (InterruptedException e) {
e.printStackTrace();
public boolean synchronizeEverything() {
List<Account> accounts = dataBaseAdapter.getAllAccountsDirectly();
if (accounts.size() > 0){
final BooleanResultHolder success = new BooleanResultHolder();
CountDownLatch latch = new CountDownLatch(accounts.size());
try {
for (Account account : accounts) {
new SyncManager(dataBaseAdapter.getContext(), null, account.getName()).synchronize(new IResponseCallback<Boolean>(account) {
@Override
public void onResponse(Boolean response) {
success.result = success.result && response.booleanValue() ;
latch.countDown();
}

@Override
public void onError(Throwable throwable) {
success.result = false;
super.onError(throwable);
latch.countDown();
}
});
}
latch.await();
return success.result;
} catch (InterruptedException e) {
DeckLog.logError(e);
return false;
}
}
return false;
return true;
}

public void synchronize(IResponseCallback<Boolean> responseCallback) {
Expand Down Expand Up @@ -949,4 +973,9 @@ public void onResponse(Attachment response) {
});
return liveData;
}


private class BooleanResultHolder {
public boolean result = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

import androidx.annotation.Nullable;

import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -64,23 +61,26 @@ public class ServerAdapter {
private SharedPreferences lastSyncPref;

public ServerAdapter(Context applicationContext, @Nullable Activity sourceActivity) {
this(applicationContext, sourceActivity, null);
}
public ServerAdapter(Context applicationContext, @Nullable Activity sourceActivity, String ssoAccountName) {
this.applicationContext = applicationContext;
this.sourceActivity = sourceActivity;
prefKeyWifiOnly = applicationContext.getResources().getString(R.string.pref_key_wifi_only);
provider = new ApiProvider(applicationContext);
provider = new ApiProvider(applicationContext, ssoAccountName);
lastSyncPref = applicationContext.getSharedPreferences(
applicationContext.getString(R.string.shared_preference_last_sync), Context.MODE_PRIVATE);
}

public String getServerUrl() throws NextcloudFilesAppAccountNotFoundException, NoCurrentAccountSelectedException {
public String getServerUrl() {
return provider.getServerUrl();
}

public String getApiPath() {
return provider.getApiPath();
}

public String getApiUrl() throws NextcloudFilesAppAccountNotFoundException, NoCurrentAccountSelectedException {
public String getApiUrl() {
return provider.getApiUrl();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,19 +290,19 @@ public void updateAccount(Account account) {
}

public LiveData<Account> readAccount(long id) {
return LiveDataHelper.onlyIfChanged(db.getAccountDao().selectById(id));
return LiveDataHelper.onlyIfChanged(db.getAccountDao().getAccountById(id));
}

public LiveData<Account> readAccount(String name) {
return LiveDataHelper.onlyIfChanged(db.getAccountDao().selectById(name));
return LiveDataHelper.onlyIfChanged(db.getAccountDao().getAccountByName(name));
}

public Account readAccountDirectly(long id) {
return db.getAccountDao().getAccountByIdDirectly(id);
}

public LiveData<List<Account>> readAccounts() {
return LiveDataHelper.onlyIfChanged(db.getAccountDao().selectAll());
return LiveDataHelper.onlyIfChanged(db.getAccountDao().getAllAccounts());
}

public LiveData<List<Board>> getBoards(long accountId) {
Expand Down Expand Up @@ -520,6 +520,10 @@ public Account getAccountByIdDirectly(long accountId) {
return db.getAccountDao().getAccountByIdDirectly(accountId);
}

public List<Account> getAllAccountsDirectly() {
return db.getAccountDao().getAllAccountsDirectly();
}

public User getUserByLocalIdDirectly(long localUserId) {
return db.getUserDao().getUserByLocalIdDirectly(localUserId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

@Dao
public interface AccountDao extends GenericDao<Account> {

@Query("SELECT * FROM account")
LiveData<List<Account>> getAccounts();

@Query("SELECT count(*) FROM account")
int countAccountsDirectly();

Expand All @@ -27,11 +23,14 @@ public interface AccountDao extends GenericDao<Account> {
Account getAccountByIdDirectly(long id);

@Query("SELECT * from account where id = :id")
LiveData<Account> selectById(long id);
LiveData<Account> getAccountById(long id);

@Query("SELECT * from account where name = :name")
LiveData<Account> selectById(String name);
LiveData<Account> getAccountByName(String name);

@Query("SELECT * from account")
LiveData<List<Account>> getAllAccounts();

@Query("SELECT * from account")
LiveData<List<Account>> selectAll();
List<Account> getAllAccountsDirectly();
}

0 comments on commit b47340d

Please sign in to comment.