diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt index 15c336505..4c0ffd820 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt @@ -14,7 +14,7 @@ class StudentRemote @Inject constructor(private val sdk: Sdk) { private fun mapStudents(students: List, email: String, password: String): List { return students.map { student -> Student( - email = email, + email = email.ifBlank { student.email }, password = password, isParent = student.isParent, symbol = student.symbol, diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/account/Account.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/account/Account.kt new file mode 100644 index 000000000..dbcb499e8 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/account/Account.kt @@ -0,0 +1,3 @@ +package io.github.wulkanowy.ui.modules.account + +data class Account(val email: String, val isParent: Boolean) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountAdapter.kt index 7df0ca378..27915a710 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountAdapter.kt @@ -3,33 +3,72 @@ package io.github.wulkanowy.ui.modules.account import android.annotation.SuppressLint import android.graphics.PorterDuff import android.view.LayoutInflater +import android.view.View.GONE +import android.view.View.VISIBLE import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.databinding.HeaderAccountBinding import io.github.wulkanowy.databinding.ItemAccountBinding +import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.getThemeAttrColor import javax.inject.Inject -class AccountAdapter @Inject constructor() : RecyclerView.Adapter() { +class AccountAdapter @Inject constructor() : RecyclerView.Adapter() { - var items = emptyList() + var items = emptyList>() var onClickListener: (Student) -> Unit = {} override fun getItemCount() = items.size - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ItemViewHolder( - ItemAccountBinding.inflate(LayoutInflater.from(parent.context), parent, false) - ) + override fun getItemViewType(position: Int) = items[position].viewType.id + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { + val inflater = LayoutInflater.from(parent.context) + + return when (viewType) { + AccountItem.ViewType.HEADER.id -> HeaderViewHolder(HeaderAccountBinding.inflate(inflater, parent, false)) + AccountItem.ViewType.ITEM.id -> ItemViewHolder(ItemAccountBinding.inflate(inflater, parent, false)) + else -> throw IllegalStateException() + } + } + + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + when (holder) { + is HeaderViewHolder -> bindHeaderViewHolder(holder.binding, items[position].value as Account) + is ItemViewHolder -> bindItemViewHolder(holder.binding, items[position].value as Student) + } + } + + private fun bindHeaderViewHolder(binding: HeaderAccountBinding, account: Account) { + with(binding) { + accountHeaderEmail.text = account.email + accountHeaderType.setText(if (account.isParent) R.string.account_type_parent else R.string.account_type_student) + } + } @SuppressLint("SetTextI18n") - override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { - val student = items[position] - - with(holder.binding) { + private fun bindItemViewHolder(binding: ItemAccountBinding, student: Student) { + with(binding) { accountItemName.text = "${student.studentName} ${student.className}" accountItemSchool.text = student.schoolName + with(accountItemLoginMode) { + visibility = when (Sdk.Mode.valueOf(student.loginMode)) { + Sdk.Mode.API -> { + setText(R.string.account_login_mobile_api) + VISIBLE + } + Sdk.Mode.HYBRID -> { + setText(R.string.account_login_hybrid) + VISIBLE + } + Sdk.Mode.SCRAPPER -> { + GONE + } + } + } with(accountItemImage) { val colorImage = if (student.isCurrent) context.getThemeAttrColor(R.attr.colorPrimary) @@ -42,5 +81,8 @@ class AccountAdapter @Inject constructor() : RecyclerView.Adapter(), AccountView { } } - override fun updateData(data: List) { + override fun updateData(data: List>) { with(accountAdapter) { items = data notifyDataSetChanged() diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountItem.kt new file mode 100644 index 000000000..05a4a69ce --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountItem.kt @@ -0,0 +1,9 @@ +package io.github.wulkanowy.ui.modules.account + +data class AccountItem(val value: T, val viewType: ViewType) { + + enum class ViewType(val id: Int) { + HEADER(1), + ITEM(2) + } +} diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt index 3416a043f..b5fbcdb63 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt @@ -83,11 +83,20 @@ class AccountPresenter @Inject constructor( } } + private fun createAccountItems(items: List): List> { + return items.groupBy { Account(it.email, it.isParent) }.map { (account, students) -> + listOf(AccountItem(account, AccountItem.ViewType.HEADER)) + students.map { student -> + AccountItem(student, AccountItem.ViewType.ITEM) + } + }.flatten() + } + private fun loadData() { Timber.i("Loading account data started") disposable.add(studentRepository.getSavedStudents(false) .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) + .map { createAccountItems(it) } .subscribe({ Timber.i("Loading account result: Success") view?.updateData(it) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountView.kt index abb9e1d27..a1f8086cd 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountView.kt @@ -1,13 +1,12 @@ package io.github.wulkanowy.ui.modules.account -import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.ui.base.BaseView interface AccountView : BaseView { fun initView() - fun updateData(data: List) + fun updateData(data: List>) fun dismissView() diff --git a/app/src/main/res/layout/dialog_account.xml b/app/src/main/res/layout/dialog_account.xml index 6e975c80d..d56de3a2e 100644 --- a/app/src/main/res/layout/dialog_account.xml +++ b/app/src/main/res/layout/dialog_account.xml @@ -5,45 +5,60 @@ android:layout_width="300dp" android:layout_height="wrap_content"> - - - + android:orientation="vertical"> - + - + + + + + + + + + + + diff --git a/app/src/main/res/layout/header_account.xml b/app/src/main/res/layout/header_account.xml new file mode 100644 index 000000000..6219c26db --- /dev/null +++ b/app/src/main/res/layout/header_account.xml @@ -0,0 +1,28 @@ + + + + + + + diff --git a/app/src/main/res/layout/item_account.xml b/app/src/main/res/layout/item_account.xml index 614e4d2df..9568b345a 100644 --- a/app/src/main/res/layout/item_account.xml +++ b/app/src/main/res/layout/item_account.xml @@ -3,20 +3,17 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" - android:layout_height="56dp" + android:layout_height="wrap_content" android:background="?selectableItemBackground" android:orientation="horizontal" - android:paddingStart="24dp" - android:paddingLeft="24dp" - android:paddingEnd="24dp" - android:paddingRight="24dp" + android:paddingVertical="8dp" + android:paddingHorizontal="16dp" tools:context=".ui.modules.account.AccountAdapter"> + + diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 7f25d8a67..4cb8997cf 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -265,6 +265,10 @@ Wyloguj Czy chcesz wylogować aktualnego ucznia? Wylogowanie ucznia + Konto ucznia + Konto rodzica + Tryb API mobilne + Tryb hybrydowy Wersja aplikacji Twórcy diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7793cd9c2..29e3460b4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -282,6 +282,10 @@ Logout Do you want to log out of an active student? Student logout + Student account + Parent account + Mobile API mode + Hybrid mode