[UI] Migrate MaterialDateTimePicker to material-components.

This commit is contained in:
Kuba Szczodrzyński 2021-03-30 22:55:46 +02:00
parent 37c0ff2ac7
commit 9ba1bf130f
10 changed files with 70 additions and 58 deletions

View File

@ -147,7 +147,6 @@ dependencies {
implementation "com.jaredrummler:colorpicker:1.1.0"
implementation "com.qifan.powerpermission:powerpermission-coroutines:1.3.0"
implementation "com.qifan.powerpermission:powerpermission:1.3.0"
implementation "com.wdullaer:materialdatetimepicker:4.2.3"
implementation "com.yuyh.json:jsonviewer:1.0.6"
implementation "io.coil-kt:coil:1.1.1"
implementation "me.dm7.barcodescanner:zxing:1.9.8"

View File

@ -37,6 +37,7 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.viewpager.widget.ViewPager
import com.google.android.material.button.MaterialButton
import com.google.android.material.datepicker.CalendarConstraints
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.gson.*
import com.google.gson.JsonArray
@ -1256,3 +1257,10 @@ fun Context.getSyncInterval(interval: Int): String {
""
return hoursText?.plus(" $minutesText") ?: minutesText
}
fun Profile.getSchoolYearConstrains(): CalendarConstraints {
return CalendarConstraints.Builder()
.setStart(dateSemester1Start.inMillisUtc)
.setEnd(dateYearEnd.inMillisUtc)
.build()
}

View File

@ -18,8 +18,8 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.core.content.FileProvider
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog
import kotlinx.coroutines.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
@ -116,16 +116,16 @@ class GenerateBlockTimetableDialog(
}}
private fun selectDate() {
val date = Date.getToday()
DatePickerDialog
.newInstance({ _, year, monthOfYear, dayOfMonth ->
val dateSelected = Date(year, monthOfYear, dayOfMonth)
MaterialDatePicker.Builder.datePicker()
.setCalendarConstraints(app.profile.getSchoolYearConstrains())
.build()
.apply {
addOnPositiveButtonClickListener { millis ->
val dateSelected = Date.fromMillisUtc(millis)
generateBlockTimetable(dateSelected.weekStart, dateSelected.weekEnd)
}, date.year, date.month, date.day)
.apply {
accentColor = R.attr.colorPrimary.resolveAttr(this@GenerateBlockTimetableDialog.activity)
show(this@GenerateBlockTimetableDialog.activity.supportFragmentManager, "DatePickerDialog")
}
}
.show(activity.supportFragmentManager, TAG)
}
@Subscribe(threadMode = ThreadMode.MAIN)

View File

@ -308,14 +308,6 @@ class SettingsLicenseActivity : MaterialAboutActivity() {
"https://github.com/underwindfall/PowerPermission"
),
license(
"Material DateTime Picker",
"2015",
"Wouter Dullaert",
OpenSourceLicense.APACHE_2,
"https://github.com/wdullaer/MaterialDateTimePicker"
),
license(
"JsonViewer",
"2017",

View File

@ -16,8 +16,8 @@ import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.google.android.material.datepicker.MaterialDatePicker
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog
import eu.szkolny.font.SzkolnyFont
import kotlinx.coroutines.*
import pl.szczodrzynski.edziennik.App
@ -25,7 +25,7 @@ import pl.szczodrzynski.edziennik.MainActivity
import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.data.db.entity.Metadata
import pl.szczodrzynski.edziennik.databinding.FragmentTimetableV2Binding
import pl.szczodrzynski.edziennik.resolveAttr
import pl.szczodrzynski.edziennik.getSchoolYearConstrains
import pl.szczodrzynski.edziennik.ui.dialogs.event.EventManualDialog
import pl.szczodrzynski.edziennik.ui.dialogs.timetable.GenerateBlockTimetableDialog
import pl.szczodrzynski.edziennik.utils.models.Date
@ -167,19 +167,23 @@ class TimetableFragment : Fragment(), CoroutineScope {
BottomSheetPrimaryItem(true)
.withTitle(R.string.timetable_select_day)
.withIcon(SzkolnyFont.Icon.szf_calendar_today_outline)
.withOnClickListener(View.OnClickListener {
.withOnClickListener { _ ->
activity.bottomSheet.close()
val date = Date.getToday()
DatePickerDialog
.newInstance({ _, year, monthOfYear, dayOfMonth ->
val dateSelected = Date(year, monthOfYear, dayOfMonth)
b.tabLayout.setCurrentItem(items.indexOfFirst { it == dateSelected }, true)
}, date.year, date.month, date.day)
.apply {
accentColor = R.attr.colorPrimary.resolveAttr(this@TimetableFragment.activity)
show(this@TimetableFragment.activity.supportFragmentManager, "DatePickerDialog")
val date = pageSelection ?: Date.getToday()
MaterialDatePicker.Builder.datePicker()
.setSelection(date.inMillisUtc)
.setCalendarConstraints(app.profile.getSchoolYearConstrains())
.build()
.apply {
addOnPositiveButtonClickListener { millis ->
val dateSelected = Date.fromMillisUtc(millis)
val index = items.indexOfFirst { it == dateSelected }
if (index != -1)
b.tabLayout.setCurrentItem(index, true)
}
}),
}
.show(activity.supportFragmentManager, TAG)
},
BottomSheetPrimaryItem(true)
.withTitle(R.string.menu_add_event)
.withDescription(R.string.menu_add_event_desc)

View File

@ -10,14 +10,13 @@ import android.util.AttributeSet
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog
import com.google.android.material.datepicker.MaterialDatePicker
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.data.db.AppDb
import pl.szczodrzynski.edziennik.data.db.full.LessonFull
import pl.szczodrzynski.edziennik.observeOnce
import pl.szczodrzynski.edziennik.resolveAttr
import pl.szczodrzynski.edziennik.utils.TextInputDropDown
import pl.szczodrzynski.edziennik.utils.models.Date
import pl.szczodrzynski.edziennik.utils.models.Week
@ -179,17 +178,17 @@ class DateDropdown : TextInputDropDown {
fun pickerDialog() {
val date = getSelected() as? Date ?: Date.getToday()
DatePickerDialog
.newInstance({ _, year, monthOfYear, dayOfMonth ->
val dateSelected = Date(year, monthOfYear+1, dayOfMonth)
MaterialDatePicker.Builder.datePicker()
.setSelection(date.inMillisUtc)
.build()
.apply {
addOnPositiveButtonClickListener { millis ->
val dateSelected = Date.fromMillisUtc(millis)
selectDate(dateSelected)
onDateSelected?.invoke(dateSelected, null)
}, date.year, date.month-1, date.day)
.apply {
this@DateDropdown.activity ?: return@apply
accentColor = R.attr.colorPrimary.resolveAttr(this@DateDropdown.activity)
show(this@DateDropdown.activity!!.supportFragmentManager, "DatePickerDialog")
}
}
.show(activity!!.supportFragmentManager, "DateDropdown")
}
fun selectDate(date: Date) {

View File

@ -8,7 +8,8 @@ import android.content.Context
import android.content.ContextWrapper
import android.util.AttributeSet
import androidx.appcompat.app.AppCompatActivity
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog
import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.TimeFormat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import pl.szczodrzynski.edziennik.*
@ -177,17 +178,19 @@ class TimeDropdown : TextInputDropDown {
fun pickerDialog() {
val time = (getSelected() as? Pair<*, *>)?.first as? Time ?: Time.getNow()
TimePickerDialog
.newInstance({ _, hourOfDay, minute, second ->
val timeSelected = Time(hourOfDay, minute, second)
MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_24H)
.setHour(time.hour)
.setMinute(time.minute)
.build()
.also { dialog ->
dialog.addOnPositiveButtonClickListener {
val timeSelected = Time(dialog.hour, dialog.minute, 0)
selectTime(timeSelected)
onTimeSelected?.invoke(timeSelected, null, null)
}, time.hour, time.minute, 0, true)
.apply {
this@TimeDropdown.activity ?: return@apply
accentColor = R.attr.colorPrimary.resolveAttr(this@TimeDropdown.activity)
show(this@TimeDropdown.activity!!.supportFragmentManager, "TimePickerDialog")
}
}
.show(activity!!.supportFragmentManager, "TimeDropdown")
}
fun selectTime(time: Time) {

View File

@ -70,6 +70,13 @@ public class Date implements Comparable<Date> {
return new Date(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
}
public static Date fromMillisUtc(long millis) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(millis);
c.setTimeZone(TimeZone.getTimeZone("UTC"));
return new Date(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
}
public static Date fromCalendar(Calendar c) {
return new Date(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
}
@ -94,6 +101,14 @@ public class Date implements Comparable<Date> {
return c.getTimeInMillis();
}
public long getInMillisUtc() {
Calendar c = Calendar.getInstance();
c.set(year, month - 1, day, 0, 0, 0);
c.set(Calendar.MILLISECOND, 0);
c.setTimeZone(TimeZone.getTimeZone("UTC"));
return c.getTimeInMillis();
}
public long getInUnix() {
return getInMillis() / 1000;
}

View File

@ -12,10 +12,6 @@
<color name="dividerColor">#3e7f7f7f</color>
<color name="mdtp_accent_color">#2196f3</color>
<color name="mdtp_accent_color_dark">#64b5f6</color>
<!-- LIGHT THEME -->
<color name="windowBackgroundLight">#ffffffff</color>
<color name="colorSurfaceLight">#ffffffff</color>

View File

@ -96,8 +96,6 @@
<style name="AppTheme" parent="NavView" />
<style name="mdtp_ActionButton.Text" parent="Widget.MaterialComponents.Button.TextButton.Dialog"/>
<!-- Base application theme. -->
<style name="AppTheme.Light" parent="NavView.Light">
<item name="colorPrimary">#2196f3</item>
@ -123,7 +121,6 @@
<item name="halfHourDividerColor">#e0e0e0</item>
<item name="materialAlertDialogTheme">@style/AppTheme.MaterialAlertDialog</item>
<item name="mdtp_theme_dark">false</item>
</style>
<style name="AppTheme.Dark" parent="NavView.Dark">
<item name="colorPrimary">#64b5f6</item>
@ -149,7 +146,6 @@
<item name="halfHourDividerColor">#40ffffff</item>
<item name="materialAlertDialogTheme">@style/AppTheme.MaterialAlertDialog</item>
<item name="mdtp_theme_dark">true</item>
</style>