Last active
June 6, 2018 12:07
-
-
Save dmz9/f1c1679fc83229ad13c6cd3bf3b81ed3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AppActivity { | |
.... | |
override fun exec(command: UiCommand) { | |
when (command) { | |
is UiCommand.CloseLeftMenu -> closeLeftMenu() | |
is UiCommand.DisplayShopData -> displayShopData(command.shop) | |
is UiCommand.DisplayUserData -> setUser(command.user) | |
is UiCommand.ResetView -> resetView() | |
is UiCommand.ShowSplashScreen -> showSplashScreen(command.text) | |
is UiCommand.HideSplashScreen -> hideSplashScreen() | |
is UiCommand.LaunchAuthorization -> launchAuthorization(command.url) | |
is UiCommand.ShowFab -> showFab() | |
is UiCommand.HideFab -> hideFab() | |
is UiCommand.EnableFab -> activateFab() | |
is UiCommand.DisableFab -> deactivateFab() | |
is UiCommand.AskRelogin -> askRelogin() | |
is UiCommand.SelectMenuItem -> { | |
selectLeftMenuItem(command.route) | |
selectBottomMenuItem(command.route) | |
} | |
is UiCommand.ShowProgress -> showProgress(command.progressItem) | |
is UiCommand.HideProgress -> hideProgress() | |
} | |
} | |
.... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class BaseFragment : Fragment() { | |
... | |
/** | |
* необработаные события обрабатывает хост activity | |
*/ | |
fun exec(command: UiCommand) { | |
(activity as AppActivity).exec(command) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface BasePresenterInterface<V : BaseViewInterface> { | |
var view: V? | |
var disposable: CompositeDisposable | |
fun onViewAttached(iView: V) { | |
TODO("not implemented view attached!") | |
} | |
/** | |
* вызывается в момент когда вьюшка отключается | |
*/ | |
fun onViewDetached() { | |
disposable.clear() | |
exec(UiCommand.HideProgress()) | |
this.view = null | |
} | |
fun exec(command: UiCommand) { | |
view?.exec(command) | |
} | |
fun exec(vararg commands: UiCommand) = commands.forEach { exec(it) } | |
/** | |
* взаимодействия юзера с юи передаются в презентер как события | |
*/ | |
fun on(event: UiEvent) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CartFragment : | |
BaseFragment() { | |
override fun exec(command: UiCommand) { | |
when (command) { | |
is UiCommand.ResetList -> adapter.clean() | |
is UiCommand.DisplayCatalogItemCollection -> { | |
if(command.collection.isEmpty()){ | |
emptyCartWarning?.text="Нет товаров" | |
}else{ | |
emptyCartWarning?.visibility = View.GONE | |
adapter.addCollection(command.collection) | |
} | |
} | |
is UiCommand.UpdateItem -> adapter.updateItem(command.item) | |
is UiCommand.HideSubmit -> hideSubmit() | |
is UiCommand.ShowSubmit -> showSubmit() | |
is UiCommand.DisplayCartTotals -> { | |
setTotalText(command.totalText) | |
setTotalPrice(command.totalPrice) | |
} | |
else -> super.exec(command) | |
} | |
} | |
.... | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CartPresenter @Inject constructor( | |
) : BasePresenterInterface<CartViewInterface> { | |
/** | |
* перехват события ко вьюшке, чтобы выполнить логику перед тем как обновить вью | |
*/ | |
override fun exec(command: UiCommand) { | |
if (command is UiCommand.UpdateItem) { | |
totals[command.item.productID] = command.item | |
recalculate() | |
} | |
super.exec(command) | |
} | |
override fun on(event: UiEvent) { | |
if (cartFlow.consume(event)) { | |
return | |
} | |
when (event) { | |
is UiEvent.OrderSubmit -> onSubmit() | |
} | |
} | |
.... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class UiCommand { | |
data class DisplayCityData(val city: City?) : UiCommand() | |
data class DisplayShopData(val shop: ShopPresentationDTO?) : UiCommand() | |
data class DisplayUserData(val user: UserPresentationDTO?) : UiCommand() | |
data class DisplayFavorite(val isFavorite: Boolean) : UiCommand() | |
class ResetView : UiCommand() | |
class ResetList : UiCommand() | |
data class DisplayShopCollection(val collection: List<Shop>) : UiCommand() | |
data class DisplayCityCollection(val collection: List<City>) : UiCommand() | |
data class DisplayCategoriesCollection(val collection: List<Category>) : UiCommand() | |
data class DisplayCatalogItemCollection(val collection: List<CatalogItem>) : UiCommand() | |
data class DisplayItem(val item: CatalogItem) : UiCommand() | |
data class UpdateItem(val item: CatalogItem) : UiCommand() | |
class CloseLeftMenu : UiCommand() | |
class SelectMenuItem(val route: Route) : UiCommand() | |
class ShowFab : UiCommand() | |
class HideFab : UiCommand() | |
class DisableFab : UiCommand() | |
class EnableFab : UiCommand() | |
class AskRelogin : UiCommand() | |
class LaunchAuthorization(val url: String) : UiCommand() | |
class ShowSplashScreen(val text: String) : UiCommand() | |
class HideSplashScreen : UiCommand() | |
data class ShowProgress(val progressItem: ProgressItem = ProgressItem()) : UiCommand() | |
class HideProgress : UiCommand() | |
class ShowSubmit : UiCommand() | |
class HideSubmit : UiCommand() | |
data class DisplayCartTotals( | |
val totalPrice: Int, | |
val totalText: String | |
) : UiCommand() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class UiEvent { | |
class ChangeShop : UiEvent() | |
class ChangeCity : UiEvent() | |
data class ShopChanged(val shop: Shop) : UiEvent() | |
data class CityChanged(val city: City) : UiEvent() | |
data class NavigateTo(val route: Route) : UiEvent() | |
class NextPageRequest : UiEvent() | |
abstract class CatalogItemEvent(val item: CatalogItem) : UiEvent() | |
class QuantityUp(item: CatalogItem) : CatalogItemEvent(item) | |
class QuantityDown(item: CatalogItem) : CatalogItemEvent(item) | |
class CatalogItemView(item: CatalogItem) : CatalogItemEvent(item) | |
class WhereToBuy(item: CatalogItem) : CatalogItemEvent(item) | |
class WatchGallery(item: CatalogItem) : CatalogItemEvent(item) | |
class ToggleFavorite(item: CatalogItem) : CatalogItemEvent(item) | |
data class CategoryView(val category: Category) : UiEvent() | |
class OrderSubmit : UiEvent() | |
class FabClick : UiEvent() | |
class RouteCalculateRequest(val shop: Shop) : UiEvent() | |
class MapShown : UiEvent() | |
class MapHidden : UiEvent() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment