From 7449a34bf8633cef2eafa8575daef039381b4589 Mon Sep 17 00:00:00 2001 From: Invinciblelee <1760316362@qq.com> Date: Thu, 23 May 2019 13:03:47 +0800 Subject: [PATCH] improve BaseActivity --- .../java/io/legado/app/base/BaseActivity.kt | 4 +- .../java/io/legado/app/data/AppDatabase.kt | 2 +- .../java/io/legado/app/data/dao/BookDao.kt | 2 +- .../io/legado/app/data/entities/SearchBook.kt | 2 + .../java/io/legado/app/help/LayoutManager.kt | 67 +++++++++++++++++++ .../io/legado/app/ui/main/MainActivity.kt | 6 +- .../io/legado/app/ui/search/SearchActivity.kt | 20 ++++-- .../legado/app/ui/search/SearchViewModel.kt | 6 ++ app/src/main/res/layout/activity_search.xml | 47 ++++++++++--- app/src/main/res/values/styles.xml | 2 +- 10 files changed, 140 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/io/legado/app/data/entities/SearchBook.kt create mode 100644 app/src/main/java/io/legado/app/help/LayoutManager.kt create mode 100644 app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index 90c1c013f..f2f2aab07 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -22,7 +22,9 @@ abstract class BaseActivity : AppCompatAct onViewModelCreated(viewModel, savedInstanceState) } - abstract fun onViewModelCreated(viewModel: VM, savedInstanceState: Bundle?) + open fun onViewModelCreated(viewModel: VM, savedInstanceState: Bundle?){ + + } override fun onOptionsItemSelected(item: MenuItem?): Boolean { item?.let { diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index 5fb47987f..327651493 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -13,7 +13,7 @@ import io.legado.app.data.entities.ReplaceRule import javax.xml.transform.Source -@Database(entities = [Book::class, Chapter::class, ReplaceRule::class, Source::class], version = 1, exportSchema = true) +@Database(entities = [Book::class, Chapter::class, ReplaceRule::class], version = 1, exportSchema = true) // @TypeConverters(Converters::class) abstract class AppDatabase : RoomDatabase() { diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index b6ad0ba40..aedc5dcab 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -13,7 +13,7 @@ interface BookDao { @Query("SELECT * FROM books WHERE `group` = :group") fun observeByGroup(group: Int): DataSource.Factory - @Query("SELECT url FROM books WHERE `group` = :group") + @Query("SELECT tocUrl FROM books WHERE `group` = :group") fun observeUrlsByGroup(group: Int): LiveData> } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/SearchBook.kt b/app/src/main/java/io/legado/app/data/entities/SearchBook.kt new file mode 100644 index 000000000..feecd7295 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/entities/SearchBook.kt @@ -0,0 +1,2 @@ +package io.legado.app.data.entities + diff --git a/app/src/main/java/io/legado/app/help/LayoutManager.kt b/app/src/main/java/io/legado/app/help/LayoutManager.kt new file mode 100644 index 000000000..9fc551222 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/LayoutManager.kt @@ -0,0 +1,67 @@ +package io.legado.app.help + +import androidx.annotation.IntDef +import androidx.recyclerview.widget.GridLayoutManager +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.StaggeredGridLayoutManager + +class LayoutManager private constructor() { + + interface LayoutManagerFactory { + fun create(recyclerView: RecyclerView): RecyclerView.LayoutManager + } + + @IntDef(LinearLayoutManager.HORIZONTAL, LinearLayoutManager.VERTICAL) + @Retention(AnnotationRetention.SOURCE) + annotation class Orientation + + companion object { + + + fun linear(): LayoutManagerFactory { + return object : LayoutManagerFactory { + override fun create(recyclerView: RecyclerView): RecyclerView.LayoutManager { + return LinearLayoutManager(recyclerView.context) + } + } + } + + + fun linear(@Orientation orientation: Int, reverseLayout: Boolean): LayoutManagerFactory { + return object : LayoutManagerFactory { + override fun create(recyclerView: RecyclerView): RecyclerView.LayoutManager { + return LinearLayoutManager(recyclerView.context, orientation, reverseLayout) + } + } + } + + + fun grid(spanCount: Int): LayoutManagerFactory { + return object : LayoutManagerFactory { + override fun create(recyclerView: RecyclerView): RecyclerView.LayoutManager { + return GridLayoutManager(recyclerView.context, spanCount) + } + } + } + + + fun grid(spanCount: Int, @Orientation orientation: Int, reverseLayout: Boolean): LayoutManagerFactory { + return object : LayoutManagerFactory { + override fun create(recyclerView: RecyclerView): RecyclerView.LayoutManager { + return GridLayoutManager(recyclerView.context, spanCount, orientation, reverseLayout) + } + } + } + + + fun staggeredGrid(spanCount: Int, @Orientation orientation: Int): LayoutManagerFactory { + return object : LayoutManagerFactory { + override fun create(recyclerView: RecyclerView): RecyclerView.LayoutManager { + return StaggeredGridLayoutManager(spanCount, orientation) + } + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 2b0156d5d..47a023576 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -1,5 +1,6 @@ package io.legado.app.ui.main +import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -7,12 +8,15 @@ import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AppCompatActivity import androidx.core.view.GravityCompat import androidx.drawerlayout.widget.DrawerLayout +import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProviders import com.google.android.material.navigation.NavigationView import io.legado.app.R +import io.legado.app.ui.search.SearchActivity import io.legado.app.utils.longSnackbar import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.app_bar_main.* +import org.jetbrains.anko.startActivity class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { @@ -22,7 +26,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte setContentView(R.layout.activity_main) setSupportActionBar(toolbar) - fab.setOnClickListener { it.longSnackbar(R.string.app_name) } + fab.setOnClickListener { startActivity(Intent(this, SearchActivity::class.java)) } val toggle = ActionBarDrawerToggle( this, drawer_layout, toolbar, diff --git a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt index 84ff4cc97..f583abc6f 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt @@ -1,13 +1,23 @@ package io.legado.app.ui.search import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.ViewModelProvider import io.legado.app.R +import io.legado.app.base.BaseActivity +import io.legado.app.search.SearchDataBinding + +class SearchActivity : BaseActivity() { + + override val viewModel: SearchViewModel + get() = ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(SearchViewModel::class.java) + + override val layoutID: Int + get() = R.layout.activity_search + + override fun onViewModelCreated(viewModel: SearchViewModel, savedInstanceState: Bundle?) { + dataBinding.searchViewModel = viewModel -class SearchActivity : AppCompatActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_search) } + } diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt new file mode 100644 index 000000000..a9a847a5b --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -0,0 +1,6 @@ +package io.legado.app.ui.search + +import android.app.Application +import androidx.lifecycle.AndroidViewModel + +class SearchViewModel(application: Application) : AndroidViewModel(application) diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml index e96e6ba5b..486b05137 100644 --- a/app/src/main/res/layout/activity_search.xml +++ b/app/src/main/res/layout/activity_search.xml @@ -1,9 +1,40 @@ - - - + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 352e2f8cf..09ffdee29 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,7 +1,7 @@ -