Skip to content

Commit

Permalink
Merge pull request #22 from NewMascota/feature/9-init-custom-calendar
Browse files Browse the repository at this point in the history
Feature/9 init custom calendar
  • Loading branch information
mdb1217 authored Feb 19, 2022
2 parents cae63d7 + f86f62b commit 7fc2567
Show file tree
Hide file tree
Showing 18 changed files with 508 additions and 16 deletions.
8 changes: 7 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
<resources>
<string name="app_name">Mascota</string>

<!--calendar-->
<string name="today">오늘</string>
<string name="more">더보기</string>

<!--tools-->
<string name="soeun">최소은</string>
<string name="s">의</string>
<string name="year2021">2021년</string>
<string name="month10">10월</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<item name="android:includeFontPadding">false</item>
</style>

<style name="CalendarDayOfWeekTextStyle" parent="@style/TextAppearance.AppCompat.Caption">
<item name="android:textSize">12sp</item>
<item name="android:fontFamily">@font/noto_sans_kr_regular</item>
</style>

<style name="h1" parent="@style/TextAppearance.AppCompat.Caption">
<item name="android:textSize">18sp</item>
<item name="fontFamily">@font/noto_sans_kr_regular</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.mascota.calendar.databinding.ActivityCalendarBinding
import org.mascota.calendar.ui.view.adapter.YearAdapter
import org.mascota.calendar.ui.view.adapter.YearAdapter.Companion.MORE
import org.mascota.core.base.BindingActivity
import org.mascota.ui.view.custom.adapter.CalendarViewPagerAdapter
import org.mascota.ui.view.custom.adapter.CalendarViewPagerAdapter.Companion.FIRST_POSITION

