bauiv1lib.playlist.share
UI functionality for importing shared playlists.
1# Released under the MIT License. See LICENSE for details. 2# 3"""UI functionality for importing shared playlists.""" 4 5from __future__ import annotations 6 7import time 8from typing import TYPE_CHECKING, override 9 10from bauiv1lib.promocode import PromoCodeWindow 11import bauiv1 as bui 12 13if TYPE_CHECKING: 14 from typing import Any, Callable 15 16 17class SharePlaylistImportWindow(PromoCodeWindow): 18 """Window for importing a shared playlist.""" 19 20 def __init__( 21 self, 22 origin_widget: bui.Widget | None = None, 23 on_success_callback: Callable[[], Any] | None = None, 24 ): 25 PromoCodeWindow.__init__(self, modal=True, origin_widget=origin_widget) 26 self._on_success_callback = on_success_callback 27 28 def _on_import_response(self, response: dict[str, Any] | None) -> None: 29 if response is None: 30 bui.screenmessage(bui.Lstr(resource='errorText'), color=(1, 0, 0)) 31 bui.getsound('error').play() 32 return 33 34 if response['playlistType'] == 'Team Tournament': 35 playlist_type_name = bui.Lstr(resource='playModes.teamsText') 36 elif response['playlistType'] == 'Free-for-All': 37 playlist_type_name = bui.Lstr(resource='playModes.freeForAllText') 38 else: 39 playlist_type_name = bui.Lstr(value=response['playlistType']) 40 41 bui.screenmessage( 42 bui.Lstr( 43 resource='importPlaylistSuccessText', 44 subs=[ 45 ('${TYPE}', playlist_type_name), 46 ('${NAME}', response['playlistName']), 47 ], 48 ), 49 color=(0, 1, 0), 50 ) 51 bui.getsound('gunCocking').play() 52 if self._on_success_callback is not None: 53 self._on_success_callback() 54 bui.containerwidget( 55 edit=self._root_widget, transition=self._transition_out 56 ) 57 58 @override 59 def _do_enter(self) -> None: 60 plus = bui.app.plus 61 assert plus is not None 62 63 plus.add_v1_account_transaction( 64 { 65 'type': 'IMPORT_PLAYLIST', 66 'expire_time': time.time() + 5, 67 'code': bui.textwidget(query=self._text_field), 68 }, 69 callback=bui.WeakCall(self._on_import_response), 70 ) 71 plus.run_v1_account_transactions() 72 bui.screenmessage(bui.Lstr(resource='importingText')) 73 74 75class SharePlaylistResultsWindow(bui.Window): 76 """Window for sharing playlists.""" 77 78 def __init__( 79 self, name: str, data: str, origin: tuple[float, float] = (0.0, 0.0) 80 ): 81 del origin # unused arg 82 self._width = 450 83 self._height = 300 84 assert bui.app.classic is not None 85 uiscale = bui.app.ui_v1.uiscale 86 super().__init__( 87 root_widget=bui.containerwidget( 88 size=(self._width, self._height), 89 color=(0.45, 0.63, 0.15), 90 transition='in_scale', 91 scale=( 92 1.8 93 if uiscale is bui.UIScale.SMALL 94 else 1.35 if uiscale is bui.UIScale.MEDIUM else 1.0 95 ), 96 ) 97 ) 98 bui.getsound('cashRegister').play() 99 bui.getsound('swish').play() 100 101 self._cancel_button = bui.buttonwidget( 102 parent=self._root_widget, 103 scale=0.7, 104 position=(40, self._height - 40), 105 size=(50, 50), 106 label='', 107 on_activate_call=self.close, 108 autoselect=True, 109 color=(0.45, 0.63, 0.15), 110 icon=bui.gettexture('crossOut'), 111 iconscale=1.2, 112 ) 113 bui.containerwidget( 114 edit=self._root_widget, cancel_button=self._cancel_button 115 ) 116 117 bui.textwidget( 118 parent=self._root_widget, 119 position=(self._width * 0.5, self._height * 0.745), 120 size=(0, 0), 121 color=bui.app.ui_v1.infotextcolor, 122 scale=1.0, 123 flatness=1.0, 124 h_align='center', 125 v_align='center', 126 text=bui.Lstr( 127 resource='exportSuccessText', subs=[('${NAME}', name)] 128 ), 129 maxwidth=self._width * 0.85, 130 ) 131 132 bui.textwidget( 133 parent=self._root_widget, 134 position=(self._width * 0.5, self._height * 0.645), 135 size=(0, 0), 136 color=bui.app.ui_v1.infotextcolor, 137 scale=0.6, 138 flatness=1.0, 139 h_align='center', 140 v_align='center', 141 text=bui.Lstr(resource='importPlaylistCodeInstructionsText'), 142 maxwidth=self._width * 0.85, 143 ) 144 145 bui.textwidget( 146 parent=self._root_widget, 147 position=(self._width * 0.5, self._height * 0.4), 148 size=(0, 0), 149 color=(1.0, 3.0, 1.0), 150 scale=2.3, 151 h_align='center', 152 v_align='center', 153 text=data, 154 maxwidth=self._width * 0.85, 155 ) 156 157 def close(self) -> None: 158 """Close the window.""" 159 bui.containerwidget(edit=self._root_widget, transition='out_scale')