Skip to content

Commit

Permalink
refactor: moved ReceiveExternalFilesViewModel creation (now when th…
Browse files Browse the repository at this point in the history
…e account is set)
  • Loading branch information
joragua committed Nov 28, 2024
1 parent 94ca7f7 commit 0a26f24
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
1 change: 0 additions & 1 deletion owncloudApp/src/main/java/com/owncloud/android/MainApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import com.owncloud.android.domain.capabilities.usecases.GetStoredCapabilitiesUs
import com.owncloud.android.domain.spaces.model.OCSpace
import com.owncloud.android.domain.spaces.usecases.GetPersonalSpaceForAccountUseCase
import com.owncloud.android.domain.user.usecases.GetStoredQuotaUseCase
import com.owncloud.android.domain.user.usecases.GetUserQuotasUseCase
import com.owncloud.android.extensions.createNotificationChannel
import com.owncloud.android.lib.common.SingleSessionManager
import com.owncloud.android.presentation.authentication.AccountUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ val viewModelModule = module {
viewModel { AuthenticationViewModel(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
viewModel { MigrationViewModel(MainApp.dataFolder, get(), get(), get(), get(), get(), get(), get()) }
viewModel { TransfersViewModel(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
viewModel { ReceiveExternalFilesViewModel(get(), get(), get()) }
viewModel { ReceiveExternalFilesViewModel(get(), get(), get(), get()) }
viewModel { (accountName: String, showPersonalSpace: Boolean) ->
SpacesListViewModel(get(), get(), get(), get(), get(), accountName, showPersonalSpace)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ReceiveExternalFilesViewModel(
private val synchronizeFolderUseCase: SynchronizeFolderUseCase,
private val coroutinesDispatcherProvider: CoroutinesDispatcherProvider,
private val getStoredCapabilitiesUseCase: GetStoredCapabilitiesUseCase,
private val accountName: String,
) : ViewModel() {

private val _syncFolderLiveData = MediatorLiveData<Event<UIResult<Unit>>>()
Expand All @@ -43,6 +44,14 @@ class ReceiveExternalFilesViewModel(
private val _spacesAreAllowed = MutableLiveData<Boolean>()
val spacesAreAllowed: LiveData<Boolean> = _spacesAreAllowed

init {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
val capabilities = getStoredCapabilitiesUseCase(GetStoredCapabilitiesUseCase.Params(accountName))
val spacesAvailableForAccount = capabilities?.isSpacesAllowed() == true
_spacesAreAllowed.postValue(spacesAvailableForAccount)
}
}

fun refreshFolderUseCase(
folderToSync: OCFile,
) = runUseCaseWithResult(
Expand All @@ -58,12 +67,4 @@ class ReceiveExternalFilesViewModel(
)
)

fun areSpacesAllowed(accountName: String) {
viewModelScope.launch(coroutinesDispatcherProvider.io) {
val capabilities = getStoredCapabilitiesUseCase(GetStoredCapabilitiesUseCase.Params(accountName))
val spacesAvailableForAccount = capabilities?.isSpacesAllowed() == true
_spacesAreAllowed.postValue(spacesAvailableForAccount)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
import com.owncloud.android.db.PreferenceManager;
import com.owncloud.android.domain.exceptions.UnauthorizedException;
import com.owncloud.android.domain.files.model.OCFile;
import com.owncloud.android.domain.spaces.model.OCSpace;
import com.owncloud.android.extensions.ActivityExtKt;
import com.owncloud.android.extensions.ThrowableExtKt;
import com.owncloud.android.lib.common.OwnCloudAccount;
Expand Down Expand Up @@ -118,6 +117,7 @@
import java.util.regex.Pattern;

import static com.owncloud.android.presentation.settings.advanced.SettingsAdvancedFragment.PREF_SHOW_HIDDEN_FILES;
import static org.koin.core.parameter.ParametersHolderKt.parametersOf;
import static org.koin.java.KoinJavaComponent.get;
import static org.koin.java.KoinJavaComponent.inject;

Expand Down Expand Up @@ -222,14 +222,11 @@ protected void onCreate(Bundle savedInstanceState) {
}
mSortOptionsView.setVisibility(View.GONE);

mReceiveExternalFilesViewModel = get(ReceiveExternalFilesViewModel.class);
subscribeToViewModels();

initPickerListener();

}
private void subscribeToViewModels() {
mReceiveExternalFilesViewModel.areSpacesAllowed(getAccount().name);
mReceiveExternalFilesViewModel.getSpacesAreAllowed().observe(this, spaces -> {
areSpacesAllowed = spaces;

Expand Down Expand Up @@ -339,7 +336,8 @@ protected void setAccount(Account account, boolean savedAccount) {
@Override
protected void onAccountSet(boolean stateWasRecovered) {
super.onAccountSet(mAccountWasRestored);
mReceiveExternalFilesViewModel.areSpacesAllowed(getAccount().name);
mReceiveExternalFilesViewModel = get(ReceiveExternalFilesViewModel.class, null , () -> parametersOf(getAccount().name));
subscribeToViewModels();
initTargetFolder();

mReceiveExternalFilesViewModel.getSyncFolderLiveData().observe(this, eventUiResult -> {
Expand All @@ -362,7 +360,7 @@ protected void onAccountSet(boolean stateWasRecovered) {
} else if (uiResult instanceof UIResult.Success) {
mSyncInProgress = false;
updateDirectoryList();
if (mParents.size() == 1) {
if (mParents.size() == 1 && !areSpacesAllowed) {
updateToolbar(getString(R.string.uploader_top_message));
}
if(fragmentContainer.getVisibility() == View.VISIBLE) {
Expand Down Expand Up @@ -435,7 +433,6 @@ public void onClick(DialogInterface dialog, int which) {
onAccountSet(mAccountWasRestored);
dialog.dismiss();
PreferenceManager.setLastUploadPath("/", this);
mReceiveExternalFilesViewModel.areSpacesAllowed(getAccount().name);
mAccountSelected = true;
mAccountSelectionShowing = false;
});
Expand All @@ -460,7 +457,7 @@ public void onBackPressed() {
String full_path = generatePath(mParents);
startSyncFolderOperation(getStorageManager().getFileByPath(full_path, currentSpaceId));
updateDirectoryList();
if (mParents.size() <= 1) {
if (mParents.size() <= 1 && !areSpacesAllowed) {
updateToolbar(getString(R.string.uploader_top_message));
}
}
Expand Down

0 comments on commit 0a26f24

Please sign in to comment.