mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-22 06:37:30 +00:00
Convert DirectorySelectActivityViewModel to kotlin
This commit is contained in:
parent
41a4739529
commit
81d43f0472
@ -9,7 +9,7 @@ import android.os.Bundle
|
|||||||
import android.provider.DocumentsContract
|
import android.provider.DocumentsContract
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.activity.viewModels
|
||||||
import awais.instagrabber.R
|
import awais.instagrabber.R
|
||||||
import awais.instagrabber.databinding.ActivityDirectorySelectBinding
|
import awais.instagrabber.databinding.ActivityDirectorySelectBinding
|
||||||
import awais.instagrabber.dialogs.ConfirmDialogFragment
|
import awais.instagrabber.dialogs.ConfirmDialogFragment
|
||||||
@ -25,13 +25,13 @@ class DirectorySelectActivity : BaseLanguageActivity() {
|
|||||||
private var initialUri: Uri? = null
|
private var initialUri: Uri? = null
|
||||||
|
|
||||||
private lateinit var binding: ActivityDirectorySelectBinding
|
private lateinit var binding: ActivityDirectorySelectBinding
|
||||||
private lateinit var viewModel: DirectorySelectActivityViewModel
|
|
||||||
|
private val viewModel: DirectorySelectActivityViewModel by viewModels()
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
binding = ActivityDirectorySelectBinding.inflate(layoutInflater)
|
binding = ActivityDirectorySelectBinding.inflate(layoutInflater)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
viewModel = ViewModelProvider(this).get(DirectorySelectActivityViewModel::class.java)
|
|
||||||
val intent = intent
|
val intent = intent
|
||||||
viewModel.setInitialUri(intent)
|
viewModel.setInitialUri(intent)
|
||||||
setupObservers()
|
setupObservers()
|
||||||
@ -52,7 +52,7 @@ class DirectorySelectActivity : BaseLanguageActivity() {
|
|||||||
binding.message2.visibility = View.VISIBLE
|
binding.message2.visibility = View.VISIBLE
|
||||||
})
|
})
|
||||||
viewModel.dirSuccess.observe(this, { success: Boolean -> binding.selectDir.visibility = if (success) View.GONE else View.VISIBLE })
|
viewModel.dirSuccess.observe(this, { success: Boolean -> binding.selectDir.visibility = if (success) View.GONE else View.VISIBLE })
|
||||||
viewModel.isLoading.observe(this, { loading: Boolean ->
|
viewModel.loading.observe(this, { loading: Boolean ->
|
||||||
binding.message.visibility = if (loading) View.GONE else View.VISIBLE
|
binding.message.visibility = if (loading) View.GONE else View.VISIBLE
|
||||||
binding.loadingIndicator.visibility = if (loading) View.VISIBLE else View.GONE
|
binding.loadingIndicator.visibility = if (loading) View.VISIBLE else View.GONE
|
||||||
})
|
})
|
||||||
|
@ -1,113 +1,89 @@
|
|||||||
package awais.instagrabber.viewmodels;
|
package awais.instagrabber.viewmodels
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application
|
||||||
import android.content.Intent;
|
import android.content.Intent
|
||||||
import android.content.UriPermission;
|
import android.content.UriPermission
|
||||||
import android.net.Uri;
|
import android.net.Uri
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable
|
||||||
|
import androidx.documentfile.provider.DocumentFile
|
||||||
|
import androidx.lifecycle.AndroidViewModel
|
||||||
|
import androidx.lifecycle.LiveData
|
||||||
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
import awais.instagrabber.R
|
||||||
|
import awais.instagrabber.fragments.settings.PreferenceKeys
|
||||||
|
import awais.instagrabber.utils.Constants
|
||||||
|
import awais.instagrabber.utils.DownloadUtils.ReselectDocumentTreeException
|
||||||
|
import awais.instagrabber.utils.TextUtils.isEmpty
|
||||||
|
import awais.instagrabber.utils.Utils
|
||||||
|
import java.io.UnsupportedEncodingException
|
||||||
|
import java.net.URLDecoder
|
||||||
|
import java.nio.charset.StandardCharsets
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
class DirectorySelectActivityViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
import androidx.annotation.Nullable;
|
private val _message = MutableLiveData<String>()
|
||||||
import androidx.documentfile.provider.DocumentFile;
|
private val _prevUri = MutableLiveData<String?>()
|
||||||
import androidx.lifecycle.AndroidViewModel;
|
private val _loading = MutableLiveData(false)
|
||||||
import androidx.lifecycle.LiveData;
|
private val _dirSuccess = MutableLiveData(false)
|
||||||
import androidx.lifecycle.MutableLiveData;
|
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
val message: LiveData<String> = _message
|
||||||
import java.net.URLDecoder;
|
val prevUri: LiveData<String?> = _prevUri
|
||||||
import java.nio.charset.StandardCharsets;
|
val loading: LiveData<Boolean> = _loading
|
||||||
import java.util.List;
|
val dirSuccess: LiveData<Boolean> = _dirSuccess
|
||||||
|
|
||||||
import awais.instagrabber.R;
|
fun setInitialUri(intent: Intent?) {
|
||||||
import awais.instagrabber.utils.Constants;
|
|
||||||
import awais.instagrabber.utils.DownloadUtils;
|
|
||||||
import awais.instagrabber.utils.TextUtils;
|
|
||||||
import awais.instagrabber.utils.Utils;
|
|
||||||
|
|
||||||
import static awais.instagrabber.fragments.settings.PreferenceKeys.FOLDER_PATH;
|
|
||||||
|
|
||||||
public class DirectorySelectActivityViewModel extends AndroidViewModel {
|
|
||||||
private static final String TAG = DirectorySelectActivityViewModel.class.getSimpleName();
|
|
||||||
|
|
||||||
private final MutableLiveData<String> message = new MutableLiveData<>();
|
|
||||||
private final MutableLiveData<String> prevUri = new MutableLiveData<>();
|
|
||||||
private final MutableLiveData<Boolean> loading = new MutableLiveData<>(false);
|
|
||||||
private final MutableLiveData<Boolean> dirSuccess = new MutableLiveData<>(false);
|
|
||||||
|
|
||||||
public DirectorySelectActivityViewModel(final Application application) {
|
|
||||||
super(application);
|
|
||||||
}
|
|
||||||
|
|
||||||
public LiveData<String> getMessage() {
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public LiveData<String> getPrevUri() {
|
|
||||||
return prevUri;
|
|
||||||
}
|
|
||||||
|
|
||||||
public LiveData<Boolean> isLoading() {
|
|
||||||
return loading;
|
|
||||||
}
|
|
||||||
|
|
||||||
public LiveData<Boolean> getDirSuccess() {
|
|
||||||
return dirSuccess;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setInitialUri(final Intent intent) {
|
|
||||||
if (intent == null) {
|
if (intent == null) {
|
||||||
setMessage(null);
|
setMessage(null)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
final Parcelable initialUriParcelable = intent.getParcelableExtra(Constants.EXTRA_INITIAL_URI);
|
val initialUriParcelable = intent.getParcelableExtra<Parcelable>(Constants.EXTRA_INITIAL_URI)
|
||||||
if (!(initialUriParcelable instanceof Uri)) {
|
if (initialUriParcelable !is Uri) {
|
||||||
setMessage(null);
|
setMessage(null)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
setMessage((Uri) initialUriParcelable);
|
setMessage(initialUriParcelable as Uri?)
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setMessage(@Nullable final Uri initialUri) {
|
private fun setMessage(initialUri: Uri?) {
|
||||||
if (initialUri == null) {
|
if (initialUri == null) {
|
||||||
final String prevVersionFolderPath = Utils.settingsHelper.getString(FOLDER_PATH);
|
val prevVersionFolderPath = Utils.settingsHelper.getString(PreferenceKeys.FOLDER_PATH)
|
||||||
if (TextUtils.isEmpty(prevVersionFolderPath)) {
|
if (isEmpty(prevVersionFolderPath)) {
|
||||||
// default message
|
// default message
|
||||||
message.postValue(getApplication().getString(R.string.dir_select_default_message));
|
_message.postValue(getApplication<Application>().getString(R.string.dir_select_default_message))
|
||||||
prevUri.postValue(null);
|
_prevUri.postValue(null)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
message.postValue(getApplication().getString(R.string.dir_select_reselect_message));
|
_message.postValue(getApplication<Application>().getString(R.string.dir_select_reselect_message))
|
||||||
prevUri.postValue(prevVersionFolderPath);
|
_prevUri.postValue(prevVersionFolderPath)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
final List<UriPermission> existingPermissions = getApplication().getContentResolver().getPersistedUriPermissions();
|
val existingPermissions = getApplication<Application>().contentResolver.persistedUriPermissions
|
||||||
final boolean anyMatch = existingPermissions.stream().anyMatch(uriPermission -> uriPermission.getUri().equals(initialUri));
|
val anyMatch = existingPermissions.stream().anyMatch { uriPermission: UriPermission -> uriPermission.uri == initialUri }
|
||||||
final DocumentFile documentFile = DocumentFile.fromSingleUri(getApplication(), initialUri);
|
val documentFile = DocumentFile.fromSingleUri(getApplication(), initialUri)
|
||||||
String path;
|
val path: String = try {
|
||||||
try {
|
URLDecoder.decode(initialUri.toString(), StandardCharsets.UTF_8.toString())
|
||||||
path = URLDecoder.decode(initialUri.toString(), StandardCharsets.UTF_8.toString());
|
} catch (e: UnsupportedEncodingException) {
|
||||||
} catch (UnsupportedEncodingException e) {
|
initialUri.toString()
|
||||||
path = initialUri.toString();
|
|
||||||
}
|
}
|
||||||
if (!anyMatch) {
|
if (!anyMatch) {
|
||||||
message.postValue(getApplication().getString(R.string.dir_select_permission_revoked_message));
|
_message.postValue(getApplication<Application>().getString(R.string.dir_select_permission_revoked_message))
|
||||||
prevUri.postValue(path);
|
_prevUri.postValue(path)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
if (documentFile == null || !documentFile.exists() || documentFile.lastModified() == 0) {
|
if (documentFile == null || !documentFile.exists() || documentFile.lastModified() == 0L) {
|
||||||
message.postValue(getApplication().getString(R.string.dir_select_folder_not_exist));
|
_message.postValue(getApplication<Application>().getString(R.string.dir_select_folder_not_exist))
|
||||||
prevUri.postValue(path);
|
_prevUri.postValue(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setupSelectedDir(@NonNull final Intent data) throws DownloadUtils.ReselectDocumentTreeException {
|
@Throws(ReselectDocumentTreeException::class)
|
||||||
loading.postValue(true);
|
fun setupSelectedDir(data: Intent) {
|
||||||
|
_loading.postValue(true)
|
||||||
try {
|
try {
|
||||||
Utils.setupSelectedDir(getApplication(), data);
|
Utils.setupSelectedDir(getApplication(), data)
|
||||||
message.postValue(getApplication().getString(R.string.dir_select_success_message));
|
_message.postValue(getApplication<Application>().getString(R.string.dir_select_success_message))
|
||||||
dirSuccess.postValue(true);
|
_dirSuccess.postValue(true)
|
||||||
} finally {
|
} finally {
|
||||||
loading.postValue(false);
|
_loading.postValue(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user