mirror of
https://github.com/szkolny-eu/szkolny-android.git
synced 2025-01-18 12:56:45 -06:00
[Architecture] Migrate homework to MVP
This commit is contained in:
parent
eb1984c6b5
commit
e1d902ceb5
@ -1,132 +0,0 @@
|
||||
package pl.szczodrzynski.edziennik.ui.modules.homework;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.PorterDuffColorFilter;
|
||||
import android.os.AsyncTask;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.cardview.widget.CardView;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import pl.szczodrzynski.edziennik.App;
|
||||
import pl.szczodrzynski.edziennik.R;
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull;
|
||||
import pl.szczodrzynski.edziennik.ui.dialogs.event.EventManualDialog;
|
||||
import pl.szczodrzynski.edziennik.ui.modules.home.HomeFragment;
|
||||
import pl.szczodrzynski.edziennik.utils.models.Date;
|
||||
|
||||
import static pl.szczodrzynski.edziennik.utils.Utils.bs;
|
||||
|
||||
public class HomeworkAdapter extends RecyclerView.Adapter<HomeworkAdapter.ViewHolder> {
|
||||
private Context context;
|
||||
private List<EventFull> homeworkList;
|
||||
|
||||
//getting the context and product list with constructor
|
||||
public HomeworkAdapter(Context mCtx, List<EventFull> homeworkList) {
|
||||
this.context = mCtx;
|
||||
this.homeworkList = homeworkList;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
//inflating and returning our view holder
|
||||
LayoutInflater inflater = LayoutInflater.from(context);
|
||||
View view = inflater.inflate(R.layout.row_homework_item, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
public static String dayDiffString(Context context, int dayDiff) {
|
||||
if (dayDiff > 0) {
|
||||
if (dayDiff == 1) {
|
||||
return context.getString(R.string.tomorrow);
|
||||
}
|
||||
else if (dayDiff == 2) {
|
||||
return context.getString(R.string.the_day_after);
|
||||
}
|
||||
return HomeFragment.plural(context, R.plurals.time_till_days, Math.abs(dayDiff));
|
||||
}
|
||||
else if (dayDiff < 0) {
|
||||
if (dayDiff == -1) {
|
||||
return context.getString(R.string.yesterday);
|
||||
}
|
||||
else if (dayDiff == -2) {
|
||||
return context.getString(R.string.the_day_before);
|
||||
}
|
||||
return context.getString(R.string.ago_format, HomeFragment.plural(context, R.plurals.time_till_days, Math.abs(dayDiff)));
|
||||
}
|
||||
return context.getString(R.string.today);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
App app = (App) context.getApplicationContext();
|
||||
|
||||
EventFull homework = homeworkList.get(position);
|
||||
|
||||
int diffDays = Date.diffDays(homework.eventDate, Date.getToday());
|
||||
|
||||
holder.homeworkItemHomeworkDate.setText(app.getString(R.string.date_relative_format, homework.eventDate.getFormattedString(), dayDiffString(context, diffDays)));
|
||||
holder.homeworkItemTopic.setText(homework.topic);
|
||||
holder.homeworkItemSubjectTeacher.setText(context.getString(R.string.homework_subject_teacher_format, bs(homework.subjectLongName), bs(homework.teacherFullName)));
|
||||
holder.homeworkItemTeamDate.setText(context.getString(R.string.homework_team_date_format, bs(homework.teamName), Date.fromMillis(homework.addedDate).getFormattedStringShort()));
|
||||
|
||||
if (!homework.seen) {
|
||||
holder.homeworkItemTopic.setBackground(context.getResources().getDrawable(R.drawable.bg_rounded_8dp));
|
||||
holder.homeworkItemTopic.getBackground().setColorFilter(new PorterDuffColorFilter(0x692196f3, PorterDuff.Mode.MULTIPLY));
|
||||
homework.seen = true;
|
||||
AsyncTask.execute(() -> {
|
||||
app.db.metadataDao().setSeen(App.profileId, homework, true);
|
||||
});
|
||||
}
|
||||
else {
|
||||
holder.homeworkItemTopic.setBackground(null);
|
||||
}
|
||||
|
||||
holder.homeworkItemEdit.setVisibility((homework.addedManually ? View.VISIBLE : View.GONE));
|
||||
holder.homeworkItemEdit.setOnClickListener(v -> {
|
||||
new EventManualDialog(context).show(app, homework, null, null, EventManualDialog.DIALOG_HOMEWORK);
|
||||
});
|
||||
|
||||
if (homework.sharedBy == null) {
|
||||
holder.homeworkItemSharedBy.setVisibility(View.GONE);
|
||||
}
|
||||
else if (homework.sharedByName != null) {
|
||||
holder.homeworkItemSharedBy.setText(app.getString(R.string.event_shared_by_format, (homework.sharedBy.equals("self") ? app.getString(R.string.event_shared_by_self) : homework.sharedByName)));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return homeworkList.size();
|
||||
}
|
||||
|
||||
class ViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
CardView homeworkItemCard;
|
||||
TextView homeworkItemTopic;
|
||||
TextView homeworkItemHomeworkDate;
|
||||
TextView homeworkItemSharedBy;
|
||||
TextView homeworkItemSubjectTeacher;
|
||||
TextView homeworkItemTeamDate;
|
||||
Button homeworkItemEdit;
|
||||
|
||||
ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
homeworkItemCard = itemView.findViewById(R.id.homeworkItemCard);
|
||||
homeworkItemTopic = itemView.findViewById(R.id.homeworkItemTopic);
|
||||
homeworkItemHomeworkDate = itemView.findViewById(R.id.homeworkItemHomeworkDate);
|
||||
homeworkItemSharedBy = itemView.findViewById(R.id.homeworkItemSharedBy);
|
||||
homeworkItemSubjectTeacher = itemView.findViewById(R.id.homeworkItemSubjectTeacher);
|
||||
homeworkItemTeamDate = itemView.findViewById(R.id.homeworkItemTeamDate);
|
||||
homeworkItemEdit = itemView.findViewById(R.id.homeworkItemEdit);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,84 +1,76 @@
|
||||
package pl.szczodrzynski.edziennik.ui.modules.homework
|
||||
|
||||
import android.os.AsyncTask
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.viewpager.widget.ViewPager
|
||||
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
|
||||
import pl.szczodrzynski.edziennik.App
|
||||
import pl.szczodrzynski.edziennik.MainActivity
|
||||
import pl.szczodrzynski.edziennik.R
|
||||
import pl.szczodrzynski.edziennik.databinding.FragmentHomeworkBinding
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
|
||||
import pl.szczodrzynski.edziennik.ui.base.BaseFragment
|
||||
import pl.szczodrzynski.edziennik.ui.dialogs.event.EventManualDialog
|
||||
import pl.szczodrzynski.edziennik.ui.modules.homework.list.HomeworkListFragment
|
||||
import pl.szczodrzynski.edziennik.ui.modules.messages.MessagesFragment
|
||||
import pl.szczodrzynski.edziennik.utils.Themes
|
||||
import pl.szczodrzynski.navlib.bottomsheet.items.BottomSheetPrimaryItem
|
||||
import pl.szczodrzynski.navlib.bottomsheet.items.BottomSheetSeparatorItem
|
||||
|
||||
class HomeworkFragment : Fragment() {
|
||||
class HomeworkFragment : BaseFragment<HomeworkPresenter>(), HomeworkView {
|
||||
companion object {
|
||||
var pageSelection = 0
|
||||
}
|
||||
|
||||
private lateinit var app: App
|
||||
override lateinit var app: App
|
||||
|
||||
private lateinit var activity: MainActivity
|
||||
private lateinit var b: FragmentHomeworkBinding
|
||||
|
||||
override val presenter: HomeworkPresenter = HomeworkPresenter()
|
||||
|
||||
override val markAsReadSuccessString: String
|
||||
get() = getString(R.string.main_menu_mark_as_read_success)
|
||||
|
||||
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
||||
super.onActivityCreated(savedInstanceState)
|
||||
presenter.onAttachView(this)
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
activity = (getActivity() as MainActivity?) ?: return null
|
||||
if (context == null)
|
||||
return null
|
||||
activity = (getActivity() as MainActivity)
|
||||
app = activity.application as App
|
||||
|
||||
context!!.theme.applyStyle(Themes.appTheme, true)
|
||||
if (app.profile == null)
|
||||
return inflater.inflate(R.layout.fragment_loading, container, false)
|
||||
// activity, context and profile is valid
|
||||
b = FragmentHomeworkBinding.inflate(inflater)
|
||||
b.refreshLayout.setParent(activity.swipeRefreshLayout)
|
||||
return b.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
override fun initView() {
|
||||
// TODO check if app, activity, b can be null
|
||||
if (app.profile == null || !isAdded)
|
||||
return
|
||||
|
||||
b.refreshLayout.setParent(activity.swipeRefreshLayout)
|
||||
|
||||
activity.bottomSheet.prependItems(
|
||||
BottomSheetPrimaryItem(true)
|
||||
.withTitle(R.string.menu_add_event)
|
||||
.withDescription(R.string.menu_add_event_desc)
|
||||
.withIcon(CommunityMaterial.Icon.cmd_calendar_plus)
|
||||
.withOnClickListener(View.OnClickListener {
|
||||
activity.bottomSheet.close()
|
||||
EventManualDialog(activity).show(app, null, null, null, EventManualDialog.DIALOG_HOMEWORK)
|
||||
}),
|
||||
BottomSheetSeparatorItem(true),
|
||||
BottomSheetPrimaryItem(true)
|
||||
.withTitle(R.string.menu_mark_as_read)
|
||||
.withIcon(CommunityMaterial.Icon.cmd_eye_check)
|
||||
.withOnClickListener(View.OnClickListener {
|
||||
activity.bottomSheet.close()
|
||||
AsyncTask.execute { app.db.metadataDao().setAllSeen(App.profileId, Metadata.TYPE_HOMEWORK, true) }
|
||||
Toast.makeText(activity, R.string.main_menu_mark_as_read_success, Toast.LENGTH_SHORT).show()
|
||||
}))
|
||||
.withTitle(R.string.menu_add_event)
|
||||
.withDescription(R.string.menu_add_event_desc)
|
||||
.withIcon(CommunityMaterial.Icon.cmd_calendar_plus)
|
||||
.withOnClickListener(View.OnClickListener { presenter.onAddEventClick() }),
|
||||
BottomSheetSeparatorItem(true),
|
||||
BottomSheetPrimaryItem(true)
|
||||
.withTitle(R.string.menu_mark_as_read)
|
||||
.withIcon(CommunityMaterial.Icon.cmd_eye_check)
|
||||
.withOnClickListener(View.OnClickListener { presenter.onMarkAsReadClick() }))
|
||||
|
||||
b.viewPager.adapter = MessagesFragment.Adapter(childFragmentManager).also { adapter ->
|
||||
adapter.addFragment(HomeworkListFragment().also { fragment ->
|
||||
fragment.arguments = Bundle().also { args ->
|
||||
args.putInt("homeworkDate", HomeworkDate.CURRENT)
|
||||
}
|
||||
}, getString(R.string.homework_tab_current))
|
||||
|
||||
adapter.addFragment(HomeworkListFragment().also { fragment ->
|
||||
fragment.arguments = Bundle().also { args ->
|
||||
args.putInt("homeworkDate", HomeworkDate.PAST)
|
||||
}
|
||||
}, getString(R.string.homework_tab_past))
|
||||
adapter.addFragment(HomeworkListFragment.newInstance(HomeworkDate.CURRENT), getString(R.string.homework_tab_current))
|
||||
adapter.addFragment(HomeworkListFragment.newInstance(HomeworkDate.PAST), getString(R.string.homework_tab_past))
|
||||
}
|
||||
|
||||
b.viewPager.currentItem = pageSelection
|
||||
@ -86,9 +78,7 @@ class HomeworkFragment : Fragment() {
|
||||
b.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
|
||||
override fun onPageScrollStateChanged(state: Int) {}
|
||||
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
|
||||
override fun onPageSelected(position: Int) {
|
||||
pageSelection = position
|
||||
}
|
||||
override fun onPageSelected(position: Int) = presenter.onPageSelected(position)
|
||||
})
|
||||
|
||||
b.tabLayout.setupWithViewPager(b.viewPager)
|
||||
@ -96,11 +86,21 @@ class HomeworkFragment : Fragment() {
|
||||
activity.navView.bottomBar.fabEnable = true
|
||||
activity.navView.bottomBar.fabExtendedText = getString(R.string.add)
|
||||
activity.navView.bottomBar.fabIcon = CommunityMaterial.Icon2.cmd_plus
|
||||
activity.navView.setFabOnClickListener(View.OnClickListener {
|
||||
EventManualDialog(activity).show(app, null, null, null, EventManualDialog.DIALOG_HOMEWORK)
|
||||
})
|
||||
activity.navView.setFabOnClickListener(View.OnClickListener { presenter.onHomeworkAddFabClick() })
|
||||
|
||||
activity.gainAttention()
|
||||
activity.gainAttentionFAB()
|
||||
}
|
||||
|
||||
override fun setPageSelection(position: Int) {
|
||||
pageSelection = position
|
||||
}
|
||||
|
||||
override fun closeBottomSheet() {
|
||||
activity.bottomSheet.close()
|
||||
}
|
||||
|
||||
override fun showAddHomeworkDialog() {
|
||||
EventManualDialog(activity).show(app, null, null, null, EventManualDialog.DIALOG_HOMEWORK)
|
||||
}
|
||||
}
|
||||
|
@ -1,77 +0,0 @@
|
||||
package pl.szczodrzynski.edziennik.ui.modules.homework
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import pl.szczodrzynski.edziennik.App
|
||||
import pl.szczodrzynski.edziennik.MainActivity
|
||||
import pl.szczodrzynski.edziennik.R
|
||||
import pl.szczodrzynski.edziennik.databinding.HomeworkListBinding
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.Event
|
||||
import pl.szczodrzynski.edziennik.getInt
|
||||
import pl.szczodrzynski.edziennik.utils.models.Date
|
||||
import pl.szczodrzynski.edziennik.utils.Themes
|
||||
|
||||
class HomeworkListFragment : Fragment() {
|
||||
|
||||
private lateinit var app: App
|
||||
private lateinit var activity: MainActivity
|
||||
private lateinit var b: HomeworkListBinding
|
||||
|
||||
private var homeworkDate = HomeworkDate.CURRENT
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
activity = (getActivity() as MainActivity?) ?: return null
|
||||
if (context == null)
|
||||
return null
|
||||
app = activity.application as App
|
||||
context!!.theme.applyStyle(Themes.appTheme, true)
|
||||
if (app.profile == null)
|
||||
return inflater.inflate(R.layout.fragment_loading, container, false)
|
||||
// activity, context and profile is valid
|
||||
b = HomeworkListBinding.inflate(inflater)
|
||||
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
|
||||
|
||||
if (arguments != null) {
|
||||
homeworkDate = arguments.getInt("homeworkDate", HomeworkDate.CURRENT)
|
||||
}
|
||||
|
||||
val layoutManager = LinearLayoutManager(context)
|
||||
layoutManager.reverseLayout = true
|
||||
layoutManager.stackFromEnd = true
|
||||
|
||||
b.homeworkView.setHasFixedSize(true)
|
||||
b.homeworkView.layoutManager = layoutManager
|
||||
|
||||
val filter = when(homeworkDate) {
|
||||
HomeworkDate.CURRENT -> "eventDate > '" + Date.getToday().stringY_m_d + "'"
|
||||
else -> "eventDate <= '" + Date.getToday().stringY_m_d + "'"
|
||||
}
|
||||
|
||||
app.db.eventDao()
|
||||
.getAllByType(App.profileId, Event.TYPE_HOMEWORK, filter)
|
||||
.observe(this, Observer { homeworkList ->
|
||||
if (app.profile == null || !isAdded) return@Observer
|
||||
|
||||
if (homeworkList != null && homeworkList.size > 0) {
|
||||
val adapter = HomeworkAdapter(context, homeworkList)
|
||||
b.homeworkView.adapter = adapter
|
||||
b.homeworkView.visibility = View.VISIBLE
|
||||
b.homeworkNoData.visibility = View.GONE
|
||||
} else {
|
||||
b.homeworkView.visibility = View.GONE
|
||||
b.homeworkNoData.visibility = View.VISIBLE
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) Kacper Ziubryniewicz 2019-11-4
|
||||
*/
|
||||
|
||||
package pl.szczodrzynski.edziennik.ui.modules.homework
|
||||
|
||||
import android.os.AsyncTask
|
||||
import pl.szczodrzynski.edziennik.App
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
|
||||
import pl.szczodrzynski.edziennik.ui.base.BasePresenter
|
||||
|
||||
class HomeworkPresenter : BasePresenter<HomeworkView>() {
|
||||
|
||||
override fun onAttachView(view: HomeworkView) {
|
||||
super.onAttachView(view)
|
||||
view.initView()
|
||||
}
|
||||
|
||||
fun onPageSelected(position: Int) {
|
||||
view?.setPageSelection(position)
|
||||
}
|
||||
|
||||
fun onAddEventClick() {
|
||||
view?.apply {
|
||||
closeBottomSheet()
|
||||
showAddHomeworkDialog()
|
||||
}
|
||||
}
|
||||
|
||||
fun onMarkAsReadClick() {
|
||||
view?.apply {
|
||||
closeBottomSheet()
|
||||
AsyncTask.execute {
|
||||
app.db.metadataDao().setAllSeen(App.profileId, Metadata.TYPE_HOMEWORK, true)
|
||||
}
|
||||
showMessage(markAsReadSuccessString)
|
||||
}
|
||||
}
|
||||
|
||||
fun onHomeworkAddFabClick() {
|
||||
view?.showAddHomeworkDialog()
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) Kacper Ziubryniewicz 2019-11-4
|
||||
*/
|
||||
|
||||
package pl.szczodrzynski.edziennik.ui.modules.homework
|
||||
|
||||
import pl.szczodrzynski.edziennik.App
|
||||
import pl.szczodrzynski.edziennik.ui.base.BaseView
|
||||
|
||||
interface HomeworkView : BaseView {
|
||||
|
||||
var app: App
|
||||
|
||||
val markAsReadSuccessString: String
|
||||
|
||||
fun initView()
|
||||
|
||||
fun closeBottomSheet()
|
||||
|
||||
fun showAddHomeworkDialog()
|
||||
|
||||
fun setPageSelection(position: Int)
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright (c) Kacper Ziubryniewicz 2019-11-4
|
||||
*/
|
||||
|
||||
package pl.szczodrzynski.edziennik.ui.modules.homework.list
|
||||
|
||||
import android.content.res.Resources
|
||||
import android.graphics.PorterDuff
|
||||
import android.graphics.PorterDuffColorFilter
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.AsyncTask
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View.GONE
|
||||
import android.view.View.VISIBLE
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import pl.szczodrzynski.edziennik.App
|
||||
import pl.szczodrzynski.edziennik.R
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull
|
||||
import pl.szczodrzynski.edziennik.databinding.RowHomeworkItemBinding
|
||||
import pl.szczodrzynski.edziennik.utils.Utils.bs
|
||||
import pl.szczodrzynski.edziennik.utils.models.Date
|
||||
import kotlin.math.abs
|
||||
|
||||
class HomeworkListAdapter : RecyclerView.Adapter<HomeworkListAdapter.ViewHolder>() {
|
||||
|
||||
val homeworkList: MutableList<EventFull> = mutableListOf()
|
||||
lateinit var onItemEditClick: (homework: EventFull) -> Unit
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val inflater = LayoutInflater.from(parent.context)
|
||||
val b: RowHomeworkItemBinding = DataBindingUtil.inflate(inflater, R.layout.row_homework_item, parent, false)
|
||||
return ViewHolder(b)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val homework = homeworkList[position]
|
||||
|
||||
holder.apply {
|
||||
val diffDaysString = dateDiffString(Date.diffDays(homework.eventDate, Date.getToday()))
|
||||
|
||||
b.homeworkItemHomeworkDate.text = getString(R.string.date_relative_format, homework.eventDate.formattedString, diffDaysString)
|
||||
b.homeworkItemTopic.text = homework.topic
|
||||
b.homeworkItemSubjectTeacher.text = getString(R.string.homework_subject_teacher_format, bs(homework.subjectLongName), bs(homework.teacherFullName))
|
||||
b.homeworkItemTeamDate.text = getString(R.string.homework_team_date_format, bs(homework.teamName), Date.fromMillis(homework.addedDate).formattedStringShort)
|
||||
|
||||
when {
|
||||
!homework.seen -> {
|
||||
b.homeworkItemTopic.apply {
|
||||
background = getDrawable(R.drawable.bg_rounded_8dp)
|
||||
background.colorFilter = PorterDuffColorFilter(0x692196f3, PorterDuff.Mode.MULTIPLY)
|
||||
}
|
||||
|
||||
homework.seen = true
|
||||
AsyncTask.execute {
|
||||
app.db.metadataDao().setSeen(App.profileId, homework, true)
|
||||
}
|
||||
}
|
||||
else -> b.homeworkItemTopic.background = null
|
||||
}
|
||||
|
||||
b.homeworkItemEdit.apply {
|
||||
visibility = if (homework.addedManually) VISIBLE else GONE
|
||||
setOnClickListener { onItemEditClick(homework) }
|
||||
}
|
||||
|
||||
b.homeworkItemSharedBy.apply {
|
||||
when {
|
||||
homework.sharedBy == null -> visibility = GONE
|
||||
homework.sharedByName != null -> text = getString(R.string.event_shared_by_format,
|
||||
when (homework.sharedBy == "self") {
|
||||
true -> getString(R.string.event_shared_by_self)
|
||||
else -> homework.sharedByName
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemCount() = homeworkList.size
|
||||
|
||||
class ViewHolder(val b: RowHomeworkItemBinding) : RecyclerView.ViewHolder(b.root) {
|
||||
fun getString(resId: Int): String = itemView.context.getString(resId)
|
||||
fun getString(resId: Int, vararg formatArgs: Any): String = itemView.context.getString(resId, *formatArgs)
|
||||
fun getDrawable(resId: Int): Drawable? = ContextCompat.getDrawable(itemView.context, resId)
|
||||
|
||||
val app: App get() = itemView.context.applicationContext as App
|
||||
val resources: Resources get() = itemView.context.resources
|
||||
|
||||
fun dateDiffString(diff: Int): String {
|
||||
return when {
|
||||
diff > 0 -> when (diff) {
|
||||
1 -> getString(R.string.tomorrow)
|
||||
2 -> getString(R.string.the_day_after)
|
||||
else -> resources.getQuantityString(R.plurals.time_till_days, abs(diff), abs(diff))
|
||||
}
|
||||
diff < 0 -> when (diff) {
|
||||
-1 -> getString(R.string.yesterday)
|
||||
-2 -> getString(R.string.the_day_before)
|
||||
else -> getString(R.string.ago_format, resources.getQuantityString(R.plurals.time_till_days, abs(diff), abs(diff)))
|
||||
}
|
||||
else -> getString(R.string.today)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (c) Kacper Ziubryniewicz 2019-11-4
|
||||
*/
|
||||
|
||||
package pl.szczodrzynski.edziennik.ui.modules.homework.list
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.View.GONE
|
||||
import android.view.View.VISIBLE
|
||||
import android.view.ViewGroup
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import pl.szczodrzynski.edziennik.App
|
||||
import pl.szczodrzynski.edziennik.MainActivity
|
||||
import pl.szczodrzynski.edziennik.R
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull
|
||||
import pl.szczodrzynski.edziennik.databinding.HomeworkListBinding
|
||||
import pl.szczodrzynski.edziennik.ui.base.BaseFragment
|
||||
import pl.szczodrzynski.edziennik.ui.dialogs.event.EventManualDialog
|
||||
import pl.szczodrzynski.edziennik.utils.Themes
|
||||
|
||||
class HomeworkListFragment : BaseFragment<HomeworkListPresenter>(), HomeworkListView {
|
||||
|
||||
override lateinit var app: App
|
||||
|
||||
private lateinit var activity: MainActivity
|
||||
private lateinit var b: HomeworkListBinding
|
||||
|
||||
private lateinit var homeworkAdapter: HomeworkListAdapter
|
||||
|
||||
override val presenter: HomeworkListPresenter = HomeworkListPresenter()
|
||||
|
||||
override val viewLifecycle: Lifecycle
|
||||
get() = lifecycle
|
||||
|
||||
companion object {
|
||||
private const val ARGUMENT_KEY = "homeworkDate"
|
||||
|
||||
fun newInstance(homeworkDate: Int) = HomeworkListFragment().apply {
|
||||
arguments = Bundle().apply { putInt(ARGUMENT_KEY, homeworkDate) }
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
||||
super.onActivityCreated(savedInstanceState)
|
||||
presenter.onAttachView(this, arguments?.getInt(ARGUMENT_KEY))
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
activity = (getActivity() as MainActivity?) ?: return null
|
||||
if (context == null)
|
||||
return null
|
||||
app = activity.application as App
|
||||
context!!.theme.applyStyle(Themes.appTheme, true)
|
||||
if (app.profile == null)
|
||||
return inflater.inflate(R.layout.fragment_loading, container, false)
|
||||
// activity, context and profile is valid
|
||||
b = HomeworkListBinding.inflate(inflater)
|
||||
return b.root
|
||||
}
|
||||
|
||||
override fun initView() {
|
||||
// TODO check if app, activity, b can be null
|
||||
if (app.profile == null || !isAdded)
|
||||
return
|
||||
|
||||
val layoutManager = LinearLayoutManager(context)
|
||||
layoutManager.reverseLayout = true
|
||||
layoutManager.stackFromEnd = true
|
||||
|
||||
homeworkAdapter = HomeworkListAdapter()
|
||||
homeworkAdapter.onItemEditClick = presenter::onItemEditClick
|
||||
|
||||
b.homeworkView.apply {
|
||||
setHasFixedSize(true)
|
||||
this.layoutManager = layoutManager
|
||||
adapter = homeworkAdapter
|
||||
}
|
||||
}
|
||||
|
||||
override fun updateData(data: List<EventFull>) {
|
||||
homeworkAdapter.apply {
|
||||
homeworkList.apply {
|
||||
clear()
|
||||
addAll(data)
|
||||
}
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
|
||||
override fun showContent(show: Boolean) {
|
||||
b.homeworkView.visibility = if (show) VISIBLE else GONE
|
||||
}
|
||||
|
||||
override fun showNoData(show: Boolean) {
|
||||
b.homeworkNoData.visibility = if (show) VISIBLE else GONE
|
||||
}
|
||||
|
||||
override fun showEditHomeworkDialog(homework: EventFull) {
|
||||
EventManualDialog(context).show(app, homework, null, null, EventManualDialog.DIALOG_HOMEWORK)
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) Kacper Ziubryniewicz 2019-11-4
|
||||
*/
|
||||
|
||||
package pl.szczodrzynski.edziennik.ui.modules.homework.list
|
||||
|
||||
import pl.szczodrzynski.edziennik.App
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.Event
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull
|
||||
import pl.szczodrzynski.edziennik.ui.base.BasePresenter
|
||||
import pl.szczodrzynski.edziennik.ui.modules.homework.HomeworkDate
|
||||
import pl.szczodrzynski.edziennik.utils.models.Date
|
||||
|
||||
class HomeworkListPresenter : BasePresenter<HomeworkListView>() {
|
||||
|
||||
fun onAttachView(view: HomeworkListView, homeworkDate: Int?) {
|
||||
super.onAttachView(view)
|
||||
view.initView()
|
||||
loadData(homeworkDate ?: HomeworkDate.CURRENT)
|
||||
}
|
||||
|
||||
private fun loadData(homeworkDate: Int) {
|
||||
val today = Date.getToday().stringY_m_d
|
||||
|
||||
val filter = when (homeworkDate) {
|
||||
HomeworkDate.CURRENT -> "eventDate > '$today'"
|
||||
else -> "eventDate <= '$today'"
|
||||
}
|
||||
|
||||
view?.run {
|
||||
app.db.eventDao()
|
||||
.getAllByType(App.profileId, Event.TYPE_HOMEWORK, filter)
|
||||
.observe({ viewLifecycle }, { homeworkList ->
|
||||
if (app.profile == null) return@observe
|
||||
|
||||
if (homeworkList != null && homeworkList.size > 0) {
|
||||
updateData(homeworkList)
|
||||
showContent(true)
|
||||
showNoData(false)
|
||||
} else {
|
||||
showContent(false)
|
||||
showNoData(true)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fun onItemEditClick(homework: EventFull) {
|
||||
view?.showEditHomeworkDialog(homework)
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) Kacper Ziubryniewicz 2019-11-4
|
||||
*/
|
||||
|
||||
package pl.szczodrzynski.edziennik.ui.modules.homework.list
|
||||
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import pl.szczodrzynski.edziennik.App
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull
|
||||
import pl.szczodrzynski.edziennik.ui.base.BaseView
|
||||
|
||||
interface HomeworkListView : BaseView {
|
||||
|
||||
var app: App
|
||||
|
||||
val viewLifecycle: Lifecycle
|
||||
|
||||
fun initView()
|
||||
|
||||
fun updateData(data: List<EventFull>)
|
||||
|
||||
fun showContent(show: Boolean)
|
||||
|
||||
fun showNoData(show: Boolean)
|
||||
|
||||
fun showEditHomeworkDialog(homework: EventFull)
|
||||
}
|
@ -1,113 +1,116 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:id="@+id/homeworkItemCard"
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
app:cardElevation="4dp"
|
||||
app:cardCornerRadius="5dp"
|
||||
android:background="?selectableItemBackground"
|
||||
android:paddingLeft="10dp"
|
||||
android:paddingTop="5dp"
|
||||
android:paddingRight="10dp"
|
||||
android:paddingBottom="3dp">
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:id="@+id/homeworkItemCard"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:padding="8dp">
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
android:background="?selectableItemBackground"
|
||||
android:paddingLeft="10dp"
|
||||
android:paddingTop="5dp"
|
||||
android:paddingRight="10dp"
|
||||
android:paddingBottom="3dp"
|
||||
app:cardCornerRadius="5dp"
|
||||
app:cardElevation="4dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="horizontal">
|
||||
android:orientation="vertical"
|
||||
android:padding="8dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/homeworkItemHomeworkDate"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginRight="16dp"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
android:textSize="16sp"
|
||||
tools:text="homeworkDate"
|
||||
android:layout_marginEnd="16dp" />
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<com.mikepenz.iconics.view.IconicsTextView
|
||||
android:id="@+id/homeworkItemSharedBy"
|
||||
<TextView
|
||||
android:id="@+id/homeworkItemHomeworkDate"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
android:textSize="16sp"
|
||||
tools:text="homeworkDate" />
|
||||
|
||||
<com.mikepenz.iconics.view.IconicsTextView
|
||||
android:id="@+id/homeworkItemSharedBy"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:ellipsize="end"
|
||||
android:gravity="end"
|
||||
android:maxLines="1"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/constraintLayout"
|
||||
app:layout_constraintTop_toBottomOf="@+id/eventListItemTopic"
|
||||
tools:text="{cmd-share-variant} przez" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:ellipsize="end"
|
||||
android:gravity="end"
|
||||
android:maxLines="1"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/constraintLayout"
|
||||
app:layout_constraintTop_toBottomOf="@+id/eventListItemTopic"
|
||||
tools:text="{cmd-share-variant} przez" />
|
||||
</LinearLayout>
|
||||
android:orientation="horizontal">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
<TextView
|
||||
android:id="@+id/homeworkItemTopic"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:autoLink="all"
|
||||
android:paddingBottom="4dp"
|
||||
android:textIsSelectable="true"
|
||||
android:textSize="18sp"
|
||||
tools:text="topic???" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/homeworkItemTopic"
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/homeworkItemEdit"
|
||||
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0"
|
||||
android:minHeight="0dp"
|
||||
android:text="@string/homework_edit"
|
||||
android:visibility="visible" /><!--android:minWidth="0dp"
|
||||
android:minHeight="0dp"-->
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:paddingBottom="4dp"
|
||||
android:textSize="18sp"
|
||||
android:textIsSelectable="true"
|
||||
android:autoLink="all"
|
||||
tools:text="topic???" />
|
||||
android:orientation="horizontal">
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/homeworkItemEdit"
|
||||
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0"
|
||||
android:minHeight="0dp"
|
||||
android:text="@string/homework_edit"
|
||||
android:visibility="visible" /><!--android:minWidth="0dp"
|
||||
android:minHeight="0dp"-->
|
||||
<TextView
|
||||
android:id="@+id/homeworkItemSubjectTeacher"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginRight="8dp"
|
||||
android:layout_weight="1"
|
||||
android:ellipsize="end"
|
||||
android:singleLine="true"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
tools:text="subjectName, teacherName" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/homeworkItemTeamDate"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
tools:text="teamName addedDate" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/homeworkItemSubjectTeacher"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginRight="8dp"
|
||||
android:layout_weight="1"
|
||||
android:ellipsize="end"
|
||||
android:singleLine="true"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
tools:text="subjectName, teacherName" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/homeworkItemTeamDate"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
tools:text="teamName addedDate" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</layout>
|
||||
|
Loading…
x
Reference in New Issue
Block a user