Merge branch 'desirepath'

# Conflicts:
#	app/src/main/java/io/legado/app/ui/main/MainActivity.kt
pull/31/head
Invinciblelee 6 years ago
commit f908228596
  1. 97
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  2. 6
      app/src/main/java/io/legado/app/ui/search/SearchActivity.kt
  3. 12
      app/src/main/java/io/legado/app/utils/ViewModelExtensions.kt

@ -1,47 +1,26 @@
package io.legado.app.ui.main package io.legado.app.ui.main
import android.Manifest
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.core.view.GravityCompat import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.navigation.NavigationView import com.google.android.material.navigation.NavigationView
import com.jayway.jsonpath.Configuration
import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.Option
import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
import io.legado.app.constant.AppConst.APP_TAG
import io.legado.app.constant.AppConst.RC_IMPORT_YUEDU_DATA
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.ui.replacerule.ReplaceRuleActivity
import io.legado.app.ui.search.SearchActivity import io.legado.app.ui.search.SearchActivity
import io.legado.app.utils.getSdPath import io.legado.app.utils.getViewModel
import io.legado.app.utils.readBool
import io.legado.app.utils.readInt
import io.legado.app.utils.readString
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.* import kotlinx.android.synthetic.main.app_bar_main.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.uiThread
import pub.devrel.easypermissions.AfterPermissionGranted
import pub.devrel.easypermissions.EasyPermissions
import java.io.File
import java.lang.Exception
class MainActivity : BaseActivity<MainDataBinding, MainViewModel>(), NavigationView.OnNavigationItemSelectedListener { class MainActivity : BaseActivity<MainDataBinding, MainViewModel>(), NavigationView.OnNavigationItemSelectedListener {
override val viewModel: MainViewModel override val viewModel: MainViewModel
get() = ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(MainViewModel::class.java) get() = getViewModel(MainViewModel::class.java)
override val layoutID: Int override val layoutID: Int
get() = R.layout.activity_main get() = R.layout.activity_main
private val PERMISSONS = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
override fun onViewModelCreated(viewModel: MainViewModel, savedInstanceState: Bundle?) { override fun onViewModelCreated(viewModel: MainViewModel, savedInstanceState: Bundle?) {
fab.setOnClickListener { startActivity(Intent(this, SearchActivity::class.java)) } fab.setOnClickListener { startActivity(Intent(this, SearchActivity::class.java)) }
@ -79,79 +58,27 @@ class MainActivity : BaseActivity<MainDataBinding, MainViewModel>(), NavigationV
override fun onNavigationItemSelected(item: MenuItem): Boolean { override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here. // Handle navigation view item clicks here.
when (item.itemId) { when (item.itemId) {
R.id.nav_backup -> { R.id.nav_home -> {
// Handle the camera action // Handle the camera action
} }
R.id.nav_import -> { R.id.nav_gallery -> {
} }
R.id.nav_import_old -> importYueDu() R.id.nav_slideshow -> {
R.id.nav_import_github -> {
} }
R.id.nav_replace_rule -> startActivity<ReplaceRuleActivity>() R.id.nav_tools -> {
R.id.nav_send -> {
} }
} R.id.nav_share -> {
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
/*
* import from YueDu backup data
* */
@AfterPermissionGranted(RC_IMPORT_YUEDU_DATA)
fun importYueDu() {
if (!EasyPermissions.hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
EasyPermissions.requestPermissions(this, getString(R.string.perm_request_storage), RC_IMPORT_YUEDU_DATA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
return
}
val yuedu = File(getSdPath(), "YueDu")
val jsonPath = JsonPath.using(Configuration.builder()
.options(Option.SUPPRESS_EXCEPTIONS)
.build())
// Replace rules
val rFile = File(yuedu, "myBookReplaceRule.json")
val replaceRules = mutableListOf<ReplaceRule>()
if (rFile.exists()) try {
val items: List<Map<String, Any>> = jsonPath.parse(rFile.readText()).read("$.*")
for (item in items) {
val jsonItem = jsonPath.parse(item)
val rRule = ReplaceRule()
rRule.name = jsonItem.readString("$.replaceSummary")
rRule.pattern = jsonItem.readString("$.regex")
rRule.replacement = jsonItem.readString("$.replacement")
rRule.isRegex = jsonItem.readBool("$.isRegex")
rRule.scope = jsonItem.readString("$.useTo")
rRule.isEnabled = jsonItem.readBool("$.enable")
rRule.order = jsonItem.readInt("$.serialNumber")
replaceRules.add(rRule)
// Log.e(APP_TAG, rRule.toString())
}
doAsync {
App.db.replaceRuleDao().insert(*replaceRules.toTypedArray())
val count = App.db.replaceRuleDao().all.size
val maxId = App.db.replaceRuleDao().maxOrder
uiThread {
Log.e(APP_TAG, "$count records were inserted to database, and max id is $maxId.")
}
} }
R.id.nav_send -> {
} catch (e: Exception) {
Log.e(APP_TAG, e.localizedMessage)
} }
} }
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
drawerLayout.closeDrawer(GravityCompat.START)
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { return true
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
} }
} }

@ -1,15 +1,15 @@
package io.legado.app.ui.search package io.legado.app.ui.search
import android.os.Bundle import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
import io.legado.app.help.LayoutManager import io.legado.app.search.SearchDataBinding
import io.legado.app.utils.getViewModel
class SearchActivity : BaseActivity<SearchDataBinding, SearchViewModel>() { class SearchActivity : BaseActivity<SearchDataBinding, SearchViewModel>() {
override val viewModel: SearchViewModel override val viewModel: SearchViewModel
get() = ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(SearchViewModel::class.java) get() = getViewModel(SearchViewModel::class.java)
override val layoutID: Int override val layoutID: Int
get() = R.layout.activity_search get() = R.layout.activity_search

@ -0,0 +1,12 @@
package io.legado.app.utils
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
fun <T : ViewModel> AppCompatActivity.getViewModel(clazz: Class<T>) = ViewModelProviders.of(this).get(clazz)
fun <T : ViewModel> Fragment.getViewModel(clazz: Class<T>) = ViewModelProviders.of(this).get(clazz)
fun <T : ViewModel> Fragment.getViewModelOfActivity(clazz: Class<T>) = ViewModelProviders.of(requireActivity()).get(clazz)
Loading…
Cancel
Save