diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt index 97a8a6f4..8dfc5099 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt @@ -29,6 +29,9 @@ import com.google.gson.JsonArray import com.google.gson.JsonElement import com.google.gson.JsonObject import im.wangchao.mhttp.Response +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher import pl.szczodrzynski.edziennik.data.db.modules.teams.Team @@ -503,3 +506,15 @@ fun View.findParentById(targetId: Int): View? { } return null } + +fun CoroutineScope.startCoroutineTimer(delayMillis: Long = 0, repeatMillis: Long = 0, action: () -> Unit) = launch { + delay(delayMillis) + if (repeatMillis > 0) { + while (true) { + action() + delay(repeatMillis) + } + } else { + action() + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt index 46e70e04..d1ae3ed3 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt @@ -56,7 +56,7 @@ import pl.szczodrzynski.edziennik.ui.modules.feedback.FeedbackFragment import pl.szczodrzynski.edziennik.ui.modules.feedback.HelpFragment import pl.szczodrzynski.edziennik.ui.modules.grades.GradesFragment import pl.szczodrzynski.edziennik.ui.modules.grades.editor.GradesEditorFragment -import pl.szczodrzynski.edziennik.ui.modules.home.HomeFragment +import pl.szczodrzynski.edziennik.ui.modules.home.HomeFragmentV2 import pl.szczodrzynski.edziennik.ui.modules.homework.HomeworkFragment import pl.szczodrzynski.edziennik.ui.modules.login.LoginActivity import pl.szczodrzynski.edziennik.ui.modules.messages.MessageFragment @@ -122,7 +122,7 @@ class MainActivity : AppCompatActivity() { val list: MutableList = mutableListOf() // home item - list += NavTarget(DRAWER_ITEM_HOME, R.string.menu_home_page, HomeFragment::class) + list += NavTarget(DRAWER_ITEM_HOME, R.string.menu_home_page, HomeFragmentV2::class) .withTitle(R.string.app_name) .withIcon(CommunityMaterial.Icon2.cmd_home_outline) .isInDrawer(true) @@ -702,7 +702,10 @@ class MainActivity : AppCompatActivity() { loadProfile(intentProfileId, intentTargetId, extras) } intentProfileId != -1 -> { - loadProfile(intentProfileId, intentTargetId, extras) + if (app.profile.id != intentProfileId) + loadProfile(intentProfileId, intentTargetId, extras) + else + loadTarget(intentTargetId, extras) } intentTargetId != -1 -> { drawer.currentProfile = app.profile.id diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/task/NotifyTask.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/task/NotifyTask.kt index edd7a6fa..d5806ba0 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/task/NotifyTask.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/task/NotifyTask.kt @@ -9,6 +9,7 @@ import pl.szczodrzynski.edziennik.MainActivity import pl.szczodrzynski.edziennik.Notifier.ID_NOTIFICATIONS import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback +import pl.szczodrzynski.edziennik.data.db.modules.notification.getNotificationTitle import pl.szczodrzynski.edziennik.utils.models.Notification import kotlin.math.min @@ -33,9 +34,9 @@ class NotifyTask : IApiTask(-1) { val pendingIntent = PendingIntent.getActivity(app, notification.id, intent, 0) val notificationBuilder = NotificationCompat.Builder(app, app.notifier.notificationGroup) // title, text, type, date - .setContentTitle(notification.title) + .setContentTitle(notification.profileName) .setContentText(notification.text) - .setSubText(Notification.stringType(app, notification.type)) + .setSubText(app.getNotificationTitle(notification.type)) .setWhen(notification.addedDate) .setTicker(app.getString(R.string.notification_ticker_format, Notification.stringType(app, notification.type))) // icon, color, lights, priority diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt index 414e0c84..e0cea83d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt @@ -375,7 +375,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) } fun shouldSyncLuckyNumber(): Boolean { - return (db.luckyNumberDao().getNearestFutureNow(profileId, Date.getToday()) ?: -1) == -1 + return (db.luckyNumberDao().getNearestFutureNow(profileId, Date.getToday().value) ?: -1) == -1 } fun error(tag: String, errorCode: Int, response: Response? = null, throwable: Throwable? = null, apiResponse: JsonObject? = null) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/luckynumber/LuckyNumberDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/luckynumber/LuckyNumberDao.java index fa80f6f6..4b8f54cd 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/luckynumber/LuckyNumberDao.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/luckynumber/LuckyNumberDao.java @@ -37,7 +37,10 @@ public abstract class LuckyNumberDao { @Nullable @Query("SELECT * FROM luckyNumbers WHERE profileId = :profileId AND luckyNumberDate >= :date ORDER BY luckyNumberDate DESC LIMIT 1") - public abstract LuckyNumber getNearestFutureNow(int profileId, Date date); + public abstract LuckyNumber getNearestFutureNow(int profileId, int date); + + @Query("SELECT * FROM luckyNumbers WHERE profileId = :profileId AND luckyNumberDate >= :date ORDER BY luckyNumberDate DESC LIMIT 1") + public abstract LiveData getNearestFuture(int profileId, int date); @RawQuery(observedEntities = {LuckyNumber.class}) abstract LiveData> getAll(SupportSQLiteQuery query); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/home/StudentNumberDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/home/StudentNumberDialog.kt new file mode 100644 index 00000000..56785d98 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/home/StudentNumberDialog.kt @@ -0,0 +1,46 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-11-24. + */ + +package pl.szczodrzynski.edziennik.ui.dialogs.home + +import android.text.InputType +import android.widget.Toast +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity +import com.afollestad.materialdialogs.MaterialDialog +import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile + +class StudentNumberDialog( + val activity: AppCompatActivity, + val profile: Profile, + val onShowListener: ((tag: String) -> Unit)? = null, + val onDismissListener: ((tag: String) -> Unit)? = null +) { + companion object { + private const val TAG = "StudentNumberDialog" + } + + private lateinit var dialog: AlertDialog + + init { run { + if (activity.isFinishing) + return@run + onShowListener?.invoke(TAG) + MaterialDialog.Builder(activity) + .title(R.string.card_lucky_number_set_title) + .content(R.string.card_lucky_number_set_text) + .inputType(InputType.TYPE_CLASS_NUMBER) + .input(null, if (profile.studentNumber == -1) "" else profile.studentNumber.toString()) { _: MaterialDialog?, input: CharSequence -> + try { + profile.studentNumber = input.toString().toInt() + } catch (e: Exception) { + Toast.makeText(activity, R.string.incorrect_format, Toast.LENGTH_SHORT).show() + } + } + .dismissListener { + onDismissListener?.invoke(TAG) + }.show() + }} +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/CardItemTouchHelperCallback.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/CardItemTouchHelperCallback.kt new file mode 100644 index 00000000..070fb19c --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/CardItemTouchHelperCallback.kt @@ -0,0 +1,49 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-11-23. + */ + +package pl.szczodrzynski.edziennik.ui.modules.home + +import androidx.recyclerview.widget.ItemTouchHelper +import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.card.MaterialCardView +import pl.szczodrzynski.edziennik.ui.modules.home.HomeFragmentV2.Companion.swapCards +import pl.szczodrzynski.edziennik.utils.SwipeRefreshLayoutNoIndicator + +class CardItemTouchHelperCallback(private val cardAdapter: HomeCardAdapter, private val refreshLayout: SwipeRefreshLayoutNoIndicator?) : ItemTouchHelper.Callback() { + companion object { + private const val TAG = "CardItemTouchHelperCallback" + private const val DRAG_FLAGS = ItemTouchHelper.UP or ItemTouchHelper.DOWN + private const val SWIPE_FLAGS = 0 + } + + private var dragCardView: MaterialCardView? = null + + override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int { + return makeMovementFlags(DRAG_FLAGS, SWIPE_FLAGS) + } + + override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { + val fromPosition = viewHolder.adapterPosition + val toPosition = target.adapterPosition + + swapCards(fromPosition, toPosition, cardAdapter) + return true + } + + override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) = Unit + + override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) { + super.onSelectedChanged(viewHolder, actionState) + + if (actionState == ItemTouchHelper.ACTION_STATE_DRAG && viewHolder != null) { + dragCardView = viewHolder.itemView as MaterialCardView + dragCardView?.isDragged = true + refreshLayout?.isEnabled = false + } else if (actionState == ItemTouchHelper.ACTION_STATE_IDLE && dragCardView != null) { + refreshLayout?.isEnabled = true + dragCardView?.isDragged = false + dragCardView = null + } + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeCard.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeCard.kt new file mode 100644 index 00000000..e6f28c68 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeCard.kt @@ -0,0 +1,10 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-11-23. + */ + +package pl.szczodrzynski.edziennik.ui.modules.home + +interface HomeCard { + fun bind(position: Int, holder: HomeCardAdapter.ViewHolder) + fun unbind(position: Int, holder: HomeCardAdapter.ViewHolder) +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeCardAdapter.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeCardAdapter.kt new file mode 100644 index 00000000..05ba72b6 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeCardAdapter.kt @@ -0,0 +1,51 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-11-23. + */ + +package pl.szczodrzynski.edziennik.ui.modules.home + +import android.annotation.SuppressLint +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.ItemTouchHelper +import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.card.MaterialCardView +import pl.szczodrzynski.edziennik.R + +class HomeCardAdapter(var items: MutableList) : RecyclerView.Adapter() { + companion object { + private const val TAG = "HomeCardAdapter" + } + + var itemTouchHelper: ItemTouchHelper? = null + + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + (holder as ViewHolder).bind(itemTouchHelper) + items[position].bind(position, holder) + } + + override fun onViewRecycled(holder: RecyclerView.ViewHolder) { + items.getOrNull(holder.adapterPosition)?.unbind(holder.adapterPosition, holder as ViewHolder) + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { + return ViewHolder( + LayoutInflater.from(parent.context).inflate(R.layout.card_home, parent, false) as MaterialCardView + ) + } + + override fun getItemCount(): Int = items.size + + class ViewHolder(val root: MaterialCardView) : RecyclerView.ViewHolder(root) { + @SuppressLint("ClickableViewAccessibility") + fun bind(itemTouchHelper: ItemTouchHelper?) { + /*root.setOnTouchListener { _: View?, event: MotionEvent -> + if (event.action == MotionEvent.ACTION_DOWN) { + itemTouchHelper?.startDrag(this) + return@setOnTouchListener true + } + false + }*/ + } + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeDummyCard.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeDummyCard.kt new file mode 100644 index 00000000..88564bad --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeDummyCard.kt @@ -0,0 +1,60 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-11-23. + */ + +package pl.szczodrzynski.edziennik.ui.modules.home + +import android.widget.TextView +import androidx.core.view.plusAssign +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch +import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.MainActivity +import pl.szczodrzynski.edziennik.startCoroutineTimer +import kotlin.coroutines.CoroutineContext + +class HomeDummyCard(val id: Int) : HomeCard, CoroutineScope { + companion object { + private const val TAG = "HomeDummyCard" + } + + private lateinit var app: App + private lateinit var activity: MainActivity + + private var job: Job = Job() + override val coroutineContext: CoroutineContext + get() = job + Dispatchers.Main + + var timer: Job? = null + var time = 0 + + override fun bind(position: Int, holder: HomeCardAdapter.ViewHolder) { launch { + holder.root.removeAllViews() + //holder.setIsRecyclable(false) + + val text = TextView(holder.root.context).apply { + text = "This is a card #$id" + } + holder.root += text + + timer = startCoroutineTimer(repeatMillis = 1000) { + time++ + text.text = "Coroutine timer at #$id! $time seconds" + } + + /*val button = MaterialButton(holder.root.context).apply { + setText("Cancel") + onClick { + timer.cancel() + } + } + holder.root += button*/ + }} + + override fun unbind(position: Int, holder: HomeCardAdapter.ViewHolder) { + timer?.cancel() + timer = null + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragmentV2.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragmentV2.kt new file mode 100644 index 00000000..e47108af --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragmentV2.kt @@ -0,0 +1,126 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-11-23. + */ + +package pl.szczodrzynski.edziennik.ui.modules.home + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.view.AccessibilityDelegateCompat +import androidx.core.view.accessibility.AccessibilityNodeInfoCompat +import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat +import androidx.fragment.app.Fragment +import androidx.recyclerview.widget.ItemTouchHelper +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerViewAccessibilityDelegate +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.MainActivity +import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.databinding.FragmentHomeV2Binding +import pl.szczodrzynski.edziennik.ui.modules.home.cards.HomeLuckyNumberCard +import pl.szczodrzynski.edziennik.utils.Themes +import kotlin.coroutines.CoroutineContext + +class HomeFragmentV2 : Fragment(), CoroutineScope { + companion object { + private const val TAG = "HomeFragment" + + fun swapCards(fromPosition: Int, toPosition: Int, cardAdapter: HomeCardAdapter) { + val fromCard = cardAdapter.items[fromPosition] + cardAdapter.items[fromPosition] = cardAdapter.items[toPosition] + cardAdapter.items[toPosition] = fromCard + cardAdapter.notifyItemMoved(fromPosition, toPosition) + } + } + + private lateinit var app: App + private lateinit var activity: MainActivity + private lateinit var b: FragmentHomeV2Binding + + private lateinit var job: Job + override val coroutineContext: CoroutineContext + get() = job + Dispatchers.Main + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + activity = (getActivity() as MainActivity?) ?: return null + context ?: return null + app = activity.application as App + context!!.theme.applyStyle(Themes.appTheme, true) + b = FragmentHomeV2Binding.inflate(inflater) + b.refreshLayout.setParent(activity.swipeRefreshLayout) + job = Job() + return b.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + // TODO check if app, activity, b can be null + if (app.profile == null || !isAdded) + return + + val items = mutableListOf( + HomeLuckyNumberCard(0, app, activity, this, app.profile), + HomeDummyCard(1), + HomeDummyCard(2), + HomeDummyCard(3), + HomeDummyCard(4), + HomeDummyCard(5), + HomeDummyCard(6), + HomeDummyCard(7), + HomeDummyCard(8), + HomeDummyCard(9), + HomeDummyCard(10), + HomeDummyCard(11), + HomeDummyCard(12), + HomeDummyCard(13), + HomeDummyCard(14), + HomeDummyCard(15), + HomeDummyCard(16) + ) + + val adapter = HomeCardAdapter(items) + val itemTouchHelper = ItemTouchHelper(CardItemTouchHelperCallback(adapter, b.refreshLayout)) + adapter.itemTouchHelper = itemTouchHelper + b.list.layoutManager = LinearLayoutManager(activity) + b.list.adapter = adapter + b.list.setAccessibilityDelegateCompat(object : RecyclerViewAccessibilityDelegate(b.list) { + override fun getItemDelegate(): AccessibilityDelegateCompat { + return object : ItemDelegate(this) { + override fun onInitializeAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfoCompat) { + super.onInitializeAccessibilityNodeInfo(host, info) + val position: Int = b.list.getChildLayoutPosition(host) + if (position != 0) { + info.addAction(AccessibilityActionCompat( + R.id.move_card_up_action, + host.resources.getString(R.string.card_action_move_up) + )) + } + if (position != adapter.itemCount - 1) { + info.addAction(AccessibilityActionCompat( + R.id.move_card_down_action, + host.resources.getString(R.string.card_action_move_down) + )) + } + } + + override fun performAccessibilityAction(host: View, action: Int, args: Bundle): Boolean { + val fromPosition: Int = b.list.getChildLayoutPosition(host) + if (action == R.id.move_card_down_action) { + swapCards(fromPosition, fromPosition + 1, adapter) + return true + } else if (action == R.id.move_card_up_action) { + swapCards(fromPosition, fromPosition - 1, adapter) + return true + } + return super.performAccessibilityAction(host, action, args) + } + } + } + }) + itemTouchHelper.attachToRecyclerView(b.list) + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeTimetableCard.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeTimetableCard.kt index 1c1c99e4..23c5c962 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeTimetableCard.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeTimetableCard.kt @@ -33,17 +33,17 @@ class HomeTimetableCard( private val layoutInflater: LayoutInflater, private val insertPoint: ViewGroup ) { + companion object { + private const val TAG = "HomeTimetableCard" + const val TIME_TILL = 0 + const val TIME_LEFT = 1 + } private lateinit var timetableTimer: Timer private lateinit var b: CardTimetableBinding private var bellSyncTime: Time? = null - private companion object { - const val TIME_TILL = 0 - const val TIME_LEFT = 1 - } - private var counterType = TIME_TILL private val counterTarget = Time(0, 0, 0) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/cards/HomeLuckyNumberCard.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/cards/HomeLuckyNumberCard.kt new file mode 100644 index 00000000..fee05477 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/cards/HomeLuckyNumberCard.kt @@ -0,0 +1,107 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-11-24. + */ + +package pl.szczodrzynski.edziennik.ui.modules.home.cards + +import android.view.LayoutInflater +import android.view.ViewGroup.LayoutParams.MATCH_PARENT +import android.view.ViewGroup.LayoutParams.WRAP_CONTENT +import android.widget.FrameLayout +import androidx.core.view.plusAssign +import androidx.core.view.setMargins +import androidx.lifecycle.Observer +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile +import pl.szczodrzynski.edziennik.databinding.CardHomeLuckyNumberBinding +import pl.szczodrzynski.edziennik.ui.dialogs.home.StudentNumberDialog +import pl.szczodrzynski.edziennik.ui.modules.home.HomeCard +import pl.szczodrzynski.edziennik.ui.modules.home.HomeCardAdapter +import pl.szczodrzynski.edziennik.ui.modules.home.HomeFragmentV2 +import pl.szczodrzynski.edziennik.utils.models.Date +import kotlin.coroutines.CoroutineContext + +class HomeLuckyNumberCard( + val id: Int, + val app: App, + val activity: MainActivity, + val fragment: HomeFragmentV2, + val profile: Profile +) : HomeCard, CoroutineScope { + companion object { + private const val TAG = "HomeLuckyNumberCard" + } + + private var job: Job = Job() + override val coroutineContext: CoroutineContext + get() = job + Dispatchers.Main + + override fun bind(position: Int, holder: HomeCardAdapter.ViewHolder) { launch { + holder.root.removeAllViews() + val b = CardHomeLuckyNumberBinding.inflate(LayoutInflater.from(holder.root.context)) + b.root.layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT).apply { + setMargins(8.dp) + } + holder.root += b.root + + val today = Date.getToday() + val todayValue = today.value + + val subTextRes = if (profile.studentNumber == -1) + R.string.home_lucky_number_details_click_to_set + else + R.string.home_lucky_number_details + b.subText.setText(subTextRes, profile.name ?: "", profile.studentNumber) + + app.db.luckyNumberDao().getNearestFuture(App.profileId, todayValue).observe(fragment, Observer { luckyNumber -> + val isYours = luckyNumber?.number == profile.studentNumber + val titleRes = when { + luckyNumber == null -> R.string.home_lucky_number_no_info + luckyNumber.number == -1 -> R.string.home_lucky_number_no_number + else -> when (isYours) { + true -> when (luckyNumber.date.value) { + todayValue -> R.string.home_lucky_number_yours_today + todayValue + 1 -> R.string.home_lucky_number_yours_tomorrow + else -> R.string.home_lucky_number_yours_later + } + false -> when (luckyNumber.date.value) { + todayValue -> R.string.home_lucky_number_today + todayValue + 1 -> R.string.home_lucky_number_tomorrow + else -> R.string.home_lucky_number_later + } + } + } + + b.title.setText( + titleRes, + luckyNumber?.number ?: 0, + luckyNumber?.date?.formattedString ?: "" + ) + + val drawableRes = when { + luckyNumber == null || luckyNumber.number == -1 -> R.drawable.emoji_sad + isYours -> R.drawable.emoji_glasses + !isYours -> R.drawable.emoji_smiling + else -> R.drawable.emoji_no_face + } + b.image.setImageResource(drawableRes) + }) + + holder.root.onClick { + StudentNumberDialog(activity, profile, onDismissListener = { + app.profileSaveAsync(profile) + val newSubTextRes = if (profile.studentNumber == -1) + R.string.home_lucky_number_details_click_to_set + else + R.string.home_lucky_number_details + b.subText.setText(newSubTextRes, profile.name ?: "", profile.studentNumber) + }) + } + }} + + override fun unbind(position: Int, holder: HomeCardAdapter.ViewHolder) = Unit +} \ No newline at end of file diff --git a/app/src/main/res/drawable/emoji_glasses.xml b/app/src/main/res/drawable/emoji_glasses.xml new file mode 100644 index 00000000..88460e0d --- /dev/null +++ b/app/src/main/res/drawable/emoji_glasses.xml @@ -0,0 +1,28 @@ + + + + + + + + + + diff --git a/app/src/main/res/drawable/emoji_neutral.xml b/app/src/main/res/drawable/emoji_neutral.xml new file mode 100644 index 00000000..27f125dd --- /dev/null +++ b/app/src/main/res/drawable/emoji_neutral.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/app/src/main/res/drawable/emoji_no_face.xml b/app/src/main/res/drawable/emoji_no_face.xml new file mode 100644 index 00000000..46a67e51 --- /dev/null +++ b/app/src/main/res/drawable/emoji_no_face.xml @@ -0,0 +1,19 @@ + + + + + + + diff --git a/app/src/main/res/drawable/emoji_sad.xml b/app/src/main/res/drawable/emoji_sad.xml new file mode 100644 index 00000000..d4f0cea0 --- /dev/null +++ b/app/src/main/res/drawable/emoji_sad.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/app/src/main/res/drawable/emoji_smiling.xml b/app/src/main/res/drawable/emoji_smiling.xml new file mode 100644 index 00000000..94fbdbd9 --- /dev/null +++ b/app/src/main/res/drawable/emoji_smiling.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/app/src/main/res/layout/card_home.xml b/app/src/main/res/layout/card_home.xml new file mode 100644 index 00000000..0db20325 --- /dev/null +++ b/app/src/main/res/layout/card_home.xml @@ -0,0 +1,17 @@ + + + + + + + diff --git a/app/src/main/res/layout/card_home_lucky_number.xml b/app/src/main/res/layout/card_home_lucky_number.xml new file mode 100644 index 00000000..8853ff68 --- /dev/null +++ b/app/src/main/res/layout/card_home_lucky_number.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home_v2.xml b/app/src/main/res/layout/fragment_home_v2.xml new file mode 100644 index 00000000..63612c70 --- /dev/null +++ b/app/src/main/res/layout/fragment_home_v2.xml @@ -0,0 +1,20 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml new file mode 100644 index 00000000..51e1215f --- /dev/null +++ b/app/src/main/res/values/ids.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 62bc6661..6521cc66 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1032,4 +1032,16 @@ Usuń wszystkie Wyczyszczono powiadomienia Wybierz dzień + Przesuń w górę + Przesuń w dół + %s • Kliknij, aby ustawić swój numerek. + %s • Numer w dzienniku to %d + Brak informacji o szczęśliwym numerku. + Dzisiaj to Ty masz szczęśliwy numerek! + Jutro to Ty masz szczęśliwy numerek! + Dnia %s Twój numerek jest szczęśliwy. + Dnia %s szczęśliwy numerek to %d. + %d to dzisiejszy szczęśliwy numerek. + %d to szczęśliwy numerek na jutro. + Nie ma dzisiaj szczęśliwego numerka.