Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Sharesheet #690

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion plyer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'brightness', 'call', 'camera', 'compass', 'cpu', 'email', 'filechooser',
'flash', 'gps', 'gravity', 'gyroscope', 'humidity', 'irblaster',
'keystore', 'light', 'notification', 'orientation', 'processors',
'proximity', 'screenshot', 'sms', 'spatialorientation', 'storagepath',
'proximity', 'screenshot', 'sms', 'share', 'spatialorientation', 'storagepath',
'stt', 'temperature', 'tts', 'uniqueid', 'vibrator', 'wifi', 'devicename'
)

Expand Down Expand Up @@ -76,6 +76,9 @@
#: Sms proxy to :class:`plyer.facades.Sms`
sms = Proxy('sms', facades.Sms)

#: Share proxy to :class:`plyer.facades.Share`
share = Proxy('share', facades.Share)

#: Speech proxy to :class:`plyer.facades.STT`
stt = Proxy('stt', facades.STT)

Expand Down
3 changes: 2 additions & 1 deletion plyer/facades/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__all__ = ('Accelerometer', 'Audio', 'Barometer', 'Battery', 'Call', 'Camera',
'Compass', 'Email', 'FileChooser', 'GPS', 'Gravity', 'Gyroscope',
'IrBlaster', 'Light', 'Orientation', 'Notification', 'Proximity',
'Sms', 'TTS', 'UniqueID', 'Vibrator', 'Wifi', 'Flash', 'CPU',
'Sms', 'Share', 'TTS', 'UniqueID', 'Vibrator', 'Wifi', 'Flash', 'CPU',
'Temperature', 'Humidity', 'SpatialOrientation', 'Brightness',
'Processors', 'StoragePath', 'Keystore', 'Bluetooth', 'Screenshot',
'STT', 'DeviceName')
Expand All @@ -33,6 +33,7 @@
from plyer.facades.orientation import Orientation
from plyer.facades.notification import Notification
from plyer.facades.sms import Sms
from plyer.facades.share import Share
from plyer.facades.stt import STT
from plyer.facades.tts import TTS
from plyer.facades.uniqueid import UniqueID
Expand Down
71 changes: 71 additions & 0 deletions plyer/facades/share.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# coding=utf-8
'''
Share
=====

The :class:`Share` provides access to public methods to share a file or text or ...

.. note::
In android you need external storage permissions.

.. versionadded:: 2.1.0

This can be used to activate the sharesheet on supported OS.

Simple Examples
---------------

Share text::

>>> from plyer import share
>>> share.share_text(text, title)

Share file::

>>> share.share_file(data, filename, title)

Supported Platforms
-------------------
Android, iOS

'''
from typing import Tuple


class Share:
"""
Share facade.
"""

def share_text(self, text: str, title: str,
size: Tuple[int, int]=(32, 32),
pos:Tuple[int, int]=(200, 200),
arrow_direction:int=0):
"""
Share Sheet for text
"""
self._share_text(text, title)

def share_file(
self, data: str, filename: str, title: str,
size: Tuple[int, int]=(1, 1),
pos:Tuple[int, int]=(0, 0),
arrow_direction:int=0):
"""
Share a file
"""
self._share_file(data, filename, title, size, pos, arrow_direction)

# private

def _share_text(self, text:str,
size: Tuple[int, int]=(32, 32),
pos:Tuple[int, int]=(200, 200),
arrow_direction:int=0):
raise NotImplementedError()

def _share_file(self, data: str, filename: str, titile: str,
size: Tuple[int, int]=(32, 32),
pos:Tuple[int, int]=(200, 200),
arrow_direction:int=0):
raise NotImplementedError()
77 changes: 77 additions & 0 deletions plyer/platforms/ios/share.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# coding=utf-8
"""
Share
-----
"""

from plyer.facades import Share
from plyer import storagepath
from pyobjus import autoclass
from pyobjus.objc_py_types import NSSize, NSRect, NSPoint
from typing import Tuple

NSURL = autoclass('NSURL')
UIApplication = autoclass('UIApplication')
UIDevice = autoclass('UIDevice')
currentDevice = UIDevice.currentDevice()
iPhone = currentDevice.userInterfaceIdiom == 0
iPad = currentDevice.userInterfaceIdiom == 1
sharedApplication = UIApplication.sharedApplication()

class IosShare(Share):

def _write_data_to_file(self, data, out_file):
with open(out_file, 'w') as ofile:
ofile.write(data)


def _share_text(self, text: str, title: str,
size: Tuple[int, int]=(32, 32),
pos:Tuple[int, int]=(200, 200),
arrow_direction:int=0):
self._share_file(text, None, title,
size=size, pos=pos, arrow_direction=arrow_direction)

def _share_file(
self, data: str, filename: str, title: str,
size: Tuple[int, int]=(32, 32),
pos:Tuple[int, int]=(200, 200),
arrow_direction:int=0):

if not data:
return

if filename:
out_file = storagepath.get_home_dir() + '/Documents/' + filename
self._write_data_to_file(data, out_file)
URL = NSURL.fileURLWithPath_(out_file)
data = URL

import gc
gc.collect()

UIActivityViewController = autoclass('UIActivityViewController')
UIActivityViewController_instance = UIActivityViewController.alloc().init()
activityViewController = UIActivityViewController_instance.initWithActivityItems_applicationActivities_([data], None)
UIcontroller = sharedApplication.keyWindow.rootViewController()


if iPad:
UIView = UIcontroller.view()
UIActivityViewController_instance.modalPresentationStyle = 9# 9 is popover
UIActivityViewController_instance.preferredContentSize = NSSize(0,0)
pc = UIActivityViewController_instance.popoverPresentationController()
pc.permittedArrowDirections = arrow_direction # 0 means no direction
pc.sourceView = UIView
val = NSRect()
val.size = NSSize(*size)# Apsect ration?
val.origin = NSPoint(*pos)
pc.sourceRect = val

UIcontroller.presentViewController_animated_completion_(activityViewController, True, None)
gc.collect()



def instance():
return IosShare()