class CalendarActivity : BindingActivity<ActivityCalendarBinding>(R.layout.activity_calendar) {
private val calendar = Calendar.getInstance()
Expand Down Expand Up @@ -37,8 +39,8 @@ class CalendarActivity : BindingActivity<ActivityCalendarBinding>(R.layout.activ
MORE
)

yearAdapter.setItemClickListener {
with(binding) {
with(binding) {
yearAdapter.setItemClickListener {
with(tvYear) {
if (it != MORE) {
text = "${it}"
Expand All @@ -56,9 +58,14 @@ class CalendarActivity : BindingActivity<ActivityCalendarBinding>(R.layout.activ
ibMore.isSelected = !ibMore.isSelected
isListVisible = false
}
}

binding.rvYear.adapter = yearAdapter
rvYear.adapter = yearAdapter

with(vpCalendar) {
adapter = CalendarViewPagerAdapter()
setCurrentItem(FIRST_POSITION, false)
}
}
}

private fun initUi() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.mascota.ui.view.custom.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import java.util.Calendar
import java.util.Locale
import org.mascota.ui.view.custom.calendar.CalendarView

class CalendarViewPagerAdapter :
RecyclerView.Adapter<CalendarViewPagerAdapter.CalendarViewHolder>() {
override fun getItemCount(): Int = MAX_ITEM_COUNT

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CalendarViewHolder {
return CalendarViewHolder(
CalendarView(parent.context).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
}
)
}

override fun onBindViewHolder(
holder: CalendarViewHolder,
position: Int
) {
holder.bind(position)
}

class CalendarViewHolder(private val view: CalendarView) : RecyclerView.ViewHolder(view) {
fun bind(position: Int) {

view.setCalendarGetter {
return@setCalendarGetter Calendar.getInstance(Locale.KOREA).apply {
set(Calendar.DAY_OF_MONTH, 1)
add(Calendar.MONTH, position - FIRST_POSITION)
}
}
}
}

companion object {
const val MAX_ITEM_COUNT = Int.MAX_VALUE
const val FIRST_POSITION = MAX_ITEM_COUNT / 2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package org.mascota.ui.view.custom.calendar

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.view.ViewCompat
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import java.util.Calendar
import org.mascota.calendar.R
import org.mascota.calendar.databinding.ItemDateBinding
import org.mascota.calendar.databinding.LayoutCalendarTopBinding

class CalendarView(context: Context, attrs: AttributeSet? = null) : LinearLayout(context, attrs) {
private val calendarTopView
get() = createCalendarTopView()

private lateinit var layoutCalendarTopBinding: LayoutCalendarTopBinding

// private var dateItemGetter: (() -> List<Pair<Boolean, Boolean>>)? = null
private var calendarGetter: (() -> Calendar)? = null

private val dateItems = (1..42).map { _ ->
ItemDateBinding.inflate(LayoutInflater.from(context), null, false).apply {
root.id = ViewCompat.generateViewId()
}
}

fun setCalendarGetter(listener: () -> Calendar) {
calendarGetter = listener
}

// fun setDateItemGetter(listener: () -> List<Pair<Boolean, Boolean>>) {
// dateItemGetter = listener
// }

private val outerLinearLayout = LinearLayout(context).apply {
id = ViewCompat.generateViewId()
orientation = VERTICAL
weightSum = 6f

layoutParams = LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
)
}

private val innerLinearLayouts = (1..6).map {
LinearLayout(context).apply {
id = ViewCompat.generateViewId()
orientation = HORIZONTAL
weightSum = 7f

layoutParams = LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
1f
)
}
}

init {
orientation = VERTICAL
addView(calendarTopView)
addView(outerLinearLayout)
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
updateUIWithDate()
}

private fun updateUIWithDate() {
// val dateItem = dateItemGetter?.invoke()
val compCalendar = requireNotNull(calendarGetter?.invoke())
val curMonth = compCalendar.get(Calendar.MONTH)

// if (dateItem != null) {
// index = dateItem.size
// }

layoutCalendarTopBinding.tvMonth.text = "${curMonth + 1}"
compCalendar.set(Calendar.DAY_OF_WEEK, 1)

innerLinearLayouts.forEach {
it.removeAllViews()
}
outerLinearLayout.removeAllViews()

run {
repeat(6) { row ->
val innerLinearLayout = innerLinearLayouts[row]
outerLinearLayout.addView(innerLinearLayout)

repeat(7) { column ->
val idx = row * 7 + column
val item = dateItems[idx]

if (row == 0)
item.line.isVisible = false

if (compCalendar.get(Calendar.MONTH) != curMonth)
item.clDay.isVisible = false
else
item.tvDay.text = compCalendar.get(Calendar.DAY_OF_MONTH).toString()

compCalendar.add(Calendar.DAY_OF_MONTH, 1)

innerLinearLayout.addView(
item.root,
LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
1f
)
)
}

if (compCalendar.get(Calendar.MONTH) == curMonth + 1)
return@run
}
}
}

private fun createCalendarTopView(): View {
val inflater: LayoutInflater = LayoutInflater.from(context)
layoutCalendarTopBinding =
DataBindingUtil.inflate(inflater, R.layout.layout_calendar_top, this, false)

return layoutCalendarTopBinding.root
}
}
10 changes: 10 additions & 0 deletions features/calendar/src/main/res/drawable/ic_empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="21dp"
android:height="21dp"
android:viewportWidth="23"
android:viewportHeight="23">
<path
android:pathData="M10.0316,0.345C10.5125,0.2823 11.0027,0.25 11.5,0.25C11.9973,0.25 12.4875,0.2823 12.9684,0.345C13.3791,0.3986 13.6687,0.775 13.6151,1.1858C13.5615,1.5965 13.1851,1.886 12.7744,1.8325C12.3576,1.7781 11.9323,1.75 11.5,1.75C11.0677,1.75 10.6424,1.7781 10.2256,1.8325C9.8149,1.886 9.4385,1.5965 9.3849,1.1858C9.3313,0.775 9.6209,0.3986 10.0316,0.345ZM14.8265,1.5094C14.9851,1.1268 15.4239,0.9453 15.8066,1.104C16.7216,1.4834 17.5753,1.9799 18.3492,2.5746C18.6777,2.827 18.7393,3.2979 18.4869,3.6263C18.2345,3.9547 17.7637,4.0164 17.4352,3.764C16.7641,3.2482 16.0242,2.8181 15.232,2.4895C14.8494,2.3309 14.6678,1.8921 14.8265,1.5094ZM8.1735,1.5095C8.3322,1.8921 8.1506,2.3309 7.768,2.4895C6.9758,2.8181 6.2359,3.2482 5.5648,3.764C5.2363,4.0164 4.7655,3.9547 4.5131,3.6263C4.2607,3.2979 4.3223,2.827 4.6508,2.5746C5.4247,1.9799 6.2784,1.4834 7.1934,1.104C7.5761,0.9453 8.0149,1.1268 8.1735,1.5095ZM19.3737,4.5131C19.7021,4.2607 20.173,4.3223 20.4254,4.6508C21.0201,5.4247 21.5166,6.2784 21.896,7.1934C22.0547,7.5761 21.8732,8.0149 21.4905,8.1735C21.1079,8.3322 20.6691,8.1506 20.5105,7.768C20.1819,6.9758 19.7518,6.2359 19.236,5.5648C18.9836,5.2363 19.0453,4.7655 19.3737,4.5131ZM3.6263,4.5131C3.9547,4.7655 4.0164,5.2363 3.764,5.5648C3.2482,6.2359 2.8181,6.9758 2.4895,7.768C2.3309,8.1506 1.8921,8.3322 1.5094,8.1735C1.1268,8.0149 0.9453,7.5761 1.104,7.1934C1.4834,6.2784 1.9799,5.4247 2.5746,4.6508C2.827,4.3223 3.2979,4.2607 3.6263,4.5131ZM21.8142,9.3849C22.225,9.3313 22.6014,9.6209 22.655,10.0316C22.7177,10.5125 22.75,11.0027 22.75,11.5C22.75,11.9973 22.7177,12.4875 22.655,12.9684C22.6014,13.3791 22.225,13.6687 21.8142,13.6151C21.4035,13.5615 21.114,13.1851 21.1676,12.7744C21.2219,12.3576 21.25,11.9323 21.25,11.5C21.25,11.0677 21.2219,10.6424 21.1676,10.2256C21.114,9.8149 21.4035,9.4385 21.8142,9.3849ZM1.1858,9.3849C1.5965,9.4385 1.886,9.8149 1.8325,10.2256C1.7781,10.6424 1.75,11.0677 1.75,11.5C1.75,11.9323 1.7781,12.3576 1.8325,12.7744C1.886,13.1851 1.5965,13.5615 1.1858,13.6151C0.775,13.6687 0.3986,13.3791 0.345,12.9684C0.2823,12.4875 0.25,11.9973 0.25,11.5C0.25,11.0027 0.2823,10.5125 0.345,10.0316C0.3986,9.6209 0.775,9.3313 1.1858,9.3849ZM21.4905,14.8265C21.8732,14.9851 22.0547,15.4239 21.896,15.8066C21.5166,16.7216 21.0201,17.5753 20.4254,18.3492C20.173,18.6777 19.7021,18.7393 19.3737,18.4869C19.0453,18.2345 18.9836,17.7637 19.236,17.4352C19.7518,16.7641 20.1819,16.0242 20.5105,15.232C20.6691,14.8494 21.1079,14.6678 21.4905,14.8265ZM1.5095,14.8265C1.8921,14.6678 2.3309,14.8494 2.4895,15.232C2.8181,16.0242 3.2482,16.7641 3.764,17.4352C4.0164,17.7637 3.9547,18.2345 3.6263,18.4869C3.2979,18.7393 2.827,18.6777 2.5746,18.3492C1.9799,17.5753 1.4834,16.7216 1.104,15.8066C0.9453,15.4239 1.1268,14.9851 1.5095,14.8265ZM18.4869,19.3737C18.7393,19.7021 18.6777,20.173 18.3492,20.4254C17.5753,21.0201 16.7216,21.5166 15.8066,21.896C15.4239,22.0547 14.9851,21.8732 14.8265,21.4905C14.6678,21.1079 14.8494,20.6691 15.232,20.5105C16.0242,20.1819 16.7641,19.7518 17.4352,19.236C17.7637,18.9836 18.2345,19.0453 18.4869,19.3737ZM4.5131,19.3737C4.7655,19.0453 5.2363,18.9836 5.5648,19.236C6.2359,19.7518 6.9758,20.1819 7.768,20.5105C8.1506,20.6691 8.3322,21.1079 8.1735,21.4905C8.0149,21.8732 7.5761,22.0547 7.1934,21.896C6.2784,21.5166 5.4247,21.0201 4.6508,20.4254C4.3223,20.173 4.2607,19.7021 4.5131,19.3737ZM9.3849,21.8142C9.4385,21.4035 9.8149,21.114 10.2256,21.1676C10.6424,21.2219 11.0677,21.25 11.5,21.25C11.9323,21.25 12.3576,21.2219 12.7744,21.1676C13.1851,21.114 13.5615,21.4035 13.6151,21.8142C13.6687,22.225 13.3791,22.6014 12.9684,22.655C12.4875,22.7177 11.9973,22.75 11.5,22.75C11.0027,22.75 10.5125,22.7177 10.0316,22.655C9.6209,22.6014 9.3313,22.225 9.3849,21.8142Z"
android:fillColor="#C4C4C4"
android:fillType="evenOdd"/>
</vector>
21 changes: 21 additions & 0 deletions features/calendar/src/main/res/drawable/ic_paw.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="25dp"
android:height="25dp"
android:viewportWidth="25"
android:viewportHeight="25">
<path
android:pathData="M19.7154,16.8021C18.8086,15.5749 17.255,15.3417 16.5634,13.9592C16.4264,13.6667 16.2602,13.3888 16.0675,13.1297C15.653,12.5632 15.1116,12.1016 14.4867,11.7818C13.8618,11.462 13.1707,11.2929 12.4687,11.2881C11.635,11.2945 10.8183,11.5252 10.1044,11.9559C9.6185,12.2745 9.1885,12.6711 8.8317,13.1297C8.6388,13.3889 8.4724,13.6668 8.335,13.9592C7.6434,15.3417 6.0898,15.572 5.1838,16.8021C4.4713,17.7695 4.1731,19.3057 4.7506,20.5049C5.3282,21.704 6.6067,22.153 6.9258,22.2526C8.8165,22.8403 9.5507,21.4585 11.9995,21.5358H12.9019C15.3499,21.4585 16.0848,22.8403 17.9755,22.2526C18.2946,22.153 19.5688,21.7112 20.1507,20.5049C20.7325,19.2985 20.4279,17.7695 19.7154,16.8021Z"
android:fillColor="#E67828"/>
<path
android:pathData="M6.3809,13.7275C7.3677,12.7408 7.1391,10.9124 5.8705,9.6437C4.6018,8.3751 2.7734,8.1465 1.7867,9.1333C0.7999,10.12 1.0285,11.9484 2.2971,13.2171C3.5658,14.4857 5.3942,14.7143 6.3809,13.7275Z"
android:fillColor="#E67828"/>
<path
android:pathData="M22.7027,13.2171C23.9714,11.9484 24.2,10.12 23.2132,9.1333C22.2265,8.1466 20.3981,8.3751 19.1294,9.6438C17.8607,10.9125 17.6322,12.7408 18.6189,13.7276C19.6057,14.7143 21.4341,14.4858 22.7027,13.2171Z"
android:fillColor="#E67828"/>
<path
android:pathData="M9.6219,10.4056C11.1044,10.1973 12.0891,8.4831 11.8212,6.5768C11.5533,4.6706 10.1343,3.2943 8.6517,3.5026C7.1691,3.711 6.1845,5.4252 6.4524,7.3314C6.7203,9.2376 8.1393,10.614 9.6219,10.4056Z"
android:fillColor="#E67828"/>
<path
android:pathData="M18.5463,7.3311C18.8142,5.4249 17.8295,3.7107 16.347,3.5023C14.8644,3.294 13.4454,4.6703 13.1775,6.5766C12.9096,8.4828 13.8942,10.197 15.3768,10.4053C16.8594,10.6137 18.2784,9.2373 18.5463,7.3311Z"
android:fillColor="#E67828"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/maco_light_gray2" />
<stroke
android:width="1dp"
android:color="@color/maco_light_gray" />
<corners android:radius="8dp" />
</shape>
17 changes: 16 additions & 1 deletion features/calendar/src/main/res/layout/activity_calendar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,27 @@
android:background="@drawable/rectangle_fill_ivory_8"
android:elevation="3dp"
android:visibility="@{isListVisible? View.VISIBLE : View.GONE}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="@+id/cl_year"
app:layout_constraintStart_toStartOf="@+id/cl_year"
app:layout_constraintTop_toBottomOf="@+id/cl_year"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="3"
tools:listitem="@layout/item_year" />

<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp_calendar"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="16dp"
android:orientation="vertical"
android:clipToPadding="false"
android:clipChildren="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line2" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Loading

0 comments on commit 7fc2567

Please sign in to comment.