FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/src/pythonnative/native_modules/share.py at main · andevsr/pythonnative · GitHub
andevsr
pythonnative
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
share.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
156 lines (124 loc) · 4.69 KB
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
share.py
Copy path
File metadata and controls
156 lines (124 loc) · 4.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Present the system share sheet.
[`Share.share`][pythonnative.Share] is a coroutine that opens
``UIActivityViewController`` (iOS) or an ``ACTION_SEND`` chooser
(Android) and resolves to ``True`` once the user completes a share or
``False`` if they dismiss it.
Example:
```python
import pythonnative as pn
async def share_link():
await pn.Share.share(
message="Check out PythonNative!",
url="https://example.com",
)
```
"""
from
__future__
import
annotations
import
asyncio
from
typing
import
Any
,
Callable
,
Dict
,
Optional
from
..
import
diagnostics
from
..
runtime
import
resolve_future
from
..
utils
import
IS_ANDROID
,
IS_IOS
# Retain pool so iOS completion handlers aren't collected early.
_pending
:
Dict
[
int
,
Any
]
=
{}
class
Share
:
"""System share-sheet interface."""
@
staticmethod
async
def
share
(
*
,
message
:
Optional
[
str
]
=
None
,
url
:
Optional
[
str
]
=
None
,
title
:
Optional
[
str
]
=
None
,
)
->
bool
:
"""Open the share sheet with ``message`` / ``url``.
Args:
message: Text body to share.
url: A URL to share (combined with ``message`` on Android).
title: Chooser title (Android) / subject (iOS mail).
Returns:
``True`` if the user completed a share, ``False`` if they
cancelled or no share UI is available.
"""
loop
=
asyncio
.
get_running_loop
()
future
:
asyncio
.
Future
[
bool
]
=
loop
.
create_future
()
def
_done
(
ok
:
bool
)
->
None
:
resolve_future
(
future
,
ok
)
if
IS_IOS
:
_ios_share
(
_done
,
message
=
message
,
url
=
url
,
title
=
title
)
elif
IS_ANDROID
:
_android_share
(
_done
,
message
=
message
,
url
=
url
,
title
=
title
)
else
:
_done
(
False
)
return
await
future
# ======================================================================
# iOS: UIActivityViewController
# ======================================================================
def
_ios_share
(
on_done
:
Callable
[[
bool
],
None
],
message
:
Optional
[
str
],
url
:
Optional
[
str
],
title
:
Optional
[
str
],
)
->
None
:
del
title
try
:
from
rubicon
.
objc
import
Block
,
ObjCClass
items
=
ObjCClass
(
"NSMutableArray"
).
alloc
().
init
()
if
message
:
items
.
addObject_
(
message
)
if
url
:
nsurl
=
ObjCClass
(
"NSURL"
).
URLWithString_
(
url
)
if
nsurl
is
not
None
:
items
.
addObject_
(
nsurl
)
controller
=
(
ObjCClass
(
"UIActivityViewController"
).
alloc
().
initWithActivityItems_applicationActivities_
(
items
,
None
)
)
token
=
id
(
controller
)
_pending
[
token
]
=
controller
def
_completion
(
activity
:
Any
,
completed
:
bool
,
items_
:
Any
,
error
:
Any
)
->
None
:
del
activity
,
items_
,
error
_pending
.
pop
(
token
,
None
)
try
:
on_done
(
bool
(
completed
))
except
Exception
:
diagnostics
.
swallowed
(
"share._ios_share._completion"
)
controller
.
setCompletionWithItemsHandler_
(
Block
(
_completion
,
None
,
ObjCClass
(
"NSString"
),
Block
,
ObjCClass
(
"NSArray"
),
ObjCClass
(
"NSError"
))
)
app
=
ObjCClass
(
"UIApplication"
).
sharedApplication
top
=
app
.
keyWindow
.
rootViewController
while
top
is
not
None
and
top
.
presentedViewController
is
not
None
:
top
=
top
.
presentedViewController
if
top
is
not
None
:
top
.
presentViewController_animated_completion_
(
controller
,
True
,
None
)
else
:
_pending
.
pop
(
token
,
None
)
on_done
(
False
)
except
Exception
:
on_done
(
False
)
# ======================================================================
# Android: ACTION_SEND chooser
# ======================================================================
def
_android_share
(
on_done
:
Callable
[[
bool
],
None
],
message
:
Optional
[
str
],
url
:
Optional
[
str
],
title
:
Optional
[
str
],
)
->
None
:
try
:
from
java
import
jclass
from
..
utils
import
get_android_context
Intent
=
jclass
(
"android.content.Intent"
)
intent
=
Intent
(
Intent
.
ACTION_SEND
)
intent
.
setType
(
"text/plain"
)
body
=
"
\n
"
.
join
(
part
for
part
in
(
message
,
url
)
if
part
)
intent
.
putExtra
(
Intent
.
EXTRA_TEXT
,
body
)
if
title
:
intent
.
putExtra
(
Intent
.
EXTRA_SUBJECT
,
title
)
chooser
=
Intent
.
createChooser
(
intent
,
title
or
"Share"
)
chooser
.
addFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
)
get_android_context
().
startActivity
(
chooser
)
# ACTION_SEND chooser gives no completion callback; report
# success once the chooser has been launched.
on_done
(
True
)
except
Exception
:
on_done
(
False
)
Back
|
FazBrowse Home
|
New Git URL