Prevent save/discard dialog from appearing when file contents haven't changed after loading

This commit is contained in:
William Brawner 2021-02-21 17:57:41 -07:00
parent c2be2274e8
commit 1522806e62
2 changed files with 47 additions and 27 deletions

View file

@ -71,7 +71,23 @@ class MarkdownTests {
} }
@Test @Test
fun newMarkdownTest() { fun openThenNewMarkdownTest() {
val markdownText = "# UI Testing\n\nThe quick brown fox jumped over the lazy dog."
file.outputStream().writer().use { it.write(markdownText) }
val activityResult = Instrumentation.ActivityResult(RESULT_OK, Intent().apply {
data = Uri.fromFile(file)
})
intending(hasAction(Intent.ACTION_OPEN_DOCUMENT)).respondWith(activityResult)
openActionBarOverflowOrOptionsMenu(getApplicationContext())
onView(withText(R.string.action_open)).perform(click())
openActionBarOverflowOrOptionsMenu(getApplicationContext())
onView(withText(R.string.action_new)).perform(click())
// The dialog to save or discard changes shouldn't be shown here since no edits were made
onView(withId(R.id.markdown_edit)).check(matches(withText("")))
}
@Test
fun editThenNewMarkdownTest() {
onView(withId(R.id.markdown_edit)) onView(withId(R.id.markdown_edit))
.perform(typeText("# UI Testing\n\nThe quick brown fox jumped over the lazy dog.")) .perform(typeText("# UI Testing\n\nThe quick brown fox jumped over the lazy dog."))
openActionBarOverflowOrOptionsMenu(getApplicationContext()) openActionBarOverflowOrOptionsMenu(getApplicationContext())

View file

@ -34,18 +34,7 @@ class EditFragment : Fragment(), ViewPagerPage {
private var markdownEditorScroller: NestedScrollView? = null private var markdownEditorScroller: NestedScrollView? = null
private val viewModel: MarkdownViewModel by viewModels({ requireParentFragment() }) private val viewModel: MarkdownViewModel by viewModels({ requireParentFragment() })
private var readabilityWatcher: TextWatcher? = null private var readabilityWatcher: TextWatcher? = null
private val markdownWatcher = object : TextWatcher {
@SuppressLint("ClickableViewAccessibility")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_edit, container, false)
@SuppressLint("ClickableViewAccessibility")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
markdownEditor = view.findViewById(R.id.markdown_edit)
markdownEditorScroller = view.findViewById(R.id.markdown_edit_container)
markdownEditor?.addTextChangedListener(object : TextWatcher {
private var searchFor = "" private var searchFor = ""
override fun afterTextChanged(s: Editable?) { override fun afterTextChanged(s: Editable?) {
@ -68,8 +57,19 @@ class EditFragment : Fragment(), ViewPagerPage {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
} }
}
}) @SuppressLint("ClickableViewAccessibility")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_edit, container, false)
@SuppressLint("ClickableViewAccessibility")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
markdownEditor = view.findViewById(R.id.markdown_edit)
markdownEditorScroller = view.findViewById(R.id.markdown_edit_container)
markdownEditor?.addTextChangedListener(markdownWatcher)
var touchDown = 0L var touchDown = 0L
var oldX = 0f var oldX = 0f
@ -97,7 +97,11 @@ class EditFragment : Fragment(), ViewPagerPage {
viewModel.editorActions.observe(viewLifecycleOwner, Observer { viewModel.editorActions.observe(viewLifecycleOwner, Observer {
if (it.consumed.getAndSet(true)) return@Observer if (it.consumed.getAndSet(true)) return@Observer
if (it is MarkdownViewModel.EditorAction.Load) { if (it is MarkdownViewModel.EditorAction.Load) {
markdownEditor?.setText(it.markdown) markdownEditor?.apply {
removeTextChangedListener(markdownWatcher)
setText(it.markdown)
addTextChangedListener(markdownWatcher)
}
} }
}) })
lifecycleScope.launch { lifecycleScope.launch {