Move backup status operations off the main thread.
This commit is contained in:
parent
f02b8001e4
commit
d44bef0eda
@ -21,6 +21,7 @@ import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.compose.ui.platform.ComposeView;
|
||||
import androidx.core.text.HtmlCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.navigation.Navigation;
|
||||
|
||||
import com.google.android.material.timepicker.MaterialTimePicker;
|
||||
@ -76,6 +77,8 @@ public class BackupsPreferenceFragment extends Fragment {
|
||||
|
||||
private final NumberFormat formatter = NumberFormat.getInstance();
|
||||
|
||||
private BackupsPreferenceViewModel viewModel;
|
||||
|
||||
@Override
|
||||
public @Nullable View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_backups, container, false);
|
||||
@ -109,6 +112,15 @@ public class BackupsPreferenceFragment extends Fragment {
|
||||
|
||||
EventBus.getDefault().register(this);
|
||||
|
||||
viewModel = new ViewModelProvider(this).get(BackupsPreferenceViewModel.class);
|
||||
viewModel.getBackupsEnabled().observe(getViewLifecycleOwner(), enabled -> {
|
||||
if (enabled) {
|
||||
setBackupsEnabled();
|
||||
} else {
|
||||
setBackupsDisabled();
|
||||
}
|
||||
});
|
||||
|
||||
updateToggle();
|
||||
}
|
||||
|
||||
@ -116,7 +128,7 @@ public class BackupsPreferenceFragment extends Fragment {
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
setBackupStatus();
|
||||
viewModel.refreshBackupStatus();
|
||||
setBackupSummary();
|
||||
setInfo();
|
||||
setUpdateState();
|
||||
@ -179,21 +191,6 @@ public class BackupsPreferenceFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
private void setBackupStatus() {
|
||||
if (SignalStore.settings().isBackupEnabled()) {
|
||||
if (BackupUtil.canUserAccessBackupDirectory(requireContext())) {
|
||||
setBackupsEnabled();
|
||||
} else {
|
||||
Log.w(TAG, "Cannot access backup directory. Disabling backups.");
|
||||
|
||||
BackupUtil.disableBackups(requireContext());
|
||||
setBackupsDisabled();
|
||||
}
|
||||
} else {
|
||||
setBackupsDisabled();
|
||||
}
|
||||
}
|
||||
|
||||
private void setBackupSummary() {
|
||||
Pair<String, String> date = BackupUtil.getLastBackupTime(requireContext(), Locale.getDefault());
|
||||
summary.setText(getString(R.string.BackupsPreferenceFragment__last_backup, date.getFirst()));
|
||||
|
||||
@ -0,0 +1,45 @@
|
||||
package org.thoughtcrime.securesms.preferences
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.signal.core.util.logging.Log
|
||||
import org.thoughtcrime.securesms.dependencies.AppDependencies
|
||||
import org.thoughtcrime.securesms.keyvalue.SignalStore
|
||||
import org.thoughtcrime.securesms.util.BackupUtil
|
||||
|
||||
class BackupsPreferenceViewModel : ViewModel() {
|
||||
|
||||
private val internalBackupsEnabled = MutableLiveData<Boolean>()
|
||||
val backupsEnabled: LiveData<Boolean> = internalBackupsEnabled
|
||||
|
||||
fun refreshBackupStatus() {
|
||||
viewModelScope.launch {
|
||||
val enabled = withContext(Dispatchers.IO) {
|
||||
val context = AppDependencies.application
|
||||
|
||||
if (SignalStore.settings.isBackupEnabled) {
|
||||
if (BackupUtil.canUserAccessBackupDirectory(context)) {
|
||||
true
|
||||
} else {
|
||||
Log.w(TAG, "Cannot access backup directory. Disabling backups.")
|
||||
BackupUtil.disableBackups(context)
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
internalBackupsEnabled.value = enabled
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TAG = Log.tag(BackupsPreferenceViewModel::class.java)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user