Added ability to enter date and time manually instead of using pickers

This commit is contained in:
Eugene Zadyra
2018-10-30 12:40:53 +01:00
parent 30d8040bed
commit 1a8533668c
4 changed files with 81 additions and 18 deletions

View File

@@ -38,9 +38,12 @@ import com.lbs.server.conversation.base.Conversation
import com.lbs.server.lang.{Localizable, Localization}
import com.lbs.server.repository.model.Monitoring
import com.lbs.server.service.{ApiService, DataService, MonitoringService}
import com.lbs.server.util.MessageExtractors.CallbackCommand
import com.lbs.server.util.DateTimeUtil._
import com.lbs.server.util.MessageExtractors.{CallbackCommand, TextCommand}
import com.lbs.server.util.ServerModelConverters._
import scala.util.control.NonFatal
class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: DataService, monitoringService: MonitoringService,
val localization: Localization, datePickerFactory: UserIdWithOriginatorTo[DatePicker], timePickerFactory: UserIdWithOriginatorTo[TimePicker],
staticDataFactory: UserIdWithOriginatorTo[StaticData], termsPagerFactory: UserIdWithOriginatorTo[Pager[AvailableVisitsTermPresentation]])(implicit val actorSystem: ActorSystem) extends Conversation[BookingData] with StaticDataForBooking with Localizable {
@@ -90,9 +93,20 @@ class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: Da
datePicker ! DateFromMode
datePicker ! bookingData.dateFrom
} onReply {
case Msg(cmd: Command, _) =>
case Msg(cmd@CallbackCommand(_), _) =>
datePicker ! cmd
stay()
case Msg(TextCommand(dayMonth), bookingData) =>
try {
val date = applyDayMonth(dayMonth, bookingData.dateFrom)
bot.sendMessage(userId.source, lang.dateFromIs(date))
goto(requestDateTo) using bookingData.copy(dateFrom = date)
} catch {
case NonFatal(ex) =>
error("Unable to parse date", ex)
bot.sendMessage(userId.source, "Incorrect date. Please use format dd MM")
goto(requestDateFrom)
}
case Msg(date: ZonedDateTime, bookingData: BookingData) =>
goto(requestDateTo) using bookingData.copy(dateFrom = date)
}
@@ -103,9 +117,20 @@ class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: Da
datePicker ! DateToMode
datePicker ! bookingData.dateFrom.plusDays(1)
} onReply {
case Msg(cmd: Command, _) =>
case Msg(cmd@CallbackCommand(_), _) =>
datePicker ! cmd
stay()
case Msg(TextCommand(dayMonth), bookingData) =>
try {
val date = applyDayMonth(dayMonth, bookingData.dateTo)
bot.sendMessage(userId.source, lang.dateToIs(date))
goto(requestTimeFrom) using bookingData.copy(dateTo = date)
} catch {
case NonFatal(ex) =>
error("Unable to parse date", ex)
bot.sendMessage(userId.source, "Incorrect date. Please use format dd MM")
goto(requestDateTo)
}
case Msg(date: ZonedDateTime, bookingData: BookingData) =>
goto(requestTimeFrom) using bookingData.copy(dateTo = date)
}
@@ -116,9 +141,20 @@ class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: Da
timePicker ! TimeFromMode
timePicker ! bookingData.timeFrom
} onReply {
case Msg(cmd: Command, _) =>
case Msg(cmd@CallbackCommand(_), _) =>
timePicker ! cmd
stay()
case Msg(TextCommand(hourMinute), bookingData) =>
try {
val time = applyHourMinute(hourMinute)
bot.sendMessage(userId.source, lang.timeFromIs(time))
goto(requestTimeTo) using bookingData.copy(timeFrom = time)
} catch {
case NonFatal(ex) =>
error("Unable to parse time", ex)
bot.sendMessage(userId.source, "Incorrect time. Please use format HH mm")
goto(requestTimeFrom)
}
case Msg(time: LocalTime, bookingData: BookingData) =>
goto(requestTimeTo) using bookingData.copy(timeFrom = time)
}
@@ -129,9 +165,20 @@ class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: Da
timePicker ! TimeToMode
timePicker ! bookingData.timeTo
} onReply {
case Msg(cmd: Command, _) =>
case Msg(cmd@CallbackCommand(_), _) =>
timePicker ! cmd
stay()
case Msg(TextCommand(hourMinute), bookingData) =>
try {
val time = applyHourMinute(hourMinute)
bot.sendMessage(userId.source, lang.timeToIs(time))
goto(requestAction) using bookingData.copy(timeTo = time)
} catch {
case NonFatal(ex) =>
error("Unable to parse time", ex)
bot.sendMessage(userId.source, "Incorrect time. Please use format HH mm")
goto(requestTimeTo)
}
case Msg(time: LocalTime, bookingData: BookingData) =>
goto(requestAction) using bookingData.copy(timeTo = time)
}
@@ -147,8 +194,9 @@ class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: Da
} onReply {
case Msg(CallbackCommand(Tags.FindTerms), _) =>
goto(requestTerm)
case Msg(CallbackCommand(Tags.ModifyDate), _) =>
goto(requestDateFrom)
case Msg(CallbackCommand(Tags.ModifyDate), bookingData) =>
goto(requestDateFrom) using bookingData.copy(dateFrom = ZonedDateTime.now(),
dateTo = ZonedDateTime.now().plusDays(1L))
}
private def requestTerm: Step =

