设计一个较为复杂的kotlin语言下基于MVP的个人信息管理系统
时间: 2023-07-18 22:51:50 浏览: 90
基于MVP架构Kotlin语言项目.zip
以下是一个较为复杂的基于MVP模式的个人信息管理系统的Kotlin代码示例,包含了多个Activity、Fragment和Presenter,供您参考:
1. Model层代码
```kotlin
interface IUserInfoModel {
fun saveUserInfo(userInfo: UserInfo)
fun loadUserInfo(): UserInfo?
fun loadContactList(): List<Contact>
fun saveContact(contact: Contact)
fun deleteContact(contact: Contact)
}
class UserInfoModel : IUserInfoModel {
override fun saveUserInfo(userInfo: UserInfo) {
// 保存用户信息到本地数据库或网络服务器
// 省略具体实现
}
override fun loadUserInfo(): UserInfo? {
// 从本地数据库或网络服务器加载用户信息
// 省略具体实现
return null
}
override fun loadContactList(): List<Contact> {
// 从本地数据库或网络服务器加载联系人列表
// 省略具体实现
return listOf()
}
override fun saveContact(contact: Contact) {
// 保存联系人到本地数据库或网络服务器
// 省略具体实现
}
override fun deleteContact(contact: Contact) {
// 从本地数据库或网络服务器删除联系人
// 省略具体实现
}
}
```
2. View层代码
```kotlin
interface IUserInfoView {
fun showUserInfo(userInfo: UserInfo)
fun showError()
}
interface IContactListView {
fun showContactList(contactList: List<Contact>)
fun showAddContactDialog()
fun showDeleteContactDialog(contact: Contact)
fun showError()
}
interface IAddContactView {
fun closeAddContactDialog()
fun showError()
}
interface IDeleteContactView {
fun closeDeleteContactDialog()
fun showError()
}
class UserInfoActivity : AppCompatActivity(), IUserInfoView {
private var mPresenter: UserInfoPresenter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_user_info)
mPresenter = UserInfoPresenter(this, UserInfoModel())
btn_save.setOnClickListener {
val userInfo = UserInfo(et_name.text.toString(), et_phone.text.toString())
mPresenter?.saveUserInfo(userInfo)
}
mPresenter?.loadUserInfo()
}
override fun showUserInfo(userInfo: UserInfo) {
et_name.setText(userInfo.name)
et_phone.setText(userInfo.phone)
}
override fun showError() {
Toast.makeText(this, "加载用户信息失败", Toast.LENGTH_SHORT).show()
}
}
class ContactListFragment : Fragment(), IContactListView {
private var mPresenter: ContactListPresenter? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_contact_list, container, false)
mPresenter = ContactListPresenter(this, UserInfoModel())
mPresenter?.loadContactList()
return view
}
override fun showContactList(contactList: List<Contact>) {
// 显示联系人列表到RecyclerView
// 省略具体实现
}
override fun showAddContactDialog() {
val dialog = AddContactDialogFragment()
dialog.show(childFragmentManager, "AddContactDialog")
}
override fun showDeleteContactDialog(contact: Contact) {
val dialog = DeleteContactDialogFragment(contact)
dialog.show(childFragmentManager, "DeleteContactDialog")
}
override fun showError() {
Toast.makeText(activity, "加载联系人列表失败", Toast.LENGTH_SHORT).show()
}
}
class AddContactDialogFragment : DialogFragment(), IAddContactView {
private var mPresenter: AddContactPresenter? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_add_contact, null)
val dialog = AlertDialog.Builder(activity)
.setTitle("添加联系人")
.setView(view)
.setPositiveButton("确定") { _, _ ->
val contact = Contact(
view.et_name.text.toString(),
view.et_phone.text.toString(),
view.et_email.text.toString()
)
mPresenter?.saveContact(contact)
}
.setNegativeButton("取消") { _, _ ->
dismiss()
}
.create()
mPresenter = AddContactPresenter(this, UserInfoModel())
return dialog
}
override fun closeAddContactDialog() {
dismiss()
}
override fun showError() {
Toast.makeText(activity, "添加联系人失败", Toast.LENGTH_SHORT).show()
}
}
class DeleteContactDialogFragment(private val contact: Contact) : DialogFragment(),
IDeleteContactView {
private var mPresenter: DeleteContactPresenter? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = AlertDialog.Builder(activity)
.setTitle("删除联系人")
.setMessage("确定要删除联系人${contact.name}吗?")
.setPositiveButton("确定") { _, _ ->
mPresenter?.deleteContact(contact)
}
.setNegativeButton("取消") { _, _ ->
dismiss()
}
.create()
mPresenter = DeleteContactPresenter(this, UserInfoModel())
return dialog
}
override fun closeDeleteContactDialog() {
dismiss()
}
override fun showError() {
Toast.makeText(activity, "删除联系人失败", Toast.LENGTH_SHORT).show()
}
}
```
3. Presenter层代码
```kotlin
class UserInfoPresenter(
private val mView: IUserInfoView,
private val mModel: IUserInfoModel
) {
fun saveUserInfo(userInfo: UserInfo) {
mModel.saveUserInfo(userInfo)
mView.showUserInfo(userInfo)
}
fun loadUserInfo() {
val userInfo = mModel.loadUserInfo()
if (userInfo != null) {
mView.showUserInfo(userInfo)
} else {
mView.showError()
}
}
}
class ContactListPresenter(
private val mView: IContactListView,
private val mModel: IUserInfoModel
) {
fun loadContactList() {
val contactList = mModel.loadContactList()
if (contactList.isNotEmpty()) {
mView.showContactList(contactList)
} else {
mView.showError()
}
}
fun addContact() {
mView.showAddContactDialog()
}
fun deleteContact(contact: Contact) {
mView.showDeleteContactDialog(contact)
}
fun saveContact(contact: Contact) {
mModel.saveContact(contact)
loadContactList()
}
fun deleteContact(contact: Contact) {
mModel.deleteContact(contact)
loadContactList()
}
}
class AddContactPresenter(
private val mView: IAddContactView,
private val mModel: IUserInfoModel
) {
fun saveContact(contact: Contact) {
mModel.saveContact(contact)
mView.closeAddContactDialog()
}
}
class DeleteContactPresenter(
private val mView: IDeleteContactView,
private val mModel: IUserInfoModel
) {
fun deleteContact(contact: Contact) {
mModel.deleteContact(contact)
mView.closeDeleteContactDialog()
}
}
```
这个示例中,除了UserInfoActivity外,还包含了一个ContactListFragment、一个AddContactDialogFragment和一个DeleteContactDialogFragment。ContactListFragment负责显示联系人列表和管理联系人操作,AddContactDialogFragment负责添加联系人操作,DeleteContactDialogFragment负责删除联系人操作。每个View层都对应一个Presenter层,负责处理业务逻辑和协调Model和View之间的交互。
当用户在ContactListFragment中点击添加联系人按钮时,ContactListFragment会调用ContactListPresenter的addContact()方法,Presenter层再通过IContactListView的showAddContactDialog()方法弹出AddContactDialogFragment。当用户在AddContactDialogFragment中输入联系人信息并点击确定按钮时,AddContactDialogFragment会调用AddContactPresenter的saveContact()方法,Presenter层再通过IAddContactView的closeAddContactDialog()方法关闭AddContactDialogFragment,并通过ContactListPresenter的loadContactList()方法重新加载联系人列表。
需要注意的是,这只是一个较为复杂的示例,实际的个人信息管理系统可能需要更多的Model层和View层代码,例如数据验证、列表分页、详情查看等功能。同时,Presenter层的单元测试也非常重要,可以使用Mockito等测试框架模拟View层和Model层的行为,确保Presenter层的功能正确性和稳定性。
阅读全文