Skip to content

Commit

Permalink
Pass uri of saved forms using activity result
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed May 26, 2023
1 parent 6c04496 commit 0bfc7ac
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@
import org.odk.collect.permissions.PermissionsChecker;
import org.odk.collect.permissions.PermissionsProvider;
import org.odk.collect.settings.SettingsProvider;
import org.odk.collect.settings.keys.MetaKeys;
import org.odk.collect.settings.keys.ProjectKeys;
import org.odk.collect.shared.strings.Md5;
import org.odk.collect.strings.localization.LocalizedActivity;
Expand Down Expand Up @@ -2238,7 +2237,6 @@ private void finishAndReturnInstance() {
}

if (uri != null) {
settingsProvider.getMetaSettings().save(MetaKeys.LAST_SAVED_FORM_URI, uri.toString());
setResult(RESULT_OK, new Intent().setData(uri));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.loader.app.LoaderManager;
Expand Down Expand Up @@ -72,6 +74,11 @@ public class InstanceChooserList extends InstanceListActivity implements Adapter
@Inject
FormsRepositoryProvider formsRepositoryProvider;

private final ActivityResultLauncher<Intent> formLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
setResult(RESULT_OK, result.getData());
finish();
});

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -156,6 +163,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
if (Intent.ACTION_PICK.equals(action)) {
// caller is waiting on a picked form
setResult(RESULT_OK, new Intent().setData(instanceUri));
finish();
} else {
// the form can be edited if it is incomplete or if, when it was
// marked as complete, it was determined that it could be edited
Expand All @@ -180,12 +188,13 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
if (formMode == null || ApplicationConstants.FormModes.EDIT_SAVED.equalsIgnoreCase(formMode)) {
logFormEdit(c);
intent.putExtra(ApplicationConstants.BundleKeys.FORM_MODE, ApplicationConstants.FormModes.EDIT_SAVED);
formLauncher.launch(intent);
} else {
intent.putExtra(ApplicationConstants.BundleKeys.FORM_MODE, ApplicationConstants.FormModes.VIEW_SENT);
startActivity(intent);
finish();
}
startActivity(intent);
}
finish();
} else {
TextView disabledCause = view.findViewById(R.id.form_subtitle2);
Toast.makeText(this, disabledCause.getText(), Toast.LENGTH_SHORT).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Bundle
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.recyclerview.widget.RecyclerView
import org.odk.collect.android.R
Expand Down Expand Up @@ -36,6 +37,12 @@ class BlankFormListActivity : LocalizedActivity(), OnFormItemClickListener {

private val adapter: BlankFormListAdapter = BlankFormListAdapter(this)

private val formLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
setResult(RESULT_OK, it.data)
finish()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
DaggerUtils.getComponent(this).inject(this)
Expand All @@ -55,11 +62,11 @@ class BlankFormListActivity : LocalizedActivity(), OnFormItemClickListener {
if (Intent.ACTION_PICK == intent.action) {
// caller is waiting on a picked form
setResult(RESULT_OK, Intent().setData(formUri))
finish()
} else {
// caller wants to view/edit a form, so launch FormFillingActivity
startActivity(FormFillingIntentFactory.newInstanceIntent(this, formUri))
formLauncher.launch(FormFillingIntentFactory.newInstanceIntent(this, formUri))
}
finish()
}

override fun onMapButtonClick(id: Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.widget.Toolbar
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.ViewModelProvider
Expand Down Expand Up @@ -38,7 +39,6 @@ import org.odk.collect.androidshared.ui.multiclicksafe.MultiClickGuard.allowClic
import org.odk.collect.crashhandler.CrashHandler
import org.odk.collect.projects.Project.Saved
import org.odk.collect.settings.SettingsProvider
import org.odk.collect.settings.keys.MetaKeys
import org.odk.collect.settings.keys.ProjectKeys
import org.odk.collect.strings.localization.LocalizedActivity
import javax.inject.Inject
Expand All @@ -57,6 +57,11 @@ class MainMenuActivity : LocalizedActivity() {
private lateinit var mainMenuViewModel: MainMenuViewModel
private lateinit var currentProjectViewModel: CurrentProjectViewModel

private val formLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
displayFormSavedSnackbar(it.data?.data)
}

override fun onCreate(savedInstanceState: Bundle?) {
initSplashScreen()
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -102,12 +107,6 @@ class MainMenuActivity : LocalizedActivity() {
mainMenuViewModel.refreshInstances()
setButtonsVisibility()
manageGoogleDriveDeprecationBanner()

val formSavedUri = settingsProvider.getMetaSettings().getString(MetaKeys.LAST_SAVED_FORM_URI)
if (formSavedUri != null) {
displayFormSavedSnackbar(Uri.parse(formSavedUri))
settingsProvider.getMetaSettings().remove(MetaKeys.LAST_SAVED_FORM_URI)
}
}

private fun setButtonsVisibility() {
Expand Down Expand Up @@ -178,11 +177,13 @@ class MainMenuActivity : LocalizedActivity() {

private fun initButtons() {
binding.enterData.setOnClickListener {
startActivity(Intent(this, BlankFormListActivity::class.java))
formLauncher.launch(
Intent(this, BlankFormListActivity::class.java)
)
}

binding.reviewData.setOnClickListener {
startActivity(
formLauncher.launch(
Intent(this, InstanceChooserList::class.java).apply {
putExtra(
ApplicationConstants.BundleKeys.FORM_MODE,
Expand Down Expand Up @@ -301,15 +302,19 @@ class MainMenuActivity : LocalizedActivity() {
}
}

private fun displayFormSavedSnackbar(uri: Uri) {
private fun displayFormSavedSnackbar(uri: Uri?) {
if (uri == null) {
return
}

val formSavedSnackbarType = mainMenuViewModel.getFormSavedSnackbarType(uri)

formSavedSnackbarType?.let { it ->
SnackbarUtils.showLongSnackbar(
binding.root,
getString(it.message),
action = SnackbarUtils.Action(getString(it.actionName)) {
startActivity(FormFillingIntentFactory.editInstanceIntent(this, uri))
formLauncher.launch(FormFillingIntentFactory.editInstanceIntent(this, uri))
},
displayDismissButton = true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ object MetaKeys {
const val EXISTING_PROJECT_IMPORTED = "existing_project_imported"
const val LAST_LAUNCHED = "last_launched"
const val LAST_USED_PEN_COLOR = "last_used_pen_color"
const val LAST_SAVED_FORM_URI = "last_saved_form_uri"
}

0 comments on commit 0bfc7ac

Please sign in to comment.