View File

@@ -67,9 +67,9 @@ object En extends Lang {
|${capitalizeFirstLetter(clinic)}: ${visit.clinic.name}
|""".stripMargin
override def chooseDateFrom: String = "<b>➡</b> Please choose date from"
override def chooseDateFrom: String = "<b>➡</b> Please choose date from or write it manually using format dd MM, e.g. 24 08"
override def chooseDateTo: String = "<b>➡</b> Please choose date to"
override def chooseDateTo: String = "<b>➡</b> Please choose date to or write it manually using format dd MM, e.g. 24 08"
override def findTerms: String = "🔍 Find terms"
@@ -364,9 +364,9 @@ object En extends Lang {
override def moreParameters: String = "🛠 More parameters"
override def chooseTimeFrom: String = "<b>➡</b> Please choose time from"
override def chooseTimeFrom: String = "<b>➡</b> Please choose time from or write time using format HH mm, e.g. 16 30"
override def chooseTimeTo: String = "<b>➡</b> Please choose time to"
override def chooseTimeTo: String = "<b>➡</b> Please choose time to or write time using format HH mm, e.g. 16 30"
override def timeFromIs(timeFrom: LocalTime): String = s"⏱ Time from is ${formatTime(timeFrom)}"

View File

@@ -67,9 +67,9 @@ object Ua extends Lang {
|${capitalizeFirstLetter(clinic)}: ${visit.clinic.name}
|""".stripMargin
override def chooseDateFrom: String = "<b>➡</b> Будь ласка, виберіть початкову дату"
override def chooseDateFrom: String = "<b>➡</b> Будь ласка, виберіть початкову дату або введіть її, використовуючи формат dd MM, наприклад 24 08"
override def chooseDateTo: String = "<b>➡</b> Будь ласка, виберіть кінцеву дату"
override def chooseDateTo: String = "<b>➡</b> Будь ласка, виберіть кінцеву дату або введіть її, використовуючи формат dd MM, наприклад 24 08"
override def findTerms: String = "🔍 Знайти терміни"
@@ -363,9 +363,9 @@ object Ua extends Lang {
override def moreParameters: String = "🛠 Більше налаштувань"
override def chooseTimeFrom: String = "<b>➡</b> Будь ласка, виберіть початковий час"
override def chooseTimeFrom: String = "<b>➡</b> Будь ласка, виберіть початковий час або введіть час, використовуючи формат HH mm, наприклад 16 30"
override def chooseTimeTo: String = "<b>➡</b> Будь ласка, виберіть кінцевий час"
override def chooseTimeTo: String = "<b>➡</b> Будь ласка, виберіть кінцевий час або введіть час, використовуючи формат HH mm, наприклад 16 30"
override def timeFromIs(timeFrom: LocalTime): String = s"⏱ Початковий час ${formatTime(timeFrom)}"

View File

@@ -23,8 +23,8 @@
*/
package com.lbs.server
import java.time._
import java.time.format.DateTimeFormatter
import java.time.{LocalTime, ZoneId, ZonedDateTime}
import java.util.Locale
import com.lbs.api.json.model._
@@ -128,11 +128,11 @@ package object util {
object MessageExtractors {
object TextCommand {
def unapply(cmd: Command): Option[String] = cmd.message.text
def unapply(cmd: Command): Option[String] = cmd.message.text.filter(_.nonEmpty)
}
object OptionalTextCommand {
def unapply(cmd: Command): Option[Option[String]] = Some(cmd.message.text)
def unapply(cmd: Command): Option[Option[String]] = Some(TextCommand.unapply(cmd))
}
object CallbackCommand {
@@ -144,6 +144,10 @@ package object util {
object DateTimeUtil {
private val DateFormat: Locale => DateTimeFormatter = locale => DateTimeFormatter.ofPattern("dd MMM yyyy", locale)
private val DayMonthFormat = DateTimeFormatter.ofPattern("dd MM")
private val HourMinuteFormat = DateTimeFormatter.ofPattern("HH mm")
private val TimeFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm")
private val DateTimeFormat: Locale => DateTimeFormatter = locale => DateTimeFormatter.ofPattern("EEE',' dd MMM yyyy',' HH:mm", locale)
@@ -159,6 +163,17 @@ package object util {
def epochMinutes(time: ZonedDateTime): Long = time.toInstant.getEpochSecond / 60
def minutesSinceBeginOf2018(time: ZonedDateTime): Long = epochMinutes(time) - EpochMinutesTillBeginOf2018
def applyDayMonth(dayMonthStr: String, date: ZonedDateTime): ZonedDateTime = {
val dayMonth = MonthDay.parse(dayMonthStr, DayMonthFormat)
val newDate = date.withDayOfMonth(dayMonth.getDayOfMonth).withMonth(dayMonth.getMonthValue)
if (newDate.isBefore(date)) newDate.plusYears(1) else newDate
}
def applyHourMinute(hourMinuteStr: String): LocalTime = {
LocalTime.parse(hourMinuteStr, HourMinuteFormat)
}
}
}