Skip to content

Commit

Permalink
Support any number of my offers in tg bot (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmvpm committed May 10, 2024
1 parent 14f33b9 commit 94a8f07
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RendererImpl extends Renderer {
photoIds: Option[Seq[MessageID]]
)(implicit message: Message): Seq[Request[?]] = {
val buttons = state.next.map(_.map { tag =>
InlineKeyboardButton.callbackData(buttonBy(tag), tag)
InlineKeyboardButton.callbackData(buttonBy(tag, state), tag)
})
val markup = Some(InlineKeyboardMarkup(buttons))

Expand Down
47 changes: 30 additions & 17 deletions bot/src/main/scala/com/github/mmvpm/bot/state/State.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ object State {
val ErrorTag: Tag = "error"
val UnknownTag: Tag = "unknown"

def buttonBy(tag: Tag): Button =
def buttonBy(tag: Tag, current: State): Button =
tag match {
case SearchTag => "Найти товар"
case ListingTag => "На следующую страницу"
case Listing.chooseOne(idx) => s"${idx.toInt + 1}" // 1-based for user
case CreateOfferNameTag => "Разместить объявление"
case CreatedOfferTag => "Опубликовать объявление"
case MyOffersTag => "Посмотреть мои объявления"
case SearchTag => "Найти товар"
case ListingTag => "На следующую страницу"
case Listing.chooseOne(idx) => s"${idx.toInt + 1}" // 1-based for user
case CreateOfferNameTag => "Разместить объявление"
case CreatedOfferTag => "Опубликовать объявление"
case MyOffersTag =>
current match {
case MyOffers(_, _, _) => "На следующую страницу"
case _ => "Посмотреть мои объявления"
}
case MyOffers.chooseOne(idx) => s"${idx.toInt + 1}" // 1-based for user
case EditOfferTag => "Изменить это объявление"
case EditOfferNameTag => "Название"
Expand All @@ -68,7 +72,7 @@ object State {
current match {
case Search(_) => ListingTag
case Listing(_, _, _) => OneOfferTag
case MyOffers(_, _) => MyOfferTag
case MyOffers(_, _, _) => MyOfferTag
case CreateOfferName(_) => CreateOfferPriceTag
case CreateOfferPrice(_, _) => CreateOfferDescriptionTag
case CreateOfferDescription(_, _) => CreateOfferPhotoTag
Expand Down Expand Up @@ -266,18 +270,18 @@ object State {

// my offers

case class MyOffers(previous: State, offers: Seq[Offer]) extends State with WithPrevious with WithPhotos {
case class MyOffers(previous: State, offers: Seq[Offer], from: Int) extends State with WithPrevious with WithPhotos {

import MyOffers._

private val StepSizeCropped = scala.math.min(StepSize, offers.size)
private val StepSizeCropped = scala.math.min(StepSize, offers.size - from)

val tag: Tag = MyOffersTag

val next: Seq[Seq[Tag]] = Seq(offersTags, Seq(BackTag))
val next: Seq[Seq[Tag]] = Seq(offersTags, Seq(BackTag) ++ nextPageTag, Seq(StartedTag))

val text: String =
if (summary.nonEmpty)
if (offers.nonEmpty)
s"""
|Все ваши объявления:
|
Expand All @@ -293,18 +297,24 @@ object State {

val photos: Seq[TgPhoto] =
offers
.take(StepSizeCropped)
.slice(from, from + StepSizeCropped)
.map(offer => TgPhoto.first(offer.photos))

def get(idx: Int): Offer =
offers.drop(idx).head
offers.drop(from + idx).head

private lazy val nextPageTag =
if (from + StepSize < offers.length)
Seq(MyOffersTag)
else
Seq()

private lazy val offersTags: Seq[Button] =
offers.indices.map(idx => s"$MyOffersTag-$idx")
private lazy val offersTags: Seq[Tag] =
(0 until StepSizeCropped).map(idx => s"$MyOffersTag-$idx")

private lazy val summary: String =
offers
.take(StepSizeCropped)
.slice(from, from + StepSizeCropped)
.zipWithIndex
.map { case (offer, idx) =>
val statusText = offer.status match {
Expand All @@ -321,6 +331,9 @@ object State {
val StepSize = 5

val chooseOne: Regex = s"$MyOffersTag-(\\d*)".r

def start(previous: State, offers: Seq[Offer]): MyOffers =
MyOffers(previous, offers, from = 0)
}

case class MyOffer(previous: State, offer: Offer) extends State with WithPrevious with WithOfferID with WithPhotos {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,21 @@ class StateManagerImpl[F[_]: MonadCancelThrow](ofsManager: OfsManager[F]) extend
}

private def toMyOffers(current: State)(implicit message: Message): F[State] =
ofsManager.getMyOffers
.handleDefaultErrors(current, ifSuccess = MyOffers(current, _).pure)
current match {
case MyOffers(_, offers, from) =>
MyOffers(current, offers, from + MyOffers.StepSize).pure
case _ =>
ofsManager.getMyOffers
.handleDefaultErrors(current, ifSuccess = MyOffers.start(current, _).pure)
}

private def toMyOffer(current: State)(implicit message: Message): F[State] = {
val optOffer = for {
offerId <- message.text
if offerId.isUUID
offers <- current match {
case MyOffers(_, offers) => Some(offers)
case _ => None
case MyOffers(_, offers, _) => Some(offers)
case _ => None
}
offer <- offers.find(_.id == offerId.toUUID)
} yield offer
Expand Down Expand Up @@ -361,15 +366,15 @@ class StateManagerImpl[F[_]: MonadCancelThrow](ofsManager: OfsManager[F]) extend
freshPrevious <- refreshOffers(previous)
} yield MyOffer(freshPrevious, freshOffer)

case MyOffers(previous, previousOffers) =>
case MyOffers(previous, previousOffers, from) =>
for {
result <- ofsManager.getOffers(previousOffers.map(_.id)).value
freshOffers = result match {
case Right(offers) => offers
case _ => previousOffers
}
freshPrevious <- refreshOffers(previous)
} yield MyOffers(freshPrevious, freshOffers)
} yield MyOffers(freshPrevious, freshOffers, from)

case current if current.optPrevious.nonEmpty =>
refreshOffers(current.optPrevious.get)
Expand Down

0 comments on commit 94a8f07

Please sign in to comment.