From dd290610562b750bacdde71d6a108de69814271e Mon Sep 17 00:00:00 2001 From: LinXiang <1073456077@qq.com> Date: Thu, 11 Mar 2021 12:18:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95-=E6=88=91=E7=9A=84=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E8=81=94=E9=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- login/src/debug/java/login/LoginActivity.kt | 1 + .../java/com/cniao5/mine/MineActivity.kt | 30 +++++----- .../main/java/com/cniao5/mine/MineFragment.kt | 16 ++++- .../java/com/test/service/repo/DataBase.kt | 58 +++++++++---------- .../java/com/test/service/repo/DbHelper.kt | 8 +-- .../java/com/test/service/utils/BindingApt.kt | 13 ++++- 6 files changed, 73 insertions(+), 53 deletions(-) diff --git a/login/src/debug/java/login/LoginActivity.kt b/login/src/debug/java/login/LoginActivity.kt index d199167..dd4d701 100644 --- a/login/src/debug/java/login/LoginActivity.kt +++ b/login/src/debug/java/login/LoginActivity.kt @@ -50,6 +50,7 @@ class LoginActivity : BaseActivity() { //将数据保存到数据库里 DbHelper.insertUserInfo(context, it) + //关闭Activity finish() } diff --git a/mine/src/debug/java/com/cniao5/mine/MineActivity.kt b/mine/src/debug/java/com/cniao5/mine/MineActivity.kt index a83d4ab..cc11d75 100644 --- a/mine/src/debug/java/com/cniao5/mine/MineActivity.kt +++ b/mine/src/debug/java/com/cniao5/mine/MineActivity.kt @@ -16,21 +16,21 @@ class MineActivity : BaseActivity() { override fun initView() { super.initView() mBinding.apply { - val ib = ItemSettingsBean(iconRes = R.drawable.ic_back, desc = "你好", title="学习卡") - - val obBean = ObservableField(ib) - bean = obBean - - ib.title = "你的学习卡" - ib.titleColor = Color.RED - ib.iconRes = R.drawable.icon_server_host - - isvCard.onClickArrow { - ToastUtils.showShort("点击箭头") - } - isvCard.setOnClickListener { - ToastUtils.showShort("点击整个Item") - } + // val ib = ItemSettingsBean(iconRes = R.drawable.ic_back, desc = "你好", title="学习卡") + // + // val obBean = ObservableField(ib) + // bean = obBean + // + // ib.title = "你的学习卡" + // ib.titleColor = Color.RED + // ib.iconRes = R.drawable.icon_server_host + // + // isvCard.onClickArrow { + // ToastUtils.showShort("点击箭头") + // } + // isvCard.setOnClickListener { + // ToastUtils.showShort("点击整个Item") + // } } } } \ No newline at end of file diff --git a/mine/src/main/java/com/cniao5/mine/MineFragment.kt b/mine/src/main/java/com/cniao5/mine/MineFragment.kt index 1804d64..c5eac69 100644 --- a/mine/src/main/java/com/cniao5/mine/MineFragment.kt +++ b/mine/src/main/java/com/cniao5/mine/MineFragment.kt @@ -4,16 +4,20 @@ import android.os.Bundle import android.view.View import androidx.databinding.ViewDataBinding import com.alibaba.android.arouter.launcher.ARouter +import com.blankj.utilcode.util.LogUtils import com.cniao5.common.base.BaseFragment import com.cniao5.mine.databinding.FragmentMineBinding import com.test.service.repo.DbHelper +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import org.koin.androidx.viewmodel.ext.android.viewModel /* * 我的界面 Fragment * 传入了R.layout.fragment_course之后就不用写onCreateView,因为布局已经被关联到fragment里了 * */ -class MineFragment: BaseFragment() { +class MineFragment : BaseFragment() { private val viewModel: MineViewModel by viewModel() @@ -31,15 +35,21 @@ class MineFragment: BaseFragment() { //跳转到登录界面 ARouter.getInstance().build("/login/login").navigation() } + isvStudyCardMine.setOnClickListener { + GlobalScope.launch(Dispatchers.IO) { + LogUtils.i("${DbHelper.getUserInfo(requireContext())}") + } + } } } override fun initData() { super.initData() //requireContext 返回此片段的上下文 - DbHelper.getLiveUserInfo(requireContext()).observeKt { + DbHelper.getLiveUserInfo(requireContext()).observeKt { info -> + LogUtils.i("拿到登录后的数据") //观察登录后的数据 - viewModel.liveUser.value = it + viewModel.liveUser.value = info } } diff --git a/service/src/main/java/com/test/service/repo/DataBase.kt b/service/src/main/java/com/test/service/repo/DataBase.kt index d11466b..47528e4 100644 --- a/service/src/main/java/com/test/service/repo/DataBase.kt +++ b/service/src/main/java/com/test/service/repo/DataBase.kt @@ -1,7 +1,6 @@ package com.test.service.repo import android.content.Context -import androidx.annotation.Keep import androidx.lifecycle.LiveData import androidx.room.* import com.google.gson.annotations.SerializedName @@ -11,31 +10,6 @@ import com.google.gson.annotations.SerializedName * 数据库的增删改查读写应该放在非UI线程 * */ -//3、database定义数据库 -//exportSchema是否导出数据表 -@Database(entities = [UserInfo::class], version = 1, exportSchema = false) -abstract class DataBase : RoomDatabase() { - - abstract val userDao: UserDao - - //单例 - companion object { - private const val CNIAO_DB_NAME = "cniao_db" - - @Volatile //保证不同线程对这个共享变量进行操作的可见性,并且禁止进行指令重排序 - private var instance: DataBase? = null - - @Synchronized //保证线程安全 - fun getInstance(context: Context): DataBase { - return instance ?: Room.databaseBuilder( - context, - DataBase::class.java, - CNIAO_DB_NAME - ).build().also { instance = it } - } - } -} - //1、entity的定义 @Entity(tableName = "tv_user") data class UserInfo( @@ -67,10 +41,34 @@ interface UserDao { //条件判断: =精确判断 in多种条件的判断 like模糊的判断 //查数据表有可能为空 - @Query("select * from tv_user where id = :id") - fun queryLiveUser(id: Int = 0): LiveData + @Query("select * from tv_user where idd = :idd") + fun queryLiveUser(idd: Int = 0): LiveData - @Query("select * from tv_user where id = :id") - fun queryUser(id: Int = 0): UserInfo + @Query("select * from tv_user where idd = :idd") + fun queryUser(idd: Int = 0): UserInfo +} + +//3、database定义数据库 +@Database(entities = [UserInfo::class], version = 1, exportSchema = false) +abstract class DataBase : RoomDatabase() { + + abstract fun userDao(): UserDao + + //单例 + companion object { + private const val DB_NAME = "database_db" + + @Volatile //保证不同线程对这个共享变量进行操作的可见性,并且禁止进行指令重排序 + private var instance: DataBase? = null + + @Synchronized //保证线程安全 + fun getInstance(context: Context): DataBase { + return instance ?: Room.databaseBuilder( + context, + DataBase::class.java, + DB_NAME + ).build().also { instance = it } + } + } } \ No newline at end of file diff --git a/service/src/main/java/com/test/service/repo/DbHelper.kt b/service/src/main/java/com/test/service/repo/DbHelper.kt index 87bac25..8253ec2 100644 --- a/service/src/main/java/com/test/service/repo/DbHelper.kt +++ b/service/src/main/java/com/test/service/repo/DbHelper.kt @@ -16,12 +16,12 @@ object DbHelper { * 获取room数据表中存储的userInfo * return liveData观察者形式 * */ - fun getLiveUserInfo(context: Context): LiveData = DataBase.getInstance(context).userDao.queryLiveUser() + fun getLiveUserInfo(context: Context): LiveData = DataBase.getInstance(context).userDao().queryLiveUser() /* * 以普通数据对象的形式,获取userInfo * */ - fun getUserInfo(context: Context) = DataBase.getInstance(context).userDao.queryUser() + fun getUserInfo(context: Context) = DataBase.getInstance(context).userDao().queryUser() /* * 删除数据表中的userInfo信息 @@ -30,7 +30,7 @@ object DbHelper { //要指定Dispatchers.IO线程,用户网络请求和文件访问 GlobalScope.launch(Dispatchers.IO) { getUserInfo(context)?.let { info -> - DataBase.getInstance(context).userDao.deleteUser(info) + DataBase.getInstance(context).userDao().deleteUser(info) } } @@ -42,7 +42,7 @@ object DbHelper { fun insertUserInfo(context: Context, user: UserInfo) { //要指定Dispatchers.IO线程,用户网络请求和文件访问 GlobalScope.launch(Dispatchers.IO) { - DataBase.getInstance(context).userDao.insertUser(user) + DataBase.getInstance(context).userDao().insertUser(user) } } diff --git a/service/src/main/java/com/test/service/utils/BindingApt.kt b/service/src/main/java/com/test/service/utils/BindingApt.kt index e6a5132..5433775 100644 --- a/service/src/main/java/com/test/service/utils/BindingApt.kt +++ b/service/src/main/java/com/test/service/utils/BindingApt.kt @@ -20,7 +20,18 @@ import com.test.service.R @BindingAdapter("app:srcCompat", requireAll = false) fun imgSrcCompat(iv: ImageView, src: Any?) { //如果src为空,设置默认图片 - val imgRes = src ?: R.drawable.icon_default_header + // val imgRes = src ?: R.drawable.icon_default_header + //如果String头为//img.cniao5.com,则在其前面拼接上https: + val imgRes = when(src) { + is String -> { + when { + src.startsWith("//img.cniao5.com") -> "https:$src" + else -> src + } + } + else -> src ?: R.drawable.icon_default_header + } + Glide.with(iv) .load(imgRes) .into(iv)