Witam wszystkich. Piszę w sprawie zostosowania MVP w Androidzie. Czytałem trochę artykułów dot. tego wzorca ale postanowiłem że skonsultuje się z Wami. Poniżej mój kod, czy tak powinno byc?
VIEW
class AboutMeView : Fragment(), AboutMePresenter.View {
lateinit var aboutMePresenter: AboutMePresenter
lateinit var fragmentView: View
lateinit var person: Person
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
fragmentView = inflater.inflate(R.layout.fragment_aboutme, container, false)
person = (activity as MainActivity).person
aboutMePresenter = AboutMePresenter(this)
return fragmentView
}
override fun onResume() {
aboutMePresenter.updateName(person)
aboutMePresenter.updateBirthdate(person)
aboutMePresenter.updateCity(person)
aboutMePresenter.updateCountry(person)
aboutMePresenter.updateEmail(person)
aboutMePresenter.updatePhone(person)
aboutMePresenter.updateDescription(person)
super.onResume()
}
override fun updateFullNameView(fullName: String) {
fragmentView.person_name.text = fullName
}
override fun updateBirthdateView(birthDate: String) {
fragmentView.person_birth_date.text = birthDate
}
override fun updateCityView(city: String) {
fragmentView.person_city.text = city
}
override fun updateCountryView(country: String) {
fragmentView.person_country.text = country
}
override fun updateEmailView(email: String) {
fragmentView.person_email.text = email
}
override fun updatePhoneView(phone: String) {
fragmentView.person_phone.text = phone
}
override fun updateDescriptionView(desc: String) {
fragmentView.person_description.text = desc
}
}
PRESENTER
class AboutMePresenter(val view: View) {
fun updateName(person: Person){
view.updateFullNameView(person.name + " " + person.surname)
}
fun updateBirthdate(person: Person){
view.updateBirthdateView(person.yearOfBirth.toString())
}
fun updateCity(person: Person){
view.updateCityView(person.city)
}
fun updateCountry(person: Person){
view.updateCountryView(person.country)
}
fun updateEmail(person: Person){
view.updateEmailView(person.mail)
}
fun updatePhone(person: Person){
view.updatePhoneView(person.phone)
}
fun updateDescription(person: Person){
view.updateDescriptionView(person.ownDescription)
}
interface View{
fun updateFullNameView(fullName: String)
fun updateBirthdateView(birthDate: String)
fun updateCityView(city: String)
fun updateCountryView(country: String)
fun updateEmailView(email: String)
fun updatePhoneView(phone:String)
fun updateDescriptionView(desc:String)
}
}
MODEL
data class Person(var name: String,
var surname: String,
var city: String,
var country: String,
var yearOfBirth: Int,
var mail: String,
var phone: String,
var ownDescription: String)