adding basic text highlighting in the editor

This commit is contained in:
tibbi 2018-08-07 15:51:22 +02:00
parent 5112d3a958
commit cbf6215f01
5 changed files with 65 additions and 3 deletions

View file

@ -45,7 +45,7 @@ ext {
}
dependencies {
implementation 'com.simplemobiletools:commons:4.6.0'
implementation 'com.simplemobiletools:commons:4.6.2'
implementation files('../libs/RootTools.jar')

View file

@ -52,6 +52,15 @@
<activity
android:name=".activities.ReadTextActivity"
android:label="@string/file_editor">
<meta-data
android:name="android.app.default_searchable"
android:resource="@xml/searchable"/>
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>

View file

@ -1,7 +1,11 @@
package com.simplemobiletools.filemanager.activities
import android.app.SearchManager
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.support.v4.view.MenuItemCompat
import android.support.v7.widget.SearchView
import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.commons.extensions.*
@ -17,6 +21,10 @@ import java.io.File
class ReadTextActivity : SimpleActivity() {
private var filePath = ""
private var originalText = ""
private var isSearchOpen = false
private var searchMenuItem: MenuItem? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -35,6 +43,7 @@ class ReadTextActivity : SimpleActivity() {
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_editor, menu)
setupSearch(menu)
return true
}
@ -47,6 +56,43 @@ class ReadTextActivity : SimpleActivity() {
return true
}
private fun setupSearch(menu: Menu) {
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
searchMenuItem = menu.findItem(R.id.search)
(searchMenuItem!!.actionView as SearchView).apply {
setSearchableInfo(searchManager.getSearchableInfo(componentName))
isSubmitButtonEnabled = false
setOnQueryTextListener(object : android.support.v7.widget.SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String) = false
override fun onQueryTextChange(newText: String): Boolean {
if (isSearchOpen) {
searchQueryChanged(newText)
}
return true
}
})
}
MenuItemCompat.setOnActionExpandListener(searchMenuItem, object : MenuItemCompat.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
isSearchOpen = true
searchQueryChanged("")
return true
}
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
isSearchOpen = false
return true
}
})
}
private fun searchQueryChanged(text: String) {
val textToHighlight = if (text.length < 2) "" else text
read_text_view.setText(originalText.highlightTextPart(textToHighlight, getAdjustedPrimaryColor(), true))
}
private fun saveText() {
if (filePath.isEmpty()) {
filePath = getRealPathFromURI(intent.data) ?: ""
@ -78,7 +124,7 @@ class ReadTextActivity : SimpleActivity() {
return
}
val text = if (uri.scheme == "file") {
originalText = if (uri.scheme == "file") {
filePath = uri.path
val file = File(filePath)
if (file.exists()) {
@ -97,6 +143,6 @@ class ReadTextActivity : SimpleActivity() {
}
}
read_text_view.setText(text)
read_text_view.setText(originalText)
}
}

View file

@ -11,6 +11,7 @@
android:layout_height="wrap_content"
android:background="@null"
android:padding="@dimen/medium_margin"
android:inputType="textMultiLine|textNoSuggestions"
android:textCursorDrawable="@null"
android:textSize="@dimen/smaller_text_size"/>

View file

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
<item
android:id="@+id/menu_save"
android:icon="@drawable/ic_save"