1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-21 14:17:29 +00:00

Convert DirectorySelectActivity to kotlin

This commit is contained in:
Ammar Githam 2021-07-13 23:57:48 +09:00
parent 02426a41df
commit 854b584356

View File

@ -1,129 +1,132 @@
package awais.instagrabber.activities;
package awais.instagrabber.activities
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.util.Log;
import android.view.View;
import android.annotation.SuppressLint
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.DocumentsContract
import android.util.Log
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;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
class DirectorySelectActivity : BaseLanguageActivity() {
private var initialUri: Uri? = null
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
private lateinit var binding: ActivityDirectorySelectBinding
private lateinit var viewModel: DirectorySelectActivityViewModel
import awais.instagrabber.R;
import awais.instagrabber.databinding.ActivityDirectorySelectBinding;
import awais.instagrabber.dialogs.ConfirmDialogFragment;
import awais.instagrabber.utils.AppExecutors;
import awais.instagrabber.viewmodels.DirectorySelectActivityViewModel;
public class DirectorySelectActivity extends BaseLanguageActivity {
private static final String TAG = DirectorySelectActivity.class.getSimpleName();
public static final int SELECT_DIR_REQUEST_CODE = 0x01;
private static final int ERROR_REQUEST_CODE = 0x02;
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()));
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()
binding.selectDir.setOnClickListener { openDirectoryChooser() }
initialUri = intent.getParcelableExtra(Constants.EXTRA_INITIAL_URI)
}
private void setupObservers() {
viewModel.getMessage().observe(this, message -> binding.message.setText(message));
viewModel.getPrevUri().observe(this, prevUri -> {
private fun setupObservers() {
viewModel.message.observe(this, { message: String? -> binding.message.text = message })
viewModel.prevUri.observe(this, { prevUri: String? ->
if (prevUri == null) {
binding.prevUri.setVisibility(View.GONE);
binding.message2.setVisibility(View.GONE);
return;
binding.prevUri.visibility = View.GONE
binding.message2.visibility = View.GONE
return@observe
}
binding.prevUri.setText(prevUri);
binding.prevUri.setVisibility(View.VISIBLE);
binding.message2.setVisibility(View.VISIBLE);
});
viewModel.getDirSuccess().observe(this, success -> binding.selectDir.setVisibility(success ? View.GONE : View.VISIBLE));
viewModel.isLoading().observe(this, loading -> {
binding.message.setVisibility(loading ? View.GONE : View.VISIBLE);
binding.loadingIndicator.setVisibility(loading ? View.VISIBLE : View.GONE);
});
binding.prevUri.text = prevUri
binding.prevUri.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.isLoading.observe(this, { loading: Boolean ->
binding.message.visibility = if (loading) View.GONE else View.VISIBLE
binding.loadingIndicator.visibility = if (loading) View.VISIBLE else View.GONE
})
}
private void openDirectoryChooser() {
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
private fun openDirectoryChooser() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
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 {
startActivityForResult(intent, SELECT_DIR_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
Log.e(TAG, "openDirectoryChooser: ", e);
showErrorDialog(getString(R.string.no_directory_picker_activity));
} catch (Exception e) {
Log.e(TAG, "openDirectoryChooser: ", e);
startActivityForResult(intent, SELECT_DIR_REQUEST_CODE)
} catch (e: ActivityNotFoundException) {
Log.e(TAG, "openDirectoryChooser: ", e)
showErrorDialog(getString(R.string.no_directory_picker_activity))
} catch (e: Exception) {
Log.e(TAG, "openDirectoryChooser: ", e)
}
}
@SuppressLint("StringFormatInvalid")
@Override
protected void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode != SELECT_DIR_REQUEST_CODE) return;
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode != SELECT_DIR_REQUEST_CODE) return
if (resultCode != RESULT_OK) {
showErrorDialog(getString(R.string.select_a_folder));
return;
showErrorDialog(getString(R.string.select_a_folder))
return
}
if (data == null || data.getData() == null) {
showErrorDialog(getString(R.string.select_a_folder));
return;
if (data == null || data.data == null) {
showErrorDialog(getString(R.string.select_a_folder))
return
}
if (!"com.android.externalstorage.documents".equals(data.getData().getAuthority())) {
showErrorDialog(getString(R.string.dir_select_no_download_folder, data.getData().getAuthority()));
return;
val authority = data.data?.authority
if ("com.android.externalstorage.documents" != authority) {
showErrorDialog(getString(R.string.dir_select_no_download_folder, authority))
return
}
AppExecutors.INSTANCE.getMainThread().execute(() -> {
mainThread.execute({
try {
viewModel.setupSelectedDir(data);
final Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
} catch (Exception e) {
viewModel.setupSelectedDir(data)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
} catch (e: Exception) {
// Should not come to this point.
// If it does, we have to show this error to the user so that they can report it.
try (final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw)) {
e.printStackTrace(pw);
showErrorDialog("Please report this error to the developers:\n\n" + sw.toString());
} catch (IOException ioException) {
Log.e(TAG, "onActivityResult: ", ioException);
try {
StringWriter().use { sw ->
PrintWriter(sw).use { pw ->
e.printStackTrace(pw)
showErrorDialog("Please report this error to the developers:\n\n$sw")
}
}
} catch (ioException: IOException) {
Log.e(TAG, "onActivityResult: ", ioException)
}
}
}, 500);
}, 500)
}
private void showErrorDialog(@NonNull final String message) {
final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance(
ERROR_REQUEST_CODE,
R.string.error,
message,
R.string.ok,
0,
0
);
dialogFragment.show(getSupportFragmentManager(), ConfirmDialogFragment.class.getSimpleName());
private fun showErrorDialog(message: String) {
val dialogFragment = ConfirmDialogFragment.newInstance(
ERROR_REQUEST_CODE,
R.string.error,
message,
R.string.ok,
0,
0
)
dialogFragment.show(supportFragmentManager, ConfirmDialogFragment::class.java.simpleName)
}
}
companion object {
const val SELECT_DIR_REQUEST_CODE = 0x01
private const val ERROR_REQUEST_CODE = 0x02
}
}