mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-21 22:27:29 +00:00
Convert DirectorySelectActivity to kotlin
This commit is contained in:
parent
02426a41df
commit
854b584356
@ -1,129 +1,132 @@
|
|||||||
package awais.instagrabber.activities;
|
package awais.instagrabber.activities
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint
|
||||||
import android.content.ActivityNotFoundException;
|
import android.content.ActivityNotFoundException
|
||||||
import android.content.Intent;
|
import android.content.Intent
|
||||||
import android.net.Uri;
|
import android.net.Uri
|
||||||
import android.os.Build;
|
import android.os.Build
|
||||||
import android.os.Bundle;
|
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 awais.instagrabber.R
|
||||||
|
import awais.instagrabber.databinding.ActivityDirectorySelectBinding
|
||||||
|
import awais.instagrabber.dialogs.ConfirmDialogFragment
|
||||||
|
import awais.instagrabber.utils.AppExecutors.mainThread
|
||||||
|
import awais.instagrabber.utils.Constants
|
||||||
|
import awais.instagrabber.utils.extensions.TAG
|
||||||
|
import awais.instagrabber.viewmodels.DirectorySelectActivityViewModel
|
||||||
|
import java.io.IOException
|
||||||
|
import java.io.PrintWriter
|
||||||
|
import java.io.StringWriter
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
class DirectorySelectActivity : BaseLanguageActivity() {
|
||||||
import androidx.annotation.Nullable;
|
private var initialUri: Uri? = null
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
private lateinit var binding: ActivityDirectorySelectBinding
|
||||||
import java.io.PrintWriter;
|
private lateinit var viewModel: DirectorySelectActivityViewModel
|
||||||
import java.io.StringWriter;
|
|
||||||
|
|
||||||
import awais.instagrabber.R;
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
import awais.instagrabber.databinding.ActivityDirectorySelectBinding;
|
super.onCreate(savedInstanceState)
|
||||||
import awais.instagrabber.dialogs.ConfirmDialogFragment;
|
binding = ActivityDirectorySelectBinding.inflate(layoutInflater)
|
||||||
import awais.instagrabber.utils.AppExecutors;
|
setContentView(binding.root)
|
||||||
import awais.instagrabber.viewmodels.DirectorySelectActivityViewModel;
|
viewModel = ViewModelProvider(this).get(DirectorySelectActivityViewModel::class.java)
|
||||||
|
val intent = intent
|
||||||
public class DirectorySelectActivity extends BaseLanguageActivity {
|
viewModel.setInitialUri(intent)
|
||||||
private static final String TAG = DirectorySelectActivity.class.getSimpleName();
|
setupObservers()
|
||||||
public static final int SELECT_DIR_REQUEST_CODE = 0x01;
|
binding.selectDir.setOnClickListener { openDirectoryChooser() }
|
||||||
private static final int ERROR_REQUEST_CODE = 0x02;
|
initialUri = intent.getParcelableExtra(Constants.EXTRA_INITIAL_URI)
|
||||||
|
|
||||||
private Uri initialUri;
|
|
||||||
private ActivityDirectorySelectBinding binding;
|
|
||||||
private DirectorySelectActivityViewModel viewModel;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCreate(@Nullable final Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
binding = ActivityDirectorySelectBinding.inflate(getLayoutInflater());
|
|
||||||
setContentView(binding.getRoot());
|
|
||||||
viewModel = new ViewModelProvider(this).get(DirectorySelectActivityViewModel.class);
|
|
||||||
setupObservers();
|
|
||||||
binding.selectDir.setOnClickListener(v -> openDirectoryChooser());
|
|
||||||
AppExecutors.INSTANCE.getMainThread().execute(() -> viewModel.setInitialUri(getIntent()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupObservers() {
|
private fun setupObservers() {
|
||||||
viewModel.getMessage().observe(this, message -> binding.message.setText(message));
|
viewModel.message.observe(this, { message: String? -> binding.message.text = message })
|
||||||
viewModel.getPrevUri().observe(this, prevUri -> {
|
viewModel.prevUri.observe(this, { prevUri: String? ->
|
||||||
if (prevUri == null) {
|
if (prevUri == null) {
|
||||||
binding.prevUri.setVisibility(View.GONE);
|
binding.prevUri.visibility = View.GONE
|
||||||
binding.message2.setVisibility(View.GONE);
|
binding.message2.visibility = View.GONE
|
||||||
return;
|
return@observe
|
||||||
}
|
}
|
||||||
binding.prevUri.setText(prevUri);
|
binding.prevUri.text = prevUri
|
||||||
binding.prevUri.setVisibility(View.VISIBLE);
|
binding.prevUri.visibility = View.VISIBLE
|
||||||
binding.message2.setVisibility(View.VISIBLE);
|
binding.message2.visibility = View.VISIBLE
|
||||||
});
|
})
|
||||||
viewModel.getDirSuccess().observe(this, success -> binding.selectDir.setVisibility(success ? View.GONE : View.VISIBLE));
|
viewModel.dirSuccess.observe(this, { success: Boolean -> binding.selectDir.visibility = if (success) View.GONE else View.VISIBLE })
|
||||||
viewModel.isLoading().observe(this, loading -> {
|
viewModel.isLoading.observe(this, { loading: Boolean ->
|
||||||
binding.message.setVisibility(loading ? View.GONE : View.VISIBLE);
|
binding.message.visibility = if (loading) View.GONE else View.VISIBLE
|
||||||
binding.loadingIndicator.setVisibility(loading ? View.VISIBLE : View.GONE);
|
binding.loadingIndicator.visibility = if (loading) View.VISIBLE else View.GONE
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openDirectoryChooser() {
|
private fun openDirectoryChooser() {
|
||||||
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && initialUri != null) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && initialUri != null) {
|
||||||
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, initialUri);
|
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, initialUri)
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
startActivityForResult(intent, SELECT_DIR_REQUEST_CODE);
|
startActivityForResult(intent, SELECT_DIR_REQUEST_CODE)
|
||||||
} catch (ActivityNotFoundException e) {
|
} catch (e: ActivityNotFoundException) {
|
||||||
Log.e(TAG, "openDirectoryChooser: ", e);
|
Log.e(TAG, "openDirectoryChooser: ", e)
|
||||||
showErrorDialog(getString(R.string.no_directory_picker_activity));
|
showErrorDialog(getString(R.string.no_directory_picker_activity))
|
||||||
} catch (Exception e) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "openDirectoryChooser: ", e);
|
Log.e(TAG, "openDirectoryChooser: ", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("StringFormatInvalid")
|
@SuppressLint("StringFormatInvalid")
|
||||||
@Override
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
protected void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) {
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
if (requestCode != SELECT_DIR_REQUEST_CODE) return
|
||||||
if (requestCode != SELECT_DIR_REQUEST_CODE) return;
|
|
||||||
if (resultCode != RESULT_OK) {
|
if (resultCode != RESULT_OK) {
|
||||||
showErrorDialog(getString(R.string.select_a_folder));
|
showErrorDialog(getString(R.string.select_a_folder))
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
if (data == null || data.getData() == null) {
|
if (data == null || data.data == null) {
|
||||||
showErrorDialog(getString(R.string.select_a_folder));
|
showErrorDialog(getString(R.string.select_a_folder))
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
if (!"com.android.externalstorage.documents".equals(data.getData().getAuthority())) {
|
val authority = data.data?.authority
|
||||||
showErrorDialog(getString(R.string.dir_select_no_download_folder, data.getData().getAuthority()));
|
if ("com.android.externalstorage.documents" != authority) {
|
||||||
return;
|
showErrorDialog(getString(R.string.dir_select_no_download_folder, authority))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
mainThread.execute({
|
||||||
try {
|
try {
|
||||||
viewModel.setupSelectedDir(data);
|
viewModel.setupSelectedDir(data)
|
||||||
final Intent intent = new Intent(this, MainActivity.class);
|
val intent = Intent(this, MainActivity::class.java)
|
||||||
startActivity(intent);
|
startActivity(intent)
|
||||||
finish();
|
finish()
|
||||||
} catch (Exception e) {
|
} catch (e: Exception) {
|
||||||
// Should not come to this point.
|
// Should not come to this point.
|
||||||
// If it does, we have to show this error to the user so that they can report it.
|
// If it does, we have to show this error to the user so that they can report it.
|
||||||
try (final StringWriter sw = new StringWriter();
|
try {
|
||||||
final PrintWriter pw = new PrintWriter(sw)) {
|
StringWriter().use { sw ->
|
||||||
e.printStackTrace(pw);
|
PrintWriter(sw).use { pw ->
|
||||||
showErrorDialog("Please report this error to the developers:\n\n" + sw.toString());
|
e.printStackTrace(pw)
|
||||||
} catch (IOException ioException) {
|
showErrorDialog("Please report this error to the developers:\n\n$sw")
|
||||||
Log.e(TAG, "onActivityResult: ", ioException);
|
}
|
||||||
|
}
|
||||||
|
} catch (ioException: IOException) {
|
||||||
|
Log.e(TAG, "onActivityResult: ", ioException)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 500);
|
}, 500)
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showErrorDialog(@NonNull final String message) {
|
private fun showErrorDialog(message: String) {
|
||||||
final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance(
|
val dialogFragment = ConfirmDialogFragment.newInstance(
|
||||||
ERROR_REQUEST_CODE,
|
ERROR_REQUEST_CODE,
|
||||||
R.string.error,
|
R.string.error,
|
||||||
message,
|
message,
|
||||||
R.string.ok,
|
R.string.ok,
|
||||||
0,
|
0,
|
||||||
0
|
0
|
||||||
);
|
)
|
||||||
dialogFragment.show(getSupportFragmentManager(), ConfirmDialogFragment.class.getSimpleName());
|
dialogFragment.show(supportFragmentManager, ConfirmDialogFragment::class.java.simpleName)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val SELECT_DIR_REQUEST_CODE = 0x01
|
||||||
|
private const val ERROR_REQUEST_CODE = 0x02
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user