Prevent save/discard dialog from appearing when file contents haven't changed after loading
This commit is contained in:
parent
c2be2274e8
commit
1522806e62
2 changed files with 47 additions and 27 deletions
|
@ -71,7 +71,23 @@ class MarkdownTests {
|
|||
}
|
||||
|
||||
@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))
|
||||
.perform(typeText("# UI Testing\n\nThe quick brown fox jumped over the lazy dog."))
|
||||
openActionBarOverflowOrOptionsMenu(getApplicationContext())
|
||||
|
|
|
@ -34,6 +34,30 @@ class EditFragment : Fragment(), ViewPagerPage {
|
|||
private var markdownEditorScroller: NestedScrollView? = null
|
||||
private val viewModel: MarkdownViewModel by viewModels({ requireParentFragment() })
|
||||
private var readabilityWatcher: TextWatcher? = null
|
||||
private val markdownWatcher = object : TextWatcher {
|
||||
private var searchFor = ""
|
||||
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
val searchText = s.toString().trim()
|
||||
if (searchText == searchFor)
|
||||
return
|
||||
|
||||
searchFor = searchText
|
||||
|
||||
lifecycleScope.launch {
|
||||
delay(50)
|
||||
if (searchText != searchFor)
|
||||
return@launch
|
||||
viewModel.updateMarkdown(searchText)
|
||||
}
|
||||
}
|
||||
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
||||
}
|
||||
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
||||
|
@ -45,31 +69,7 @@ class EditFragment : Fragment(), ViewPagerPage {
|
|||
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 = ""
|
||||
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
val searchText = s.toString().trim()
|
||||
if (searchText == searchFor)
|
||||
return
|
||||
|
||||
searchFor = searchText
|
||||
|
||||
lifecycleScope.launch {
|
||||
delay(50)
|
||||
if (searchText != searchFor)
|
||||
return@launch
|
||||
viewModel.updateMarkdown(searchText)
|
||||
}
|
||||
}
|
||||
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
||||
}
|
||||
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||
}
|
||||
|
||||
})
|
||||
markdownEditor?.addTextChangedListener(markdownWatcher)
|
||||
|
||||
var touchDown = 0L
|
||||
var oldX = 0f
|
||||
|
@ -97,7 +97,11 @@ class EditFragment : Fragment(), ViewPagerPage {
|
|||
viewModel.editorActions.observe(viewLifecycleOwner, Observer {
|
||||
if (it.consumed.getAndSet(true)) return@Observer
|
||||
if (it is MarkdownViewModel.EditorAction.Load) {
|
||||
markdownEditor?.setText(it.markdown)
|
||||
markdownEditor?.apply {
|
||||
removeTextChangedListener(markdownWatcher)
|
||||
setText(it.markdown)
|
||||
addTextChangedListener(markdownWatcher)
|
||||
}
|
||||
}
|
||||
})
|
||||
lifecycleScope.launch {
|
||||
|
|
Loading…
Reference in a new issue