Skip to content

Commit

Permalink
登录-我的界面联通
Browse files Browse the repository at this point in the history
  • Loading branch information
linxiangcheer committed Mar 19, 2021
1 parent 9e9c613 commit dd29061
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 53 deletions.
1 change: 1 addition & 0 deletions login/src/debug/java/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>() {

//将数据保存到数据库里
DbHelper.insertUserInfo(context, it)

//关闭Activity
finish()
}
Expand Down
30 changes: 15 additions & 15 deletions mine/src/debug/java/com/cniao5/mine/MineActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ class MineActivity : BaseActivity<ActivityMineBinding>() {
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")
// }
}
}
}
16 changes: 13 additions & 3 deletions mine/src/main/java/com/cniao5/mine/MineFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
}
}

Expand Down
58 changes: 28 additions & 30 deletions service/src/main/java/com/test/service/repo/DataBase.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -67,10 +41,34 @@ interface UserDao {

//条件判断: =精确判断 in多种条件的判断 like模糊的判断
//查数据表有可能为空
@Query("select * from tv_user where id = :id")
fun queryLiveUser(id: Int = 0): LiveData<UserInfo>
@Query("select * from tv_user where idd = :idd")
fun queryLiveUser(idd: Int = 0): LiveData<UserInfo>

@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 }
}
}
}
8 changes: 4 additions & 4 deletions service/src/main/java/com/test/service/repo/DbHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ object DbHelper {
* 获取room数据表中存储的userInfo
* return liveData观察者形式
* */
fun getLiveUserInfo(context: Context): LiveData<UserInfo> = DataBase.getInstance(context).userDao.queryLiveUser()
fun getLiveUserInfo(context: Context): LiveData<UserInfo> = 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信息
Expand All @@ -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)
}

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

Expand Down
13 changes: 12 additions & 1 deletion service/src/main/java/com/test/service/utils/BindingApt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dd29061

Please sign in to comment.