improve BaseActivity

pull/30/head
Invinciblelee 6 years ago
parent 2734464f5b
commit 7449a34bf8
  1. 4
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  2. 2
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  3. 2
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  4. 2
      app/src/main/java/io/legado/app/data/entities/SearchBook.kt
  5. 67
      app/src/main/java/io/legado/app/help/LayoutManager.kt
  6. 6
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  7. 20
      app/src/main/java/io/legado/app/ui/search/SearchActivity.kt
  8. 6
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt
  9. 31
      app/src/main/res/layout/activity_search.xml
  10. 2
      app/src/main/res/values/styles.xml

@ -22,7 +22,9 @@ abstract class BaseActivity<BD : ViewDataBinding, VM : ViewModel> : AppCompatAct
onViewModelCreated(viewModel, savedInstanceState) onViewModelCreated(viewModel, savedInstanceState)
} }
abstract fun onViewModelCreated(viewModel: VM, savedInstanceState: Bundle?) open fun onViewModelCreated(viewModel: VM, savedInstanceState: Bundle?){
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean { override fun onOptionsItemSelected(item: MenuItem?): Boolean {
item?.let { item?.let {

@ -13,7 +13,7 @@ import io.legado.app.data.entities.ReplaceRule
import javax.xml.transform.Source 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) // @TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {

@ -13,7 +13,7 @@ interface BookDao {
@Query("SELECT * FROM books WHERE `group` = :group") @Query("SELECT * FROM books WHERE `group` = :group")
fun observeByGroup(group: Int): DataSource.Factory<Int, Book> fun observeByGroup(group: Int): DataSource.Factory<Int, Book>
@Query("SELECT url FROM books WHERE `group` = :group") @Query("SELECT tocUrl FROM books WHERE `group` = :group")
fun observeUrlsByGroup(group: Int): LiveData<List<String>> fun observeUrlsByGroup(group: Int): LiveData<List<String>>
} }

@ -0,0 +1,2 @@
package io.legado.app.data.entities

@ -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)
}
}
}
}
}

@ -1,5 +1,6 @@
package io.legado.app.ui.main package io.legado.app.ui.main
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
@ -7,12 +8,15 @@ import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
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 androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import com.google.android.material.navigation.NavigationView import com.google.android.material.navigation.NavigationView
import io.legado.app.R import io.legado.app.R
import io.legado.app.ui.search.SearchActivity
import io.legado.app.utils.longSnackbar import io.legado.app.utils.longSnackbar
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.startActivity
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
@ -22,7 +26,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
fab.setOnClickListener { it.longSnackbar(R.string.app_name) } fab.setOnClickListener { startActivity(Intent(this, SearchActivity::class.java)) }
val toggle = ActionBarDrawerToggle( val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, this, drawer_layout, toolbar,

@ -1,13 +1,23 @@
package io.legado.app.ui.search package io.legado.app.ui.search
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.ViewModelProvider
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity
import io.legado.app.search.SearchDataBinding
class SearchActivity : BaseActivity<SearchDataBinding, SearchViewModel>() {
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)
} }
} }

@ -0,0 +1,6 @@
package io.legado.app.ui.search
import android.app.Application
import androidx.lifecycle.AndroidViewModel
class SearchViewModel(application: Application) : AndroidViewModel(application)

@ -1,4 +1,10 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
<data class=".search.SearchDataBinding">
<variable name="SearchViewModel" type="io.legado.app.ui.search.SearchViewModel"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
@ -6,4 +12,29 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="io.legado.app.ui.search.SearchActivity"> tools:context="io.legado.app.ui.search.SearchActivity">
<io.legado.app.ui.widget.TitleBar
android:id="@+id/titleBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
/>
<io.legado.app.ui.widget.dynamiclayout.DynamicFrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/titleBar">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/searchList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</io.legado.app.ui.widget.dynamiclayout.DynamicFrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

@ -1,7 +1,7 @@
<resources> <resources>
<!-- Base application theme. --> <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. --> <!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

Loading…
Cancel
Save