增加databinding的使用
增加paging分页工具
This commit is contained in:
parent
cdb057e477
commit
de0a2fe5fc
@ -2,6 +2,7 @@ plugins {
|
||||
id 'com.android.application'
|
||||
id 'org.jetbrains.kotlin.android'
|
||||
id 'kotlin-android'
|
||||
id 'kotlin-parcelize'
|
||||
id 'kotlin-kapt'
|
||||
id 'dagger.hilt.android.plugin'
|
||||
// id 'com.google.dagger.hilt.android'
|
||||
@ -61,14 +62,17 @@ dependencies {
|
||||
implementation 'androidx.appcompat:appcompat:1.5.1'
|
||||
implementation 'com.google.android.material:material:1.7.0'
|
||||
implementation "androidx.compose.material3:material3:1.0.0-alpha04"
|
||||
|
||||
//布局
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||
|
||||
|
||||
//生命周期
|
||||
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
|
||||
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
|
||||
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
|
||||
implementation "androidx.lifecycle:lifecycle-common-java8:2.4.1"
|
||||
|
||||
//导航
|
||||
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
|
||||
implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'
|
||||
|
||||
@ -76,7 +80,7 @@ dependencies {
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
|
||||
|
||||
//room
|
||||
//room 数据库相关
|
||||
implementation 'com.tencent.wcdb:room:1.1-19' // 代替 room-runtime,同时也不需要再引用 wcdb-android
|
||||
api 'androidx.sqlite:sqlite:2.2.0'
|
||||
implementation 'androidx.room:room-runtime:2.4.3'
|
||||
@ -86,6 +90,10 @@ dependencies {
|
||||
kapt 'android.arch.persistence.room:compiler:1.1.1'// compiler 需要用 room 的
|
||||
kapt 'androidx.room:room-compiler:2.4.3'
|
||||
kapt 'androidx.room:room-ktx:2.4.3'
|
||||
//分页加载
|
||||
implementation "androidx.room:room-paging:2.4.3"
|
||||
implementation "androidx.paging:paging-runtime-ktx:3.1.1"
|
||||
|
||||
androidTestImplementation "android.arch.persistence.room:testing:1.1.1"
|
||||
// implementation "android.arch.lifecycle:extensions:1.1.1"
|
||||
// annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
|
||||
@ -96,6 +104,8 @@ dependencies {
|
||||
implementation 'me.rosuh:AndroidFilePicker:0.8.2'
|
||||
// 时间选择器 https://github.com/Gredicer/datetimepicker
|
||||
implementation 'com.github.Gredicer:datetimepicker:V1.0.0'
|
||||
|
||||
//带侧滑的自定义列表
|
||||
implementation 'com.yanzhenjie.recyclerview:x:1.3.2'
|
||||
// implementation 'androidx.appcompat:appcompat:1.5.1'
|
||||
|
||||
@ -139,6 +149,8 @@ dependencies {
|
||||
implementation("com.github.bumptech.glide:glide:4.11.0") {
|
||||
exclude group: "com.android.support"
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
kapt {
|
||||
|
||||
@ -21,7 +21,7 @@
|
||||
android:configChanges="locale"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:icon="@mipmap/volvo_logo_small"
|
||||
android:requestLegacyExternalStorage="true"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
|
||||
@ -1,19 +1,27 @@
|
||||
package com.navinfo.volvo.database.dao
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.navinfo.volvo.database.entity.GreetingMessage
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface GreetingMessageDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(vararg check: GreetingMessage)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(list: List<GreetingMessage>)
|
||||
|
||||
@Query("SELECT * FROM GreetingMessage where id =:id")
|
||||
fun findCheckManagerById(id: Long): GreetingMessage?
|
||||
|
||||
@Query("SELECT * FROM GreetingMessage")
|
||||
fun findList(): List<GreetingMessage>
|
||||
fun findAllByFlow(): Flow<List<GreetingMessage>>
|
||||
|
||||
@Query("SELECT * FROM GreetingMessage")
|
||||
fun findAllByDataSource(): PagingSource<Int, GreetingMessage>
|
||||
}
|
||||
@ -1,8 +1,10 @@
|
||||
package com.navinfo.volvo.database.entity
|
||||
|
||||
import android.os.Parcelable
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import androidx.room.TypeConverters
|
||||
import kotlinx.android.parcel.Parcelize
|
||||
import org.jetbrains.annotations.NotNull
|
||||
|
||||
@Entity(tableName = "GreetingMessage")
|
||||
@ -40,5 +42,4 @@ data class GreetingMessage @JvmOverloads constructor(
|
||||
* 附件列表
|
||||
*/
|
||||
var attachment: MutableList<Attachment> = mutableListOf()
|
||||
) {
|
||||
}
|
||||
)
|
||||
|
||||
@ -5,6 +5,7 @@ import androidx.room.Room
|
||||
import com.navinfo.volvo.database.AppDatabase
|
||||
import com.navinfo.volvo.database.dao.GreetingMessageDao
|
||||
import com.navinfo.volvo.database.dao.UserDao
|
||||
import com.navinfo.volvo.utils.SystemConstant
|
||||
import com.tencent.wcdb.database.SQLiteCipherSpec
|
||||
import com.tencent.wcdb.room.db.WCDBOpenHelperFactory
|
||||
import dagger.Module
|
||||
@ -31,7 +32,7 @@ class DatabaseModule {
|
||||
.writeAheadLoggingEnabled(true) // enable WAL mode, remove if not needed
|
||||
.asyncCheckpointEnabled(true); // enable asynchronous checkpoint, remove if not needed
|
||||
|
||||
return Room.databaseBuilder(context, AppDatabase::class.java, "NavinfoVolvoDb")
|
||||
return Room.databaseBuilder(context, AppDatabase::class.java, "${SystemConstant.ROOT_PATH}/NavinfoVolvoDb.db")
|
||||
|
||||
// [WCDB] Specify open helper to use WCDB database implementation instead
|
||||
// of the Android framework.
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
package com.navinfo.volvo.di.module
|
||||
|
||||
import com.navinfo.volvo.repository.database.DatabaseRepository
|
||||
import com.navinfo.volvo.repository.database.DatabaseRepositoryImp
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
|
||||
@InstallIn(SingletonComponent::class)
|
||||
@Module
|
||||
abstract class DatabaseRepositoryModule {
|
||||
@Binds
|
||||
abstract fun bingDatabaseRepository(databaseRepositoryImp: DatabaseRepositoryImp): DatabaseRepository
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
package com.navinfo.volvo.di.module
|
||||
|
||||
import com.navinfo.volvo.repository.NetworkDataSource
|
||||
import com.navinfo.volvo.repository.NetworkDataSourceImp
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
|
||||
@InstallIn(SingletonComponent::class)
|
||||
@Module
|
||||
abstract class NetworkDataModule {
|
||||
@Binds
|
||||
abstract fun bindNetworkData(networkDataSourceImp: NetworkDataSourceImp): NetworkDataSource
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package com.navinfo.volvo.di.module
|
||||
|
||||
import com.navinfo.volvo.repository.network.NetworkRepository
|
||||
import com.navinfo.volvo.repository.network.NetworkRepositoryImp
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
|
||||
@InstallIn(SingletonComponent::class)
|
||||
@Module
|
||||
abstract class NetworkRepositoryModule {
|
||||
@Binds
|
||||
abstract fun bindNetworkRepository(networkRepositoryImp: NetworkRepositoryImp): NetworkRepository
|
||||
}
|
||||
@ -4,7 +4,7 @@ import android.app.Application
|
||||
import android.content.Context
|
||||
import com.google.gson.Gson
|
||||
import com.navinfo.volvo.Constant
|
||||
import com.navinfo.volvo.repository.service.NetworkService
|
||||
import com.navinfo.volvo.repository.network.NetworkService
|
||||
import com.navinfo.volvo.tools.GsonUtil
|
||||
import dagger.Lazy
|
||||
import dagger.Module
|
||||
@ -15,7 +15,6 @@ import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Singleton
|
||||
|
||||
|
||||
@ -101,4 +100,5 @@ class NetworkUtilModule {
|
||||
fun provideNetworkService(retrofit: Retrofit): NetworkService {
|
||||
return retrofit.create(NetworkService::class.java)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -3,8 +3,9 @@ package com.navinfo.volvo.di.module
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.navinfo.volvo.di.key.ViewModelKey
|
||||
import com.navinfo.volvo.ui.fragments.home.MessageViewModel
|
||||
import com.navinfo.volvo.ui.fragments.home.HomeViewModel
|
||||
import com.navinfo.volvo.ui.fragments.login.LoginViewModel
|
||||
import com.navinfo.volvo.ui.fragments.message.ObtainMessageViewModel
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.hilt.InstallIn
|
||||
@ -25,6 +26,11 @@ abstract class ViewModelModule {
|
||||
|
||||
@IntoMap
|
||||
@Binds
|
||||
@ViewModelKey(MessageViewModel::class)
|
||||
abstract fun bindMessageFragmentViewModel(viewModel: MessageViewModel): ViewModel
|
||||
@ViewModelKey(HomeViewModel::class)
|
||||
abstract fun bindMessageFragmentViewModel(viewModel: HomeViewModel): ViewModel
|
||||
|
||||
@IntoMap
|
||||
@Binds
|
||||
@ViewModelKey(ObtainMessageViewModel::class)
|
||||
abstract fun bindObtainMessageFragmentViewModel(viewModel: ObtainMessageViewModel): ViewModel
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package com.navinfo.volvo.repository.database
|
||||
|
||||
import androidx.paging.PagingData
|
||||
import com.navinfo.volvo.database.entity.GreetingMessage
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
/**
|
||||
* 数据库操作接口
|
||||
*/
|
||||
interface DatabaseRepository {
|
||||
fun getMessageByPaging(): Flow<PagingData<GreetingMessage>>
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package com.navinfo.volvo.repository.database
|
||||
|
||||
import androidx.paging.Pager
|
||||
import androidx.paging.PagingConfig
|
||||
import androidx.paging.PagingData
|
||||
import com.navinfo.volvo.database.dao.GreetingMessageDao
|
||||
import com.navinfo.volvo.database.entity.GreetingMessage
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import javax.inject.Inject
|
||||
|
||||
class DatabaseRepositoryImp @Inject constructor(
|
||||
private val messageDao: GreetingMessageDao
|
||||
) : DatabaseRepository {
|
||||
companion object {
|
||||
const val PAGE_SIZE = 20
|
||||
}
|
||||
|
||||
override fun getMessageByPaging(): Flow<PagingData<GreetingMessage>> {
|
||||
return Pager(PagingConfig(PAGE_SIZE)) {
|
||||
messageDao.findAllByDataSource()
|
||||
}.flow
|
||||
}
|
||||
}
|
||||
@ -1,10 +1,13 @@
|
||||
package com.navinfo.volvo.repository
|
||||
package com.navinfo.volvo.repository.network
|
||||
|
||||
import com.navinfo.volvo.http.DefaultResponse
|
||||
import com.navinfo.volvo.model.messagelist.NetworkMessageListPost
|
||||
import com.navinfo.volvo.model.messagelist.NetworkMessageListResponse
|
||||
import com.navinfo.volvo.util.NetResult
|
||||
|
||||
interface NetworkDataSource {
|
||||
/**
|
||||
* 网络访问接口
|
||||
*/
|
||||
interface NetworkRepository {
|
||||
suspend fun getCardList(message: NetworkMessageListPost): NetResult<DefaultResponse<NetworkMessageListResponse>>
|
||||
}
|
||||
@ -1,13 +1,10 @@
|
||||
package com.navinfo.volvo.repository
|
||||
package com.navinfo.volvo.repository.network
|
||||
|
||||
import com.navinfo.volvo.database.AppDatabase
|
||||
import com.navinfo.volvo.database.dao.GreetingMessageDao
|
||||
import com.navinfo.volvo.di.scope.IoDispatcher
|
||||
import com.navinfo.volvo.database.entity.GreetingMessage
|
||||
import com.navinfo.volvo.http.DefaultResponse
|
||||
import com.navinfo.volvo.model.messagelist.NetworkMessageListPost
|
||||
import com.navinfo.volvo.model.messagelist.NetworkMessageListResponse
|
||||
import com.navinfo.volvo.repository.service.NetworkService
|
||||
import com.navinfo.volvo.tools.GsonUtil
|
||||
import com.navinfo.volvo.util.NetResult
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
@ -17,11 +14,11 @@ import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import javax.inject.Inject
|
||||
|
||||
|
||||
class NetworkDataSourceImp @Inject constructor(
|
||||
class NetworkRepositoryImp @Inject constructor(
|
||||
private val netWorkService: NetworkService,
|
||||
private val messageDao: GreetingMessageDao,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher
|
||||
) : NetworkDataSource {
|
||||
) : NetworkRepository {
|
||||
|
||||
override suspend fun getCardList(message: NetworkMessageListPost): NetResult<DefaultResponse<NetworkMessageListResponse>> =
|
||||
withContext(ioDispatcher) {
|
||||
@ -31,9 +28,7 @@ class NetworkDataSourceImp @Inject constructor(
|
||||
val result = netWorkService.queryCardListByApp(stringBody)
|
||||
if (result.isSuccessful) {
|
||||
val body = result.body()
|
||||
val list: MutableList<GreetingMessage> =
|
||||
listOf(body!!.data!!.rows) as MutableList<GreetingMessage>
|
||||
messageDao.insert(*list.map { it }.toTypedArray())
|
||||
messageDao.insert(body!!.data!!.rows)
|
||||
NetResult.Success(body)
|
||||
} else {
|
||||
NetResult.Success(null)
|
||||
@ -1,4 +1,4 @@
|
||||
package com.navinfo.volvo.repository.service
|
||||
package com.navinfo.volvo.repository.network
|
||||
|
||||
import com.navinfo.volvo.http.DefaultResponse
|
||||
import com.navinfo.volvo.model.messagelist.NetworkMessageListResponse
|
||||
@ -1,7 +1,6 @@
|
||||
package com.navinfo.volvo.ui
|
||||
|
||||
import android.content.DialogInterface
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
@ -85,13 +84,13 @@ class MainActivity : AppCompatActivity() {
|
||||
val navController = findNavController(R.id.nav_host_fragment_activity_main)
|
||||
val appBarConfiguration = AppBarConfiguration(
|
||||
setOf(
|
||||
R.id.navigation_message, R.id.navigation_dashboard, R.id.navigation_notifications,
|
||||
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications,
|
||||
)
|
||||
)
|
||||
setupActionBarWithNavController(navController, appBarConfiguration)
|
||||
navView.setupWithNavController(navController)
|
||||
navController.addOnDestinationChangedListener { controller, destination, arguments ->
|
||||
if (destination.id == R.id.navigation_message
|
||||
if (destination.id == R.id.navigation_home
|
||||
|| destination.id == R.id.navigation_dashboard
|
||||
|| destination.id == R.id.navigation_notifications
|
||||
|| destination.id == R.id.navigation_obtain_message
|
||||
|
||||
@ -1,54 +0,0 @@
|
||||
package com.navinfo.volvo.ui.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.navinfo.volvo.R
|
||||
import com.navinfo.volvo.database.entity.GreetingMessage
|
||||
|
||||
class MessageAdapter : RecyclerView.Adapter<MessageAdapter.MyViewHolder>() {
|
||||
|
||||
var itemList: MutableList<GreetingMessage> = mutableListOf()
|
||||
|
||||
fun addItem(message: GreetingMessage) {
|
||||
itemList.add(message)
|
||||
notifyItemInserted(itemList.size - 1)
|
||||
}
|
||||
|
||||
fun setItem(messageList: MutableList<GreetingMessage>){
|
||||
itemList = messageList
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
|
||||
val view =
|
||||
LayoutInflater.from(parent.context).inflate(R.layout.adapter_message, parent, false)
|
||||
var viewHolder = MyViewHolder(view)
|
||||
viewHolder.itemView.setOnClickListener {
|
||||
|
||||
}
|
||||
return viewHolder
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
|
||||
val message = itemList[position]
|
||||
holder.toName.text = message.toWho
|
||||
holder.messageText.text = message.name
|
||||
holder.sendTime.text = message.sendDate
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return itemList.size
|
||||
}
|
||||
|
||||
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
var image: ImageView = itemView.findViewById(R.id.message_head_icon)
|
||||
var toName: TextView = itemView.findViewById(R.id.message_to_username)
|
||||
var sendTime: TextView = itemView.findViewById(R.id.message_send_time)
|
||||
var status: TextView = itemView.findViewById(R.id.message_status)
|
||||
var messageText: TextView = itemView.findViewById(R.id.message_text)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,81 @@
|
||||
package com.navinfo.volvo.ui.fragments.home
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.paging.PagingDataAdapter
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.bumptech.glide.Glide
|
||||
import com.navinfo.volvo.R
|
||||
import com.navinfo.volvo.database.entity.GreetingMessage
|
||||
import com.navinfo.volvo.databinding.AdapterHomeBinding
|
||||
|
||||
class HomeAdapter(fragment: Fragment) :
|
||||
PagingDataAdapter<GreetingMessage, HomeAdapter.MyViewHolder>(DiffCallback()) {
|
||||
|
||||
val fragment = fragment
|
||||
// var itemList = ArrayList<GreetingMessage>()
|
||||
//
|
||||
// fun addItem(message: GreetingMessage) {
|
||||
// itemList.add(message)
|
||||
// notifyItemInserted(itemList.size - 1)
|
||||
// }
|
||||
//
|
||||
// fun setItems(messageList: List<GreetingMessage>) {
|
||||
// itemList.clear()
|
||||
// itemList.addAll(messageList)
|
||||
// notifyDataSetChanged()
|
||||
// }
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
|
||||
val mDataBinding: AdapterHomeBinding =
|
||||
DataBindingUtil.inflate(
|
||||
LayoutInflater.from(fragment.context),
|
||||
R.layout.adapter_home,
|
||||
parent,
|
||||
false
|
||||
)
|
||||
return MyViewHolder(mDataBinding)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
|
||||
holder.onBind(position)
|
||||
}
|
||||
|
||||
// override fun getItemCount(): Int {
|
||||
// return itemList.size
|
||||
// }
|
||||
|
||||
inner class MyViewHolder(private val mDataBinding: AdapterHomeBinding) :
|
||||
RecyclerView.ViewHolder(mDataBinding.root) {
|
||||
fun onBind(position: Int) {
|
||||
var row = getItem(position)
|
||||
mDataBinding.greetingMessage = row
|
||||
Glide.with(fragment)
|
||||
.asBitmap().fitCenter()
|
||||
.load(row!!.imageUrl)
|
||||
.placeholder(R.mipmap.volvo_logo_small)
|
||||
.error(R.mipmap.volvo_logo_small)
|
||||
.into(mDataBinding.messageHeadIcon)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class DiffCallback : DiffUtil.ItemCallback<GreetingMessage>() {
|
||||
override fun areItemsTheSame(oldItem: GreetingMessage, newItem: GreetingMessage): Boolean {
|
||||
return oldItem.uuid == newItem.uuid
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(
|
||||
oldItem: GreetingMessage,
|
||||
newItem: GreetingMessage
|
||||
): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4,50 +4,44 @@ import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.DividerItemDecoration
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.navinfo.volvo.R
|
||||
import com.navinfo.volvo.databinding.FragmentHomeBinding
|
||||
import com.navinfo.volvo.tools.DisplayUtil
|
||||
import com.navinfo.volvo.ui.BaseFragment
|
||||
import com.navinfo.volvo.ui.adapter.MessageAdapter
|
||||
import com.navinfo.volvo.ui.fragments.message.ObtainMessageViewModel
|
||||
import com.yanzhenjie.recyclerview.*
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@AndroidEntryPoint
|
||||
class MessageFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListener {
|
||||
class HomeFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickListener {
|
||||
|
||||
private var _binding: FragmentHomeBinding? = null
|
||||
private val viewModel by viewModels<HomeViewModel> { viewModelFactoryProvider }
|
||||
|
||||
// This property is only valid between onCreateView and
|
||||
// onDestroyView.
|
||||
private val binding get() = _binding!!
|
||||
|
||||
private val viewModel by viewModels<MessageViewModel> { viewModelFactoryProvider }
|
||||
private lateinit var messageAdapter: HomeAdapter
|
||||
private lateinit var mDataBinding: FragmentHomeBinding
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
// val homeViewModel =
|
||||
// ViewModelProvider(this)[MessageViewModel::class.java]
|
||||
// val obtainMessageViewModel =
|
||||
// ViewModelProvider(requireActivity()).get(ObtainMessageViewModel::class.java)
|
||||
|
||||
_binding = FragmentHomeBinding.inflate(inflater, container, false)
|
||||
val root: View = binding.root
|
||||
mDataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
|
||||
mDataBinding.lifecycleOwner = this
|
||||
initView()
|
||||
return root
|
||||
return mDataBinding.root
|
||||
}
|
||||
|
||||
|
||||
private fun initView() {
|
||||
val recyclerview: SwipeRecyclerView = binding.homeMessageRecyclerview
|
||||
mDataBinding.homeViewModel = viewModel
|
||||
messageAdapter = HomeAdapter(this)
|
||||
val recyclerview: SwipeRecyclerView = mDataBinding.homeRecyclerview
|
||||
recyclerview.adapter = null //先设置null,否则会报错
|
||||
//创建菜单选项
|
||||
//注意:使用滑动菜单不能开启滑动删除,否则只有滑动删除没有滑动菜单
|
||||
@ -71,34 +65,37 @@ class MessageFragment : BaseFragment(), OnItemClickListener, OnItemMenuClickList
|
||||
rightMenu.addMenuItem(shareItem)
|
||||
}
|
||||
val layoutManager = LinearLayoutManager(context)
|
||||
val adapter = MessageAdapter()
|
||||
|
||||
recyclerview.layoutManager = layoutManager
|
||||
recyclerview.addItemDecoration(DividerItemDecoration(context, layoutManager.orientation))
|
||||
recyclerview.setSwipeMenuCreator(mSwipeMenuCreator)
|
||||
recyclerview.setOnItemClickListener(this)
|
||||
recyclerview.useDefaultLoadMore()
|
||||
recyclerview.setLoadMoreListener {
|
||||
|
||||
// recyclerview.useDefaultLoadMore()
|
||||
// recyclerview.setLoadMoreListener {
|
||||
//
|
||||
// }
|
||||
lifecycleScope.launch {
|
||||
viewModel.messageList.collectLatest {
|
||||
messageAdapter.submitData(it)
|
||||
}
|
||||
recyclerview.adapter = adapter
|
||||
// homeViewModel.getMessageList().observe(viewLifecycleOwner, Observer { contacts ->
|
||||
// adapter.setItem(contacts)
|
||||
// })
|
||||
}
|
||||
// messageAdapter.withLoadStateFooter(
|
||||
// footer = RecLoadStateAdapter { messageAdapter.retry() }
|
||||
// )
|
||||
|
||||
// messageAdapter.withLoadStateHeader()
|
||||
recyclerview.adapter = messageAdapter
|
||||
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
getMessageList()
|
||||
}
|
||||
|
||||
private fun getMessageList() {
|
||||
viewModel.getMessageList()
|
||||
viewModel.getNetMessageList()
|
||||
}
|
||||
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
|
||||
override fun onItemClick(view: View?, adapterPosition: Int) {
|
||||
@ -0,0 +1,57 @@
|
||||
package com.navinfo.volvo.ui.fragments.home
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.paging.PagingData
|
||||
import com.navinfo.volvo.database.dao.GreetingMessageDao
|
||||
import com.navinfo.volvo.database.entity.GreetingMessage
|
||||
import com.navinfo.volvo.model.messagelist.NetworkMessageListPost
|
||||
import com.navinfo.volvo.repository.database.DatabaseRepository
|
||||
import com.navinfo.volvo.repository.network.NetworkRepository
|
||||
import com.navinfo.volvo.util.NetResult
|
||||
import com.navinfo.volvo.util.asLiveData
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
class HomeViewModel @Inject constructor(
|
||||
private val netRepository: NetworkRepository,
|
||||
private val dataRepository: DatabaseRepository
|
||||
) : ViewModel() {
|
||||
|
||||
private val _isLoading = MutableLiveData<Boolean>()
|
||||
val isLoading = _isLoading.asLiveData()
|
||||
|
||||
// private val _messageList = MutableLiveData<List<GreetingMessage>>()
|
||||
// val messageList = _messageList.asLiveData()
|
||||
|
||||
|
||||
val messageList: Flow<PagingData<GreetingMessage>>
|
||||
get() = dataRepository.getMessageByPaging()
|
||||
|
||||
|
||||
fun getNetMessageList() {
|
||||
_isLoading.postValue(true)
|
||||
viewModelScope.launch {
|
||||
val messagePost = NetworkMessageListPost(who = "", toWho = "")
|
||||
when (val result = netRepository.getCardList(messagePost)) {
|
||||
is NetResult.Success -> {
|
||||
_isLoading.value = false
|
||||
// if (result.data != null) {
|
||||
// val list = (result.data.data as NetworkMessageListResponse).rows
|
||||
// _messageList.value = list
|
||||
// }
|
||||
}
|
||||
|
||||
is NetResult.Error -> {
|
||||
_isLoading.value = false
|
||||
}
|
||||
is NetResult.Loading -> {
|
||||
_isLoading.postValue(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,48 +0,0 @@
|
||||
package com.navinfo.volvo.ui.fragments.home
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.navinfo.volvo.database.entity.GreetingMessage
|
||||
import com.navinfo.volvo.model.messagelist.NetworkMessageListPost
|
||||
import com.navinfo.volvo.model.messagelist.NetworkMessageListResponse
|
||||
import com.navinfo.volvo.repository.NetworkDataSource
|
||||
import com.navinfo.volvo.util.NetResult
|
||||
import com.navinfo.volvo.util.asLiveData
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
class MessageViewModel @Inject constructor(
|
||||
private val repository: NetworkDataSource
|
||||
) : ViewModel() {
|
||||
|
||||
private val _isLoading = MutableLiveData<Boolean>()
|
||||
val isLoading = _isLoading.asLiveData()
|
||||
|
||||
private val _messageList = MutableLiveData<List<GreetingMessage>>()
|
||||
val messageList = _messageList.asLiveData()
|
||||
|
||||
fun getMessageList() {
|
||||
_isLoading.postValue(true)
|
||||
viewModelScope.launch {
|
||||
val messagePost = NetworkMessageListPost(who = "北京测试", toWho = "volvo测试")
|
||||
when (val result = repository.getCardList(messagePost)) {
|
||||
is NetResult.Success -> {
|
||||
_isLoading.value = false
|
||||
if (result.data != null) {
|
||||
val list = (result.data.data as NetworkMessageListResponse).rows
|
||||
_messageList.value = list
|
||||
}
|
||||
}
|
||||
|
||||
is NetResult.Error -> {
|
||||
_isLoading.value = false
|
||||
}
|
||||
is NetResult.Loading -> {
|
||||
_isLoading.postValue(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
//package com.navinfo.volvo.ui.fragments.home
|
||||
//
|
||||
//import android.view.LayoutInflater
|
||||
//import android.view.View
|
||||
//import android.view.ViewGroup
|
||||
//import androidx.core.view.isVisible
|
||||
//import androidx.paging.LoadState
|
||||
//import androidx.paging.LoadStateAdapter
|
||||
//import androidx.recyclerview.widget.RecyclerView
|
||||
//import com.example.picsapp.R
|
||||
//import com.example.picsapp.databinding.LoadStateViewBinding
|
||||
//
|
||||
//class RecLoadStateAdapter(
|
||||
// private val retry: () -> Unit
|
||||
//) : LoadStateAdapter<RecLoadStateAdapter.LoadStateViewHolder>() {
|
||||
//
|
||||
// override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadStateViewHolder {
|
||||
//
|
||||
// val binding = LoadStateViewBinding
|
||||
// .inflate(LayoutInflater.from(parent.context), parent, false)
|
||||
//
|
||||
// return LoadStateViewHolder(binding)
|
||||
// }
|
||||
//
|
||||
// override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) {
|
||||
// holder.onBind(loadState)
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
// inner class LoadStateViewHolder(private val binding: LoadStateViewBinding) : RecyclerView.ViewHolder(binding.root){
|
||||
// fun onBind(loadState: LoadState) {
|
||||
// val progress = binding.loadStateProgress
|
||||
// val btnRetry = binding.loadStateRetry
|
||||
// val txtErrorMessage = binding.loadStateErrorMessage
|
||||
//
|
||||
// btnRetry.isVisible = loadState !is LoadState.Loading
|
||||
// txtErrorMessage.isVisible = loadState !is LoadState.Loading
|
||||
// progress.isVisible = loadState is LoadState.Loading
|
||||
//
|
||||
// if (loadState is LoadState.Error){
|
||||
// txtErrorMessage.text = loadState.error.localizedMessage
|
||||
// }
|
||||
//
|
||||
// btnRetry.setOnClickListener {
|
||||
// retry.invoke()
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
67
app/src/main/res/layout/adapter_home.xml
Normal file
67
app/src/main/res/layout/adapter_home.xml
Normal file
@ -0,0 +1,67 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<data>
|
||||
|
||||
<import type="android.view.View" />
|
||||
|
||||
<variable
|
||||
name="greetingMessage"
|
||||
type="com.navinfo.volvo.database.entity.GreetingMessage" />
|
||||
</data>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/message_head_icon"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="60dp"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:src="@mipmap/volvo_logo_small"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/message_to_who"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:ellipsize="end"
|
||||
android:textSize="16sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintLeft_toRightOf="@id/message_head_icon"
|
||||
app:layout_constraintTop_toTopOf="@id/message_head_icon"
|
||||
android:text="@{greetingMessage.toWho,default=发送对象}" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/message_text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:text="@{greetingMessage.name,default=消息内容}"
|
||||
app:layout_constraintBottom_toBottomOf="@id/message_head_icon"
|
||||
app:layout_constraintLeft_toRightOf="@id/message_head_icon" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/message_send_time"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginRight="8dp"
|
||||
android:text="@{greetingMessage.sendDate,default=发送时间}"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/message_head_icon" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/message_status"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:text="@{greetingMessage.status,default=消息状态}"
|
||||
app:layout_constraintBottom_toBottomOf="@id/message_head_icon"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</layout>
|
||||
@ -1,54 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/message_head_icon"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:src="@mipmap/volvo_logo_small"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/message_to_username"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:text="问候对象"
|
||||
android:ellipsize="end"
|
||||
android:textSize="16sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintLeft_toRightOf="@id/message_head_icon"
|
||||
app:layout_constraintTop_toTopOf="@id/message_head_icon" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/message_text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:text="信息内容"
|
||||
app:layout_constraintBottom_toBottomOf="@id/message_head_icon"
|
||||
app:layout_constraintLeft_toRightOf="@id/message_head_icon" />
|
||||
<TextView
|
||||
android:id="@+id/message_send_time"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginRight="8dp"
|
||||
android:text="日期"
|
||||
app:layout_constraintTop_toTopOf="@id/message_head_icon"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
/>
|
||||
<TextView
|
||||
android:id="@+id/message_status"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:text="状态"
|
||||
app:layout_constraintBottom_toBottomOf="@id/message_head_icon"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
@ -1,10 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.yanzhenjie.recyclerview.SwipeRecyclerView
|
||||
android:id="@+id/home_message_recyclerview"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<data>
|
||||
|
||||
<import type="android.view.View" />
|
||||
|
||||
<variable
|
||||
name="HomeViewModel"
|
||||
type="com.navinfo.volvo.ui.fragments.home.HomeViewModel" />
|
||||
</data>
|
||||
|
||||
<com.yanzhenjie.recyclerview.SwipeRecyclerView
|
||||
android:id="@+id/home_recyclerview"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.fragments.home.MessageFragment">
|
||||
</com.yanzhenjie.recyclerview.SwipeRecyclerView>
|
||||
tools:context=".ui.fragments.home.HomeFragment" />
|
||||
</layout>
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
android:id="@+id/login_fragment_logo"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:src="@mipmap/ic_launcher"
|
||||
android:src="@mipmap/volvo_logo_small"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<item
|
||||
android:id="@+id/navigation_message"
|
||||
android:id="@+id/navigation_home"
|
||||
android:icon="@drawable/ic_home_black_24dp"
|
||||
android:title="@string/title_home" />
|
||||
<item
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
tools:layout="@layout/fragment_login">
|
||||
<action
|
||||
android:id="@+id/action_login_to_home"
|
||||
app:destination="@id/navigation_message"
|
||||
app:destination="@id/navigation_home"
|
||||
app:enterAnim="@anim/from_left"
|
||||
app:exitAnim="@anim/to_right"
|
||||
app:popEnterAnim="@anim/from_right"
|
||||
@ -21,8 +21,8 @@
|
||||
app:popUpToInclusive="true" />
|
||||
</fragment>
|
||||
<fragment
|
||||
android:id="@+id/navigation_message"
|
||||
android:name="com.navinfo.volvo.ui.fragments.home.MessageFragment"
|
||||
android:id="@+id/navigation_home"
|
||||
android:name="com.navinfo.volvo.ui.fragments.home.HomeFragment"
|
||||
android:label="@string/title_home"
|
||||
tools:layout="@layout/fragment_home"></fragment>
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user