From 81d43f047203462fc925af26a430cdaee820db86 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Wed, 14 Jul 2021 00:17:52 +0900 Subject: [PATCH] Convert DirectorySelectActivityViewModel to kotlin --- .../activities/DirectorySelectActivity.kt | 8 +- .../DirectorySelectActivityViewModel.kt | 156 ++++++++---------- 2 files changed, 70 insertions(+), 94 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/DirectorySelectActivity.kt b/app/src/main/java/awais/instagrabber/activities/DirectorySelectActivity.kt index 5bef280e..d8c36d62 100644 --- a/app/src/main/java/awais/instagrabber/activities/DirectorySelectActivity.kt +++ b/app/src/main/java/awais/instagrabber/activities/DirectorySelectActivity.kt @@ -9,7 +9,7 @@ import android.os.Bundle import android.provider.DocumentsContract import android.util.Log import android.view.View -import androidx.lifecycle.ViewModelProvider +import androidx.activity.viewModels import awais.instagrabber.R import awais.instagrabber.databinding.ActivityDirectorySelectBinding import awais.instagrabber.dialogs.ConfirmDialogFragment @@ -25,13 +25,13 @@ class DirectorySelectActivity : BaseLanguageActivity() { private var initialUri: Uri? = null private lateinit var binding: ActivityDirectorySelectBinding - private lateinit var viewModel: DirectorySelectActivityViewModel + + private val viewModel: DirectorySelectActivityViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityDirectorySelectBinding.inflate(layoutInflater) setContentView(binding.root) - viewModel = ViewModelProvider(this).get(DirectorySelectActivityViewModel::class.java) val intent = intent viewModel.setInitialUri(intent) setupObservers() @@ -52,7 +52,7 @@ class DirectorySelectActivity : BaseLanguageActivity() { binding.message2.visibility = 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.loadingIndicator.visibility = if (loading) View.VISIBLE else View.GONE }) diff --git a/app/src/main/java/awais/instagrabber/viewmodels/DirectorySelectActivityViewModel.kt b/app/src/main/java/awais/instagrabber/viewmodels/DirectorySelectActivityViewModel.kt index 03e0420f..732c2d9e 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/DirectorySelectActivityViewModel.kt +++ b/app/src/main/java/awais/instagrabber/viewmodels/DirectorySelectActivityViewModel.kt @@ -1,113 +1,89 @@ -package awais.instagrabber.viewmodels; +package awais.instagrabber.viewmodels -import android.app.Application; -import android.content.Intent; -import android.content.UriPermission; -import android.net.Uri; -import android.os.Parcelable; +import android.app.Application +import android.content.Intent +import android.content.UriPermission +import android.net.Uri +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; -import androidx.annotation.Nullable; -import androidx.documentfile.provider.DocumentFile; -import androidx.lifecycle.AndroidViewModel; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MutableLiveData; +class DirectorySelectActivityViewModel(application: Application) : AndroidViewModel(application) { + private val _message = MutableLiveData() + private val _prevUri = MutableLiveData() + private val _loading = MutableLiveData(false) + private val _dirSuccess = MutableLiveData(false) -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; -import java.util.List; + val message: LiveData = _message + val prevUri: LiveData = _prevUri + val loading: LiveData = _loading + val dirSuccess: LiveData = _dirSuccess -import awais.instagrabber.R; -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 message = new MutableLiveData<>(); - private final MutableLiveData prevUri = new MutableLiveData<>(); - private final MutableLiveData loading = new MutableLiveData<>(false); - private final MutableLiveData dirSuccess = new MutableLiveData<>(false); - - public DirectorySelectActivityViewModel(final Application application) { - super(application); - } - - public LiveData getMessage() { - return message; - } - - public LiveData getPrevUri() { - return prevUri; - } - - public LiveData isLoading() { - return loading; - } - - public LiveData getDirSuccess() { - return dirSuccess; - } - - public void setInitialUri(final Intent intent) { + fun setInitialUri(intent: Intent?) { if (intent == null) { - setMessage(null); - return; + setMessage(null) + return } - final Parcelable initialUriParcelable = intent.getParcelableExtra(Constants.EXTRA_INITIAL_URI); - if (!(initialUriParcelable instanceof Uri)) { - setMessage(null); - return; + val initialUriParcelable = intent.getParcelableExtra(Constants.EXTRA_INITIAL_URI) + if (initialUriParcelable !is Uri) { + setMessage(null) + return } - setMessage((Uri) initialUriParcelable); + setMessage(initialUriParcelable as Uri?) } - private void setMessage(@Nullable final Uri initialUri) { + private fun setMessage(initialUri: Uri?) { if (initialUri == null) { - final String prevVersionFolderPath = Utils.settingsHelper.getString(FOLDER_PATH); - if (TextUtils.isEmpty(prevVersionFolderPath)) { + val prevVersionFolderPath = Utils.settingsHelper.getString(PreferenceKeys.FOLDER_PATH) + if (isEmpty(prevVersionFolderPath)) { // default message - message.postValue(getApplication().getString(R.string.dir_select_default_message)); - prevUri.postValue(null); - return; + _message.postValue(getApplication().getString(R.string.dir_select_default_message)) + _prevUri.postValue(null) + return } - message.postValue(getApplication().getString(R.string.dir_select_reselect_message)); - prevUri.postValue(prevVersionFolderPath); - return; + _message.postValue(getApplication().getString(R.string.dir_select_reselect_message)) + _prevUri.postValue(prevVersionFolderPath) + return } - final List existingPermissions = getApplication().getContentResolver().getPersistedUriPermissions(); - final boolean anyMatch = existingPermissions.stream().anyMatch(uriPermission -> uriPermission.getUri().equals(initialUri)); - final DocumentFile documentFile = DocumentFile.fromSingleUri(getApplication(), initialUri); - String path; - try { - path = URLDecoder.decode(initialUri.toString(), StandardCharsets.UTF_8.toString()); - } catch (UnsupportedEncodingException e) { - path = initialUri.toString(); + val existingPermissions = getApplication().contentResolver.persistedUriPermissions + val anyMatch = existingPermissions.stream().anyMatch { uriPermission: UriPermission -> uriPermission.uri == initialUri } + val documentFile = DocumentFile.fromSingleUri(getApplication(), initialUri) + val path: String = try { + URLDecoder.decode(initialUri.toString(), StandardCharsets.UTF_8.toString()) + } catch (e: UnsupportedEncodingException) { + initialUri.toString() } if (!anyMatch) { - message.postValue(getApplication().getString(R.string.dir_select_permission_revoked_message)); - prevUri.postValue(path); - return; + _message.postValue(getApplication().getString(R.string.dir_select_permission_revoked_message)) + _prevUri.postValue(path) + return } - if (documentFile == null || !documentFile.exists() || documentFile.lastModified() == 0) { - message.postValue(getApplication().getString(R.string.dir_select_folder_not_exist)); - prevUri.postValue(path); + if (documentFile == null || !documentFile.exists() || documentFile.lastModified() == 0L) { + _message.postValue(getApplication().getString(R.string.dir_select_folder_not_exist)) + _prevUri.postValue(path) } } - public void setupSelectedDir(@NonNull final Intent data) throws DownloadUtils.ReselectDocumentTreeException { - loading.postValue(true); + @Throws(ReselectDocumentTreeException::class) + fun setupSelectedDir(data: Intent) { + _loading.postValue(true) try { - Utils.setupSelectedDir(getApplication(), data); - message.postValue(getApplication().getString(R.string.dir_select_success_message)); - dirSuccess.postValue(true); + Utils.setupSelectedDir(getApplication(), data) + _message.postValue(getApplication().getString(R.string.dir_select_success_message)) + _dirSuccess.postValue(true) } finally { - loading.postValue(false); + _loading.postValue(false) } } -} +} \ No newline at end of file