Skip to content

Commit

Permalink
Merge pull request #12 from estivensh4/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
estivensh authored Nov 16, 2023
2 parents 4a302eb + 4126aba commit 074f86b
Show file tree
Hide file tree
Showing 19 changed files with 235 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
package com.estivensh4.aws_kmp

class AwsException : Exception {
@Throws(Exception::class)
constructor(message: String?) : super(message)

@Throws(Exception::class)
constructor(cause: Throwable?) : super(cause)

@Throws(Exception::class)
constructor(message: String?, cause: Throwable?) : super(message, cause)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

package com.estivensh4.aws_s3

expect class ImageFile
expect open class ImageFile
expect fun ImageFile.toByteArray(): ByteArray
15 changes: 9 additions & 6 deletions aws-s3/src/iosMain/kotlin/com/estivensh4/aws_s3/AWSS3.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import cocoapods.AWSS3.AWSS3CreateBucketRequest
import cocoapods.AWSS3.AWSS3DeleteBucketRequest
import cocoapods.AWSS3.AWSS3DeleteObjectsRequest
import cocoapods.AWSS3.AWSS3DeletedObject
import cocoapods.AWSS3.AWSS3GetObjectRequest
import cocoapods.AWSS3.AWSS3GetPreSignedURLRequest
import cocoapods.AWSS3.AWSS3ListObjectsV2Output
import cocoapods.AWSS3.AWSS3ListObjectsV2Request
Expand Down Expand Up @@ -97,17 +98,17 @@ actual class AWSS3 actual constructor(
return try {
val preSignedURLRequest = AWSS3GetPreSignedURLRequest()
preSignedURLRequest.apply {
bucket = bucketName
this.bucket = bucketName
this.key = key
expires = NSDate().dateByAddingTimeInterval(expirationInSeconds.toDouble())
this.expires = NSDate().dateByAddingTimeInterval(expirationInSeconds.toDouble())
}

val request = AWSS3PreSignedURLBuilder.defaultS3PreSignedURLBuilder()
.getPreSignedURL(preSignedURLRequest)
val url = request.result() as NSURL
url.absoluteString
} catch (exception: Exception) {
throw AwsException("Exception is ${exception.message}", exception)
throw Exception("Exception is ${exception.message}", exception)
}
}

Expand Down Expand Up @@ -323,14 +324,16 @@ actual class AWSS3 actual constructor(
* @throws AwsException If any errors are encountered in the client
* while making the request or handling the response.
*/
@Suppress("CAST_NEVER_SUCCEEDS")
actual suspend fun createBucket(bucketName: String): Bucket {
val request = AWSS3CreateBucketRequest()
request.bucket = bucketName

val result = awaitResult { client.createBucket(request, it) } as AWSS3Bucket
val result = awaitResult { client.createBucket(request, it) }

return result.toBucket()
return Bucket(
name = result.location,
creationDate = null
)
}

/**
Expand Down
6 changes: 3 additions & 3 deletions example/androidapp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ android {
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = "11"
}
buildFeatures {
compose = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ fun Greeting() {

Column {
CButton(text = "GeneratePresignedURL") {
text = sampleViewModel.generatePresignedUrl(
sampleViewModel.generatePresignedUrl(
bucketName = bucketName,
key = key
) ?: ""
)
Log.d("ResultGeneratePresignedUrl", text)
}
CButton(text = "Create bucket") {
Expand Down
24 changes: 21 additions & 3 deletions example/desktopApp/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Button
import androidx.compose.material.Divider
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -26,11 +31,13 @@ import java.io.File
@Composable
@Preview
fun App() {
var generatePresignerUrlResult by remember { mutableStateOf("") }
val sampleViewModel = SampleViewModel()
val bucketName = "test-bucket-desktop-app"
val key = "test.jpg"
val scope = rememberCoroutineScope()
val bucketList by sampleViewModel.bucketList.collectAsState()
val bucket by sampleViewModel.bucket.collectAsState()
val generatePresignedUrl by sampleViewModel.generatePresignedUrl.collectAsState()

MaterialTheme {
Column(
Expand All @@ -56,11 +63,22 @@ fun App() {
}
)
}
LazyColumn {
items(bucketList) { item ->
Text("${item.name}")
}
}

Divider()
Text("Status bucket: $bucket")
Divider()
Text("Status generate presigned url: $generatePresignedUrl")

CButton(text = "generatePresignedUrl") {
generatePresignerUrlResult = sampleViewModel.generatePresignedUrl(
sampleViewModel.generatePresignedUrl(
bucketName = bucketName,
key = key
) ?: ""
)
}
CButton(text = "Create bucket") {
scope.launch {
Expand Down
2 changes: 1 addition & 1 deletion example/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
agp = "8.1.3"
awsS3 = "0.4.4"
awsS3 = "0.5.0"
kotlin = "1.9.20"
compose = "1.5.4"
compose-compiler = "1.5.4"
Expand Down
2 changes: 1 addition & 1 deletion example/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#Thu Nov 09 01:52:10 COT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
39 changes: 39 additions & 0 deletions example/iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
058557BB273AAA24004C7B11 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557BA273AAA24004C7B11 /* Assets.xcassets */; };
058557D9273AAEEB004C7B11 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557D8273AAEEB004C7B11 /* Preview Assets.xcassets */; };
14B556C42B02FCD900F83F55 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14B556C32B02FCD900F83F55 /* AppDelegate.swift */; };
14D2717A2B069C5E008E0210 /* KMMViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14D271792B069C5E008E0210 /* KMMViewModel.swift */; };
14D2717D2B069CFA008E0210 /* KMMViewModelCore in Frameworks */ = {isa = PBXBuildFile; productRef = 14D2717C2B069CFA008E0210 /* KMMViewModelCore */; };
14D2717F2B069CFA008E0210 /* KMMViewModelSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 14D2717E2B069CFA008E0210 /* KMMViewModelSwiftUI */; };
2080562A60F485076008115A /* Pods_iosApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B5A9C2A9FE107A1EB6B1A594 /* Pods_iosApp.framework */; };
2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2152FB032600AC8F00CF470E /* iOSApp.swift */; };
7555FF83242A565900829871 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7555FF82242A565900829871 /* ContentView.swift */; };
Expand All @@ -19,6 +22,7 @@
058557BA273AAA24004C7B11 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
058557D8273AAEEB004C7B11 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
14B556C32B02FCD900F83F55 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
14D271792B069C5E008E0210 /* KMMViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMMViewModel.swift; sourceTree = "<group>"; };
2152FB032600AC8F00CF470E /* iOSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSApp.swift; sourceTree = "<group>"; };
6BCC0A48C5FCF9A097A71478 /* Pods-iosApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iosApp.debug.xcconfig"; path = "Target Support Files/Pods-iosApp/Pods-iosApp.debug.xcconfig"; sourceTree = "<group>"; };
7555FF7B242A565900829871 /* iosApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = iosApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -33,6 +37,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
14D2717D2B069CFA008E0210 /* KMMViewModelCore in Frameworks */,
14D2717F2B069CFA008E0210 /* KMMViewModelSwiftUI in Frameworks */,
2080562A60F485076008115A /* Pods_iosApp.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -75,6 +81,7 @@
2152FB032600AC8F00CF470E /* iOSApp.swift */,
058557D7273AAEEB004C7B11 /* Preview Content */,
14B556C32B02FCD900F83F55 /* AppDelegate.swift */,
14D271792B069C5E008E0210 /* KMMViewModel.swift */,
);
path = iosApp;
sourceTree = "<group>";
Expand Down Expand Up @@ -114,6 +121,10 @@
dependencies = (
);
name = iosApp;
packageProductDependencies = (
14D2717C2B069CFA008E0210 /* KMMViewModelCore */,
14D2717E2B069CFA008E0210 /* KMMViewModelSwiftUI */,
);
productName = iosApp;
productReference = 7555FF7B242A565900829871 /* iosApp.app */;
productType = "com.apple.product-type.application";
Expand Down Expand Up @@ -142,6 +153,9 @@
Base,
);
mainGroup = 7555FF72242A565900829871;
packageReferences = (
14D2717B2B069CFA008E0210 /* XCRemoteSwiftPackageReference "KMM-ViewModel" */,
);
productRefGroup = 7555FF7C242A565900829871 /* Products */;
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -212,6 +226,7 @@
files = (
14B556C42B02FCD900F83F55 /* AppDelegate.swift in Sources */,
2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */,
14D2717A2B069C5E008E0210 /* KMMViewModel.swift in Sources */,
7555FF83242A565900829871 /* ContentView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -399,6 +414,30 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
14D2717B2B069CFA008E0210 /* XCRemoteSwiftPackageReference "KMM-ViewModel" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/rickclephas/KMM-ViewModel.git";
requirement = {
branch = master;
kind = branch;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
14D2717C2B069CFA008E0210 /* KMMViewModelCore */ = {
isa = XCSwiftPackageProductDependency;
package = 14D2717B2B069CFA008E0210 /* XCRemoteSwiftPackageReference "KMM-ViewModel" */;
productName = KMMViewModelCore;
};
14D2717E2B069CFA008E0210 /* KMMViewModelSwiftUI */ = {
isa = XCSwiftPackageProductDependency;
package = 14D2717B2B069CFA008E0210 /* XCRemoteSwiftPackageReference "KMM-ViewModel" */;
productName = KMMViewModelSwiftUI;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 7555FF73242A565900829871 /* Project object */;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "kmm-viewmodel",
"kind" : "remoteSourceControl",
"location" : "https://github.com/rickclephas/KMM-ViewModel.git",
"state" : {
"branch" : "master",
"revision" : "5122024085c468d006c66afc3ae37f233f88ec1f"
}
}
],
"version" : 2
}
2 changes: 1 addition & 1 deletion example/iosApp/iosApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func initializeS3(){

let credentialsProvider = AWSStaticCredentialsProvider(accessKey: "1", secretKey: "1")
let credentialsProvider = AWSStaticCredentialsProvider(accessKey: BuildPublicConfig().accessKey, secretKey: BuildPublicConfig().secretKey)
let configuration = AWSServiceConfiguration.init(region: .USEast1, credentialsProvider: credentialsProvider)

AWSServiceManager.default().defaultServiceConfiguration = configuration
Expand Down
82 changes: 75 additions & 7 deletions example/iosApp/iosApp/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,83 @@
import SwiftUI
import shared
import KMMViewModelSwiftUI
import PhotosUI

@available(iOS 16.0, *)
struct ContentView: View {
var greet = "Hi"

@StateViewModel var sampleViewModel = SampleViewModel()
let bucketName = "test-bucket-desktop-app"
let key = "test.jpg"
@State private var selectedItem: PhotosPickerItem? = nil
@State private var selectedImageData: Data? = nil

var body: some View {
Text(greet)
PhotosPicker(
selection: $selectedItem,
matching: .images,
photoLibrary: .shared()) {
Text("Select a photo")
}
.onChange(of: selectedItem) { newItem in
Task {
// Retrieve selected asset in the form of Data
if let data = try? await newItem?.loadTransferable(type: Data.self) {
selectedImageData = data
}
}
}

if let selectedImageData,
let uiImage = UIImage(data: selectedImageData) {
Image(uiImage: uiImage)
.resizable()
.scaledToFit()
.frame(width: 250, height: 250)
}


VStack {
List {
ForEach(sampleViewModel.bucketList, id: \.name){ data in
Text(data.name ?? "Default value")
}
}
Text("Status bucket: \(sampleViewModel.bucket)")
Text("Status generate presigned url: \(sampleViewModel.generatePresignedUrl)")
Button(action: {
sampleViewModel.createBucket(bucketName: bucketName)
}, label: {
Text("Create bucket")
})
Button(action: {
sampleViewModel.listBuckets()
}, label: {
Text("List bucket")
})
Button(action: {
sampleViewModel.deleteBucket(bucketName: bucketName)
}, label: {
Text("Delete bucket")
})
Button(action: {
sampleViewModel.generatePresignedUrl(bucketName: bucketName, key: key)
}, label: {
Text("Generate presigned URL")
})

Button(action: {

if let selectedImageData,
let uiImage = UIImage(data: selectedImageData) {
sampleViewModel.putObject(bucketName: bucketName, key: key, imageFile: uiImage)
}


}, label: {
Text("Put object")
})
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
12 changes: 12 additions & 0 deletions example/iosApp/iosApp/KMMViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// KMMViewModel.swift
// iosApp
//
// Created by Estiven on 16/11/23.
// Copyright © 2023 orgName. All rights reserved.
//

import KMMViewModelCore
import shared

extension Kmm_viewmodel_coreKMMViewModel: KMMViewModel { }
Loading

0 comments on commit 074f86b

Please sign in to comment.