1
- package awais.instagrabber.viewmodels ;
1
+ package awais.instagrabber.viewmodels
2
2
3
- import android.app.Application;
4
- import android.content.Intent;
5
- import android.content.UriPermission;
6
- import android.net.Uri;
7
- import android.os.Parcelable;
3
+ import android.app.Application
4
+ import android.content.Intent
5
+ import android.content.UriPermission
6
+ import android.net.Uri
7
+ import android.os.Parcelable
8
+ import androidx.documentfile.provider.DocumentFile
9
+ import androidx.lifecycle.AndroidViewModel
10
+ import androidx.lifecycle.LiveData
11
+ import androidx.lifecycle.MutableLiveData
12
+ import awais.instagrabber.R
13
+ import awais.instagrabber.fragments.settings.PreferenceKeys
14
+ import awais.instagrabber.utils.Constants
15
+ import awais.instagrabber.utils.DownloadUtils.ReselectDocumentTreeException
16
+ import awais.instagrabber.utils.TextUtils.isEmpty
17
+ import awais.instagrabber.utils.Utils
18
+ import java.io.UnsupportedEncodingException
19
+ import java.net.URLDecoder
20
+ import java.nio.charset.StandardCharsets
8
21
9
- import androidx.annotation.NonNull;
10
- import androidx.annotation.Nullable;
11
- import androidx.documentfile.provider.DocumentFile;
12
- import androidx.lifecycle.AndroidViewModel;
13
- import androidx.lifecycle.LiveData;
14
- import androidx.lifecycle.MutableLiveData;
22
+ class DirectorySelectActivityViewModel (application : Application ) : AndroidViewModel(application) {
23
+ private val _message = MutableLiveData <String >()
24
+ private val _prevUri = MutableLiveData <String ?>()
25
+ private val _loading = MutableLiveData (false )
26
+ private val _dirSuccess = MutableLiveData (false )
15
27
16
- import java.io.UnsupportedEncodingException;
17
- import java.net.URLDecoder;
18
- import java.nio.charset.StandardCharsets;
19
- import java.util.List;
28
+ val message : LiveData < String > = _message
29
+ val prevUri : LiveData < String ?> = _prevUri
30
+ val loading : LiveData < Boolean > = _loading
31
+ val dirSuccess : LiveData < Boolean > = _dirSuccess
20
32
21
- import awais.instagrabber.R;
22
- import awais.instagrabber.utils.Constants;
23
- import awais.instagrabber.utils.DownloadUtils;
24
- import awais.instagrabber.utils.TextUtils;
25
- import awais.instagrabber.utils.Utils;
26
-
27
- import static awais.instagrabber.fragments.settings.PreferenceKeys .FOLDER_PATH ;
28
-
29
- public class DirectorySelectActivityViewModel extends AndroidViewModel {
30
- private static final String TAG = DirectorySelectActivityViewModel .class .getSimpleName();
31
-
32
- private final MutableLiveData <String > message = new MutableLiveData <> ();
33
- private final MutableLiveData <String > prevUri = new MutableLiveData <> ();
34
- private final MutableLiveData <Boolean > loading = new MutableLiveData <> (false );
35
- private final MutableLiveData <Boolean > dirSuccess = new MutableLiveData <> (false );
36
-
37
- public DirectorySelectActivityViewModel (final Application application) {
38
- super (application);
39
- }
40
-
41
- public LiveData <String > getMessage() {
42
- return message;
43
- }
44
-
45
- public LiveData <String > getPrevUri() {
46
- return prevUri;
47
- }
48
-
49
- public LiveData <Boolean > isLoading() {
50
- return loading;
51
- }
52
-
53
- public LiveData <Boolean > getDirSuccess() {
54
- return dirSuccess;
55
- }
56
-
57
- public void setInitialUri(final Intent intent) {
33
+ fun setInitialUri (intent : Intent ? ) {
58
34
if (intent == null ) {
59
- setMessage(null );
60
- return ;
35
+ setMessage(null )
36
+ return
61
37
}
62
- final Parcelable initialUriParcelable = intent.getParcelableExtra(Constants .EXTRA_INITIAL_URI );
63
- if (! ( initialUriParcelable instanceof Uri ) ) {
64
- setMessage(null );
65
- return ;
38
+ val initialUriParcelable = intent.getParcelableExtra< Parcelable > (Constants .EXTRA_INITIAL_URI )
39
+ if (initialUriParcelable !is Uri ) {
40
+ setMessage(null )
41
+ return
66
42
}
67
- setMessage(( Uri ) initialUriParcelable);
43
+ setMessage(initialUriParcelable as Uri ? )
68
44
}
69
45
70
- private void setMessage(@Nullable final Uri initialUri ) {
46
+ private fun setMessage (initialUri : Uri ? ) {
71
47
if (initialUri == null ) {
72
- final String prevVersionFolderPath = Utils .settingsHelper.getString(FOLDER_PATH );
73
- if (TextUtils . isEmpty(prevVersionFolderPath)) {
48
+ val prevVersionFolderPath = Utils .settingsHelper.getString(PreferenceKeys . FOLDER_PATH )
49
+ if (isEmpty(prevVersionFolderPath)) {
74
50
// default message
75
- message .postValue(getApplication().getString(R .string.dir_select_default_message));
76
- prevUri .postValue(null );
77
- return ;
51
+ _message .postValue(getApplication< Application > ().getString(R .string.dir_select_default_message))
52
+ _prevUri .postValue(null )
53
+ return
78
54
}
79
- message .postValue(getApplication().getString(R .string.dir_select_reselect_message));
80
- prevUri .postValue(prevVersionFolderPath);
81
- return ;
55
+ _message .postValue(getApplication< Application > ().getString(R .string.dir_select_reselect_message))
56
+ _prevUri .postValue(prevVersionFolderPath)
57
+ return
82
58
}
83
- final List <UriPermission > existingPermissions = getApplication().getContentResolver().getPersistedUriPermissions();
84
- final boolean anyMatch = existingPermissions.stream().anyMatch(uriPermission -> uriPermission.getUri().equals(initialUri));
85
- final DocumentFile documentFile = DocumentFile .fromSingleUri(getApplication(), initialUri);
86
- String path;
87
- try {
88
- path = URLDecoder .decode(initialUri.toString(), StandardCharsets .UTF_8 .toString());
89
- } catch (UnsupportedEncodingException e) {
90
- path = initialUri.toString();
59
+ val existingPermissions = getApplication<Application >().contentResolver.persistedUriPermissions
60
+ val anyMatch = existingPermissions.stream().anyMatch { uriPermission: UriPermission -> uriPermission.uri == initialUri }
61
+ val documentFile = DocumentFile .fromSingleUri(getApplication(), initialUri)
62
+ val path: String = try {
63
+ URLDecoder .decode(initialUri.toString(), StandardCharsets .UTF_8 .toString())
64
+ } catch (e: UnsupportedEncodingException ) {
65
+ initialUri.toString()
91
66
}
92
67
if (! anyMatch) {
93
- message .postValue(getApplication().getString(R .string.dir_select_permission_revoked_message));
94
- prevUri .postValue(path);
95
- return ;
68
+ _message .postValue(getApplication< Application > ().getString(R .string.dir_select_permission_revoked_message))
69
+ _prevUri .postValue(path)
70
+ return
96
71
}
97
- if (documentFile == null || ! documentFile.exists() || documentFile.lastModified() == 0 ) {
98
- message .postValue(getApplication().getString(R .string.dir_select_folder_not_exist));
99
- prevUri .postValue(path);
72
+ if (documentFile == null || ! documentFile.exists() || documentFile.lastModified() == 0L ) {
73
+ _message .postValue(getApplication< Application > ().getString(R .string.dir_select_folder_not_exist))
74
+ _prevUri .postValue(path)
100
75
}
101
76
}
102
77
103
- public void setupSelectedDir(@NonNull final Intent data) throws DownloadUtils .ReselectDocumentTreeException {
104
- loading.postValue(true );
78
+ @Throws(ReselectDocumentTreeException ::class )
79
+ fun setupSelectedDir (data : Intent ) {
80
+ _loading .postValue(true )
105
81
try {
106
- Utils .setupSelectedDir(getApplication(), data);
107
- message .postValue(getApplication().getString(R .string.dir_select_success_message));
108
- dirSuccess .postValue(true );
82
+ Utils .setupSelectedDir(getApplication(), data)
83
+ _message .postValue(getApplication< Application > ().getString(R .string.dir_select_success_message))
84
+ _dirSuccess .postValue(true )
109
85
} finally {
110
- loading .postValue(false );
86
+ _loading .postValue(false )
111
87
}
112
88
}
113
- }
89
+ }
0 commit comments