Skip to content

Commit

Permalink
Merge branch 'matomo-integration' into refactor-fileInfoActionView
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Feb 22, 2022
2 parents 70ff537 + 3ac92cc commit 149ed7b
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 93 deletions.
11 changes: 7 additions & 4 deletions app/src/main/java/com/infomaniak/drive/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,15 @@ class MainViewModel(appContext: Application) : AndroidViewModel(appContext) {
}

fun deleteFileFromFavorites(file: File, userDrive: UserDrive? = null) = liveData(Dispatchers.IO) {
CoroutineScope(Dispatchers.IO).launch {
FileController.updateFile(file.id, userDrive = userDrive) {
it.isFavorite = false
with(ApiRepository.deleteFavoriteFile(file)) {
emit(this)

if (isSuccess()) {
FileController.updateFile(file.id, userDrive = userDrive) {
it.isFavorite = false
}
}
}
emit(ApiRepository.deleteFavoriteFile(file))
}

fun getFileDetails(fileId: Int, userDrive: UserDrive): LiveData<File?> {
Expand Down
124 changes: 65 additions & 59 deletions app/src/main/java/com/infomaniak/drive/ui/SaveExternalFilesActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.infomaniak.drive.data.cache.FileController
import com.infomaniak.drive.data.models.UiSettings
import com.infomaniak.drive.data.models.UploadFile
import com.infomaniak.drive.data.models.UserDrive
import com.infomaniak.drive.data.models.drive.Drive
import com.infomaniak.drive.ui.fileList.SelectFolderActivity
import com.infomaniak.drive.ui.menu.settings.SelectDriveDialog
import com.infomaniak.drive.ui.menu.settings.SelectDriveViewModel
Expand Down Expand Up @@ -84,11 +85,20 @@ class SaveExternalFilesActivity : BaseActivity() {
setupDrivePermissions()
activeDefaultUser()
application.trackCurrentUserId()
observeSelectedDrive()
observeFolderId()
fetchSelectedDrive()
fetchFolder()
setupSaveButton()
}

private fun isAuth(): Boolean {
if (AccountUtils.currentUserId == -1) {
startActivity(Intent(this, LaunchActivity::class.java))
finish()
return false
}
return true
}

private fun setupDrivePermissions() {
drivePermissions = DrivePermissions().apply {
registerPermissions(
Expand All @@ -99,24 +109,56 @@ class SaveExternalFilesActivity : BaseActivity() {
}
}

private fun observeSelectedDrive() {
selectDriveViewModel.selectedDrive.observe(this) {
private fun activeDefaultUser() {
AccountUtils.getAllUsers().observe(this) { users ->
if (users.size > 1) activeSelectDrive()
}
val currentUserDrives = DriveInfosController.getDrives(AccountUtils.currentUserId)
if (currentUserDrives.size > 1) activeSelectDrive()

var (userId, driveId) = UiSettings(this).getSaveExternalFilesPref()
var drive = DriveInfosController.getDrives(userId, driveId).firstOrNull()
if (drive == null) {
userId = AccountUtils.currentUserId
drive = AccountUtils.getCurrentDrive()
}

selectDriveViewModel.apply {
selectedUserId.value = userId
selectedDrive.value = drive
}
}

private fun fetchSelectedDrive() = with(selectDriveViewModel) {
selectedDrive.observe(this@SaveExternalFilesActivity) {
it?.let { drive ->
driveIcon.imageTintList = ColorStateList.valueOf(Color.parseColor(drive.preferences.color))
driveName.text = drive.name
displaySelectedDrive(drive)
saveButton.isEnabled = false
pathTitle.isVisible = true
setupSelectPath()
UiSettings(this).getSaveExternalFilesPref().let { (userId, driveId, folderId) ->
saveExternalFilesViewModel.folderId.value =
if (userId == selectDriveViewModel.selectedUserId.value && driveId == drive.id) folderId else null
UiSettings(this@SaveExternalFilesActivity).getSaveExternalFilesPref().let { (userId, driveId, folderId) ->
saveExternalFilesViewModel.folderId.value = if (userId == selectedUserId.value && driveId == drive.id) {
folderId
} else {
null
}
}
} ?: run {
showSelectDrive()
displayDriveSelection()
}
}
}

private fun displaySelectedDrive(drive: Drive) {
driveIcon.imageTintList = ColorStateList.valueOf(Color.parseColor(drive.preferences.color))
driveName.text = drive.name
}

private fun displayDriveSelection() {
driveIcon.imageTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.iconColor))
driveName.setText(R.string.selectDriveTitle)
}

private fun setupSelectPath() {
selectPath.apply {
isVisible = true
Expand All @@ -130,23 +172,19 @@ class SaveExternalFilesActivity : BaseActivity() {
}
}

private fun observeFolderId() {
saveExternalFilesViewModel.folderId.observe(this) { folderId ->
val folder = with(selectDriveViewModel) {
if (selectedUserId.value == null || selectedDrive.value?.id == null || folderId == null) {
null
} else {
val userDrive = UserDrive(
userId = selectedUserId.value!!,
driveId = selectedDrive.value!!.id,
)
FileController.getFileById(folderId, userDrive)
}
private fun fetchFolder() = with(selectDriveViewModel) {
saveExternalFilesViewModel.folderId.observe(this@SaveExternalFilesActivity) { folderId ->

val folder = if (selectedUserId.value == null || selectedDrive.value?.id == null || folderId == null) {
null
} else {
val userDrive = UserDrive(userId = selectedUserId.value!!, driveId = selectedDrive.value!!.id)
FileController.getFileById(folderId, userDrive)
}

folder?.let {
val folderName = if (folder.isRoot()) {
getString(R.string.allRootName, selectDriveViewModel.selectedDrive.value?.name)
getString(R.string.allRootName, selectedDrive.value?.name)
} else {
folder.name
}
Expand All @@ -158,14 +196,14 @@ class SaveExternalFilesActivity : BaseActivity() {
}
}

private fun setupSaveButton() {
private fun setupSaveButton() = with(selectDriveViewModel) {
saveButton.apply {
initProgress(this@SaveExternalFilesActivity)
setOnClickListener {
showProgress()
if (drivePermissions.checkSyncPermissions()) {
val userId = selectDriveViewModel.selectedUserId.value!!
val driveId = selectDriveViewModel.selectedDrive.value?.id!!
val userId = selectedUserId.value!!
val driveId = selectedDrive.value?.id!!
val folderId = saveExternalFilesViewModel.folderId.value!!

UiSettings(this@SaveExternalFilesActivity).setSaveExternalFilesPref(userId, driveId, folderId)
Expand All @@ -175,7 +213,7 @@ class SaveExternalFilesActivity : BaseActivity() {
finish()
} else {
withContext(Dispatchers.Main) {
this@apply?.hideProgress(R.string.buttonSave)
saveButton?.hideProgress(R.string.buttonSave)
showSnackbar(R.string.errorSave)
}
}
Expand Down Expand Up @@ -209,33 +247,6 @@ class SaveExternalFilesActivity : BaseActivity() {
}
}

private fun activeDefaultUser() {
AccountUtils.getAllUsers().observe(this) { users ->
if (users.size > 1) activeSelectDrive()
}
val currentUserDrives = DriveInfosController.getDrives(AccountUtils.currentUserId)
if (currentUserDrives.size > 1) activeSelectDrive()

var (userId, driveId) = UiSettings(this).getSaveExternalFilesPref()
var drive = DriveInfosController.getDrives(userId, driveId).firstOrNull()
if (drive == null) {
userId = AccountUtils.currentUserId
drive = AccountUtils.getCurrentDrive()
}

selectDriveViewModel.selectedUserId.value = userId
selectDriveViewModel.selectedDrive.value = drive
}

private fun isAuth(): Boolean {
if (AccountUtils.currentUserId == -1) {
startActivity(Intent(this, LaunchActivity::class.java))
finish()
return false
}
return true
}

private fun handleSendSingle() {
fileNameEdit.addTextChangedListener {
fileNameEdit.showOrHideEmptyError()
Expand Down Expand Up @@ -287,11 +298,6 @@ class SaveExternalFilesActivity : BaseActivity() {
saveExternalFilesViewModel.folderId.value != null
}

private fun showSelectDrive() {
driveIcon.imageTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.iconColor))
driveName.setText(R.string.selectDriveTitle)
}

private fun activeSelectDrive() {
switchDrive.isVisible = true
selectDrive.setOnClickListener { SelectDriveDialog().show(supportFragmentManager, "SyncSettingsSelectDriveDialog") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,11 @@ class AddFileBottomSheetDialog : BottomSheetDialogFragment() {
super.onViewCreated(view, savedInstanceState)
currentFolder.setFileItem(currentFolderFile)

openCameraPermissions = DrivePermissions()
openCameraPermissions.registerPermissions(this) { autorized ->
if (autorized) openCamera()
openCameraPermissions = DrivePermissions().apply {
registerPermissions(this@AddFileBottomSheetDialog) { authorized -> if (authorized) openCamera() }
}
uploadFilesPermissions = DrivePermissions()
uploadFilesPermissions.registerPermissions(this) { autorized ->
if (autorized) uploadFiles()
uploadFilesPermissions = DrivePermissions().apply {
registerPermissions(this@AddFileBottomSheetDialog) { authorized -> if (authorized) uploadFiles() }
}

openCamera.setOnClickListener { openCamera() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class SelectFolderActivity : BaseActivity() {
const val USER_ID_TAG = "userId"
const val USER_DRIVE_ID_TAG = "userDriveId"
const val FOLDER_ID_TAG = "folderId"
const val FOLDER_NAME_TAG = "folderNAME"
const val DISABLE_SELECTED_FOLDER_TAG = "disableSelectedFolder"
const val FOLDER_NAME_TAG = "folderName"
const val CURRENT_FOLDER_ID_TAG = "currentFolderId"
const val CUSTOM_ARGS_TAG = "customArgs"

const val BULK_OPERATION_CUSTOM_TAG = "bulk_operation_type"
Expand All @@ -56,10 +56,12 @@ class SelectFolderActivity : BaseActivity() {

mainViewModel.selectFolderUserDrive = currentUserDrive

val currentFolderId = extras?.getInt(CURRENT_FOLDER_ID_TAG)

saveExternalViewModel.apply {
userDrive = currentUserDrive
currentDrive = DriveInfosController.getDrives(userId, driveId).firstOrNull()
disableSelectedFolderId = extras?.getInt(DISABLE_SELECTED_FOLDER_TAG)
disableSelectedFolderId = currentFolderId
}

super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene

setBackActionHandlers()

drivePermissions = DrivePermissions()
drivePermissions.registerPermissions(this) { authorized -> if (authorized) downloadFileClicked() }
drivePermissions = DrivePermissions().apply {
registerPermissions(this@PreviewSliderFragment) { authorized -> if (authorized) downloadFileClicked() }
}

bottomSheetFileInfos.apply {
init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ class SelectMediaFoldersDialog : FullScreenBottomSheetDialog() {
initialListView = mediaFolderList,
)

val drivePermissions = DrivePermissions()
drivePermissions.registerPermissions(this) { authorized -> if (authorized) loadFolders() else dismiss() }
val drivePermissions = DrivePermissions().apply {
registerPermissions(this@SelectMediaFoldersDialog) { authorized -> if (authorized) loadFolders() else dismiss() }
}
if (drivePermissions.checkWriteStoragePermission()) loadFolders()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ class SettingsFragment : Fragment() {
findNavController().popBackStack()
}

val drivePermissions = DrivePermissions()
drivePermissions.registerPermissions(this) { autorized -> if (autorized) requireActivity().syncImmediately() }
val drivePermissions = DrivePermissions().apply {
registerPermissions(this@SettingsFragment) { authorized -> if (authorized) requireActivity().syncImmediately() }
}

onlyWifiSyncValue.isChecked = AppSettings.onlyWifiSync
onlyWifiSyncValue.setOnCheckedChangeListener { _, isChecked ->
context?.applicationContext?.trackEventWithBooleanValue("settings", "onlyWifiTransfer", isChecked)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,23 @@ class SyncSettingsActivity : BaseActivity() {
val intent = Intent(this, SelectFolderActivity::class.java).apply {
putExtra(SelectFolderActivity.USER_ID_TAG, selectDriveViewModel.selectedUserId.value)
putExtra(SelectFolderActivity.USER_DRIVE_ID_TAG, selectDriveViewModel.selectedDrive.value?.id)
putExtra(SelectFolderActivity.DISABLE_SELECTED_FOLDER_TAG, Utils.ROOT_ID)
putExtra(SelectFolderActivity.CURRENT_FOLDER_ID_TAG, Utils.ROOT_ID)
}
selectFolderResultLauncher.launch(intent)
}

syncSettingsViewModel.syncFolder.observe(this) { syncFolder ->
syncSettingsViewModel.syncFolder.observe(this) { syncFolderId ->
val selectedUserId = selectDriveViewModel.selectedUserId.value
val selectedDriveId = selectDriveViewModel.selectedDrive.value?.id
if (syncFolder != null && selectedUserId != null && selectedDriveId != null) {
FileController.getFileById(syncFolder, UserDrive(selectedUserId, selectedDriveId))?.let {
if (syncFolderId != null && selectedUserId != null && selectedDriveId != null) {
FileController.getFileById(syncFolderId, UserDrive(selectedUserId, selectedDriveId))?.let {
pathName.text = it.name
changeSaveButtonStatus()
}
} else {
pathName.setText(R.string.selectFolderTitle)
}
mediaFoldersSettingsVisibility(syncFolder != null)
mediaFoldersSettingsVisibility(syncFolderId != null)
}

syncSettingsViewModel.saveOldPictures.observe(this) {
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/java/com/infomaniak/drive/utils/DrivePermissions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import android.os.Build
import android.os.PowerManager
import android.provider.Settings
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.contract.ActivityResultContracts.RequestMultiplePermissions
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
Expand All @@ -54,17 +54,16 @@ class DrivePermissions {

fun registerPermissions(activity: FragmentActivity, onPermissionResult: ((authorized: Boolean) -> Unit)? = null) {
this.activity = activity
registerForActivityResult =
activity.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
val authorized = permissions.values.all { it == true }
resultPermissions(onPermissionResult, authorized)
}
registerForActivityResult = activity.registerForActivityResult(RequestMultiplePermissions()) { permissions ->
val authorized = permissions.values.all { it == true }
resultPermissions(onPermissionResult, authorized)
}
}

fun registerPermissions(fragment: Fragment, onPermissionResult: ((authorized: Boolean) -> Unit)? = null) {
this.activity = fragment.requireActivity()
registerForActivityResult =
fragment.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
fragment.registerForActivityResult(RequestMultiplePermissions()) { permissions ->
val authorized = permissions.values.all { it == true }
resultPermissions(onPermissionResult, authorized)
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/infomaniak/drive/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ object Utils {
val intent = Intent(this, SelectFolderActivity::class.java).apply {
putExtra(SelectFolderActivity.USER_ID_TAG, AccountUtils.currentUserId)
putExtra(SelectFolderActivity.USER_DRIVE_ID_TAG, AccountUtils.currentDriveId)
putExtra(SelectFolderActivity.DISABLE_SELECTED_FOLDER_TAG, currentFolderId)
putExtra(SelectFolderActivity.CURRENT_FOLDER_ID_TAG, currentFolderId)
putExtra(
SelectFolderActivity.CUSTOM_ARGS_TAG,
bundleOf(SelectFolderActivity.BULK_OPERATION_CUSTOM_TAG to BulkOperationType.MOVE),
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/navigation/main_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_navigation"
app:startDestination="@id/homeFragment">

<fragment
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/navigation/select_folder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/select_folder"
app:startDestination="@id/selectFolderFragment">

<fragment
Expand Down

0 comments on commit 149ed7b

Please sign in to comment.