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