|
| 1 | +# coding=utf-8 |
| 2 | +""" |
| 3 | +Share |
| 4 | +----- |
| 5 | +""" |
| 6 | + |
| 7 | +from plyer.facades import Share |
| 8 | +from plyer import storagepath |
| 9 | +from pyobjus import autoclass |
| 10 | +from pyobjus.objc_py_types import NSSize, NSRect, NSPoint |
| 11 | +from typing import Tuple |
| 12 | + |
| 13 | +NSURL = autoclass('NSURL') |
| 14 | +UIApplication = autoclass('UIApplication') |
| 15 | +UIDevice = autoclass('UIDevice') |
| 16 | +currentDevice = UIDevice.currentDevice() |
| 17 | +iPhone = currentDevice.userInterfaceIdiom == 0 |
| 18 | +iPad = currentDevice.userInterfaceIdiom == 1 |
| 19 | +sharedApplication = UIApplication.sharedApplication() |
| 20 | + |
| 21 | +class IosShare(Share): |
| 22 | + |
| 23 | + def _write_data_to_file(self, data, out_file): |
| 24 | + with open(out_file, 'w') as ofile: |
| 25 | + ofile.write(data) |
| 26 | + |
| 27 | + |
| 28 | + def _share_text(self, text: str, title: str, |
| 29 | + size: Tuple[int, int]=(32, 32), |
| 30 | + pos:Tuple[int, int]=(200, 200), |
| 31 | + arrow_direction:int=0): |
| 32 | + self._share_file(text, None, title, |
| 33 | + size=size, pos=pos, arrow_direction=arrow_direction) |
| 34 | + |
| 35 | + def _share_file( |
| 36 | + self, data: str, filename: str, title: str, |
| 37 | + size: Tuple[int, int]=(32, 32), |
| 38 | + pos:Tuple[int, int]=(200, 200), |
| 39 | + arrow_direction:int=0): |
| 40 | + |
| 41 | + if not data: |
| 42 | + return |
| 43 | + |
| 44 | + if filename: |
| 45 | + out_file = storagepath.get_home_dir() + '/Documents/' + filename |
| 46 | + self._write_data_to_file(data, out_file) |
| 47 | + URL = NSURL.fileURLWithPath_(out_file) |
| 48 | + data = URL |
| 49 | + |
| 50 | + import gc |
| 51 | + gc.collect() |
| 52 | + |
| 53 | + UIActivityViewController = autoclass('UIActivityViewController') |
| 54 | + UIActivityViewController_instance = UIActivityViewController.alloc().init() |
| 55 | + activityViewController = UIActivityViewController_instance.initWithActivityItems_applicationActivities_([data], None) |
| 56 | + UIcontroller = sharedApplication.keyWindow.rootViewController() |
| 57 | + |
| 58 | + |
| 59 | + if iPad: |
| 60 | + UIView = UIcontroller.view() |
| 61 | + UIActivityViewController_instance.modalPresentationStyle = 9# 9 is popover |
| 62 | + UIActivityViewController_instance.preferredContentSize = NSSize(0,0) |
| 63 | + pc = UIActivityViewController_instance.popoverPresentationController() |
| 64 | + pc.permittedArrowDirections = arrow_direction # 0 means no direction |
| 65 | + pc.sourceView = UIView |
| 66 | + val = NSRect() |
| 67 | + val.size = NSSize(*size)# Apsect ration? |
| 68 | + val.origin = NSPoint(*pos) |
| 69 | + pc.sourceRect = val |
| 70 | + |
| 71 | + UIcontroller.presentViewController_animated_completion_(activityViewController, True, None) |
| 72 | + gc.collect() |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +def instance(): |
| 77 | + return IosShare() |
0 commit comments