Skip to content

Commit

Permalink
First pass for iOS file save
Browse files Browse the repository at this point in the history
Working for 'On this Phone' file locations.
Seems to have an issue with DropBox export.
Fixes #2076
  • Loading branch information
andydotxyz committed Mar 18, 2021
1 parent 27f482f commit 730d408
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -7,7 +7,7 @@ require (
github.com/akavel/rsrc v0.8.0 // indirect
github.com/fredbi/uri v0.0.0-20181227131451-3dcfdacbaaf3
github.com/fsnotify/fsnotify v1.4.9
github.com/fyne-io/mobile v0.1.3-0.20210312180903-f9a21000f5dc
github.com/fyne-io/mobile v0.1.3-0.20210318200029-09e9c4e13a8f
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200625191551-73d3c3675aa3
github.com/godbus/dbus/v5 v5.0.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -14,8 +14,8 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWo
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fyne-io/glfw/v3.3/glfw v0.0.0-20201123143003-f2279069162d h1:WfVxpuVm+5Gr3ipAoWrxV8lJFYkaBWoEwFRrWThWRSU=
github.com/fyne-io/glfw/v3.3/glfw v0.0.0-20201123143003-f2279069162d/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/fyne-io/mobile v0.1.3-0.20210312180903-f9a21000f5dc h1:A5hFL3tVUfFHhVpjmLGPs8SdVIcSkTRpFMm1Vsio5+k=
github.com/fyne-io/mobile v0.1.3-0.20210312180903-f9a21000f5dc/go.mod h1:/kOrWrZB6sasLbEy2JIvr4arEzQTXBTZGb3Y96yWbHY=
github.com/fyne-io/mobile v0.1.3-0.20210318200029-09e9c4e13a8f h1:rguJ/t99j/6zRSFzsBKlsmmyl+vOvCeTJ+2uTBvuXFI=
github.com/fyne-io/mobile v0.1.3-0.20210318200029-09e9c4e13a8f/go.mod h1:/kOrWrZB6sasLbEy2JIvr4arEzQTXBTZGb3Y96yWbHY=
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw=
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME=
Expand Down
39 changes: 37 additions & 2 deletions internal/driver/gomobile/file_ios.go
Expand Up @@ -10,10 +10,10 @@ package gomobile
void* iosParseUrl(const char* url);
const void* iosReadFromURL(void* url, int* len);
const int iosWriteToURL(void* url, const void* bytes, int len);
*/
import "C"
import (
"errors"
"io"
"unsafe"

Expand Down Expand Up @@ -64,6 +64,31 @@ func (s *secureReadCloser) Close() error {
return nil
}

type secureWriteCloser struct {
url unsafe.Pointer
closer func()

offset int
}

// Declare conformity to WriteCloser interface
var _ io.WriteCloser = (*secureWriteCloser)(nil)

func (s *secureWriteCloser) Write(p []byte) (int, error) {
count := int(C.iosWriteToURL(s.url, C.CBytes(p), C.int(len(p))))
s.offset += count

return count, nil
}

func (s *secureWriteCloser) Close() error {
if s.closer != nil {
s.closer()
}
s.url = nil
return nil
}

func nativeFileOpen(f *fileOpen) (io.ReadCloser, error) {
if f.uri == nil || f.uri.String() == "" {
return nil, nil
Expand All @@ -79,7 +104,17 @@ func nativeFileOpen(f *fileOpen) (io.ReadCloser, error) {
}

func nativeFileSave(f *fileSave) (io.WriteCloser, error) {
return nil, errors.New("Currently unsupported.")
if f.uri == nil || f.uri.String() == "" {
return nil, nil
}

cStr := C.CString(f.uri.String())
defer C.free(unsafe.Pointer(cStr))

url := C.iosParseUrl(cStr)

fileStruct := &secureWriteCloser{url: url, closer: f.done}
return fileStruct, nil
}

func registerRepository(d *mobileDriver) {
Expand Down
11 changes: 11 additions & 0 deletions internal/driver/gomobile/file_ios.m
Expand Up @@ -16,3 +16,14 @@
*len = data.length;
return data.bytes;
}

const int iosWriteToURL(void* urlPtr, const void* bytes, int len) {
NSURL* url = (NSURL*)urlPtr;
NSData *data = [NSData dataWithBytes:bytes length:len];
BOOL ok = [data writeToURL:url atomically:YES];

if (!ok) {
return 0;
}
return data.length;
}
33 changes: 22 additions & 11 deletions vendor/github.com/fyne-io/mobile/app/darwin_ios.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 30 additions & 3 deletions vendor/github.com/fyne-io/mobile/app/darwin_ios.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Expand Up @@ -12,7 +12,7 @@ github.com/davecgh/go-spew/spew
github.com/fredbi/uri
# github.com/fsnotify/fsnotify v1.4.9
github.com/fsnotify/fsnotify
# github.com/fyne-io/mobile v0.1.3-0.20210312180903-f9a21000f5dc
# github.com/fyne-io/mobile v0.1.3-0.20210318200029-09e9c4e13a8f
github.com/fyne-io/mobile/app
github.com/fyne-io/mobile/app/internal/callfn
github.com/fyne-io/mobile/event/key
Expand Down

0 comments on commit 730d408

Please sign in to comment.