Skip to content

Commit

Permalink
Also add file deletion for iOS apps :)
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Feb 15, 2024
1 parent 56f29d2 commit e696513
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions internal/driver/mobile/file_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ func (s *javaStream) Write(p []byte) (int, error) {
return len(p), err
}

func deleteURI(u fyne.URI) error {
// TODO implement this for Android
return repository.ErrOperationNotSupported
}

func existsURI(uri fyne.URI) (bool, error) {
uriStr := C.CString(uri.String())
defer C.free(unsafe.Pointer(uriStr))
Expand Down
5 changes: 5 additions & 0 deletions internal/driver/mobile/file_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"fyne.io/fyne/v2/storage/repository"
)

func deleteURI(u fyne.URI) error {
// no-op as we use the internal FileRepository
return nil
}

func existsURI(fyne.URI) (bool, error) {
// no-op as we use the internal FileRepository
return false, nil
Expand Down
15 changes: 14 additions & 1 deletion internal/driver/mobile/file_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package mobile
#import <stdlib.h>
#import <stdbool.h>
void iosDeletePath(const char* path);
bool iosExistsPath(const char* path);
void* iosParseUrl(const char* url);
const void* iosReadFromURL(void* url, int* len);
Expand Down Expand Up @@ -97,9 +98,21 @@ func (s *secureWriteCloser) Close() error {
return nil
}

func deleteURI(u fyne.URI) error {
if u.Scheme() != "file" {
return errors.New("cannot delete from " + u.Scheme() + " scheme on iOS")
}

cStr := C.CString(u.Path())
defer C.free(unsafe.Pointer(cStr))

C.iosDeletePath(cStr)
return nil
}

func existsURI(u fyne.URI) (bool, error) {
if u.Scheme() != "file" {
return true, errors.New("cannot check existence of " + u.Scheme() + " on iOS")
return true, errors.New("cannot check existence of " + u.Scheme() + " scheme on iOS")
}

cStr := C.CString(u.Path())
Expand Down
5 changes: 5 additions & 0 deletions internal/driver/mobile/file_ios.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

#import <stdbool.h>

void iosDeletePath(const char* path) {
NSString *pathStr = [NSString stringWithUTF8String:path];
[[NSFileManager defaultManager] removeItemAtPath:pathStr error:nil];
}

bool iosExistsPath(const char* path) {
NSString *pathStr = [NSString stringWithUTF8String:path];
return [[NSFileManager defaultManager] fileExistsAtPath:pathStr];
Expand Down
3 changes: 1 addition & 2 deletions internal/driver/mobile/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func (m *mobileFileRepo) CreateListable(u fyne.URI) error {
}

func (m *mobileFileRepo) Delete(u fyne.URI) error {
// TODO: implement this
return repository.ErrOperationNotSupported
return deleteURI(u)
}

func (m *mobileFileRepo) Destroy(string) {
Expand Down

0 comments on commit e696513

Please sign in to comment.