-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66daaf6
commit 21cac33
Showing
10 changed files
with
222 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
app_jetpack_compose/src/main/java/app/quiltt/app_jetpack_compose/QuilttHubActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package app.quiltt.app_jetpack_compose | ||
|
||
import android.os.Bundle | ||
import android.view.ViewGroup | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
|
||
class QuilttHubActivity : ComponentActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
QuilttHubContent() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun QuilttHubContent() { | ||
val token = SharedPreferencesHelper(context = LocalContext.current).getData("token") | ||
val url = "https://www.quiltthub.com/login?token=$token" | ||
println(url) | ||
WebViewComposable(url = url) | ||
} | ||
|
||
@Composable | ||
fun WebViewComposable(url: String) { | ||
AndroidView(factory = { context -> | ||
WebView(context).apply { | ||
layoutParams = ViewGroup.LayoutParams( | ||
ViewGroup.LayoutParams.MATCH_PARENT, | ||
ViewGroup.LayoutParams.MATCH_PARENT | ||
) | ||
settings.javaScriptEnabled = true | ||
settings.domStorageEnabled = true | ||
webViewClient = WebViewClient() | ||
loadUrl(url) | ||
} | ||
}, update = { webView -> | ||
webView.loadUrl(url) | ||
}) | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
fun GreetingPreview() { | ||
QuilttHubContent() | ||
} |
19 changes: 19 additions & 0 deletions
19
app_jetpack_compose/src/main/java/app/quiltt/app_jetpack_compose/SharedPreferencesHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package app.quiltt.app_jetpack_compose | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
|
||
class SharedPreferencesHelper(context: Context) { | ||
private val sharedPreferences: SharedPreferences = | ||
context.getSharedPreferences("MyAppPreferences", Context.MODE_PRIVATE) | ||
|
||
fun saveData(key: String, value: String) { | ||
val editor = sharedPreferences.edit() | ||
editor.putString(key, value) | ||
editor.apply() | ||
} | ||
|
||
fun getData(key: String): String? { | ||
return sharedPreferences.getString(key, null) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<resources> | ||
<string name="app_name">App Jetpack Compose</string> | ||
<string name="title_activity_quiltt_connector">QuilttConnectorActivity</string> | ||
<string name="title_activity_quiltt_hub">QuilttHubActivity</string> | ||
</resources> |
96 changes: 96 additions & 0 deletions
96
connector/src/main/java/app/quiltt/connector/QuilttAuthApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package app.quiltt.connector | ||
|
||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
import java.io.OutputStreamWriter | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
import org.json.JSONObject | ||
|
||
data class EmailInput(val email: String) | ||
data class PhoneInput(val phone: String) | ||
|
||
sealed class UsernamePayload { | ||
data class Email(val email: String) : UsernamePayload() | ||
data class Phone(val phone: String) : UsernamePayload() | ||
} | ||
|
||
data class PasscodePayload(val usernamePayload: UsernamePayload, val passcode: String) | ||
|
||
data class SessionData(val token: String) | ||
data class UnauthorizedData(val message: String, val instruction: String) | ||
data class UnprocessableData(val attribute: Map<String, List<String>>) | ||
|
||
class QuilttAuthApi(private val clientId: String?) { | ||
private val endpointAuth = "https://www.quiltt.dev/api-reference/rest/auth" | ||
|
||
fun ping(token: String): SessionData { | ||
val url = URL(endpointAuth) | ||
val connection = url.openConnection() as HttpURLConnection | ||
connection.requestMethod = "GET" | ||
connection.setRequestProperty("Authorization", "Bearer $token") | ||
|
||
val reader = BufferedReader(InputStreamReader(connection.inputStream)) | ||
val response = reader.readLine() | ||
reader.close() | ||
|
||
val jsonObject = JSONObject(response) | ||
return SessionData(jsonObject.getString("token")) | ||
} | ||
|
||
fun identify(payload: UsernamePayload): SessionData { | ||
val url = URL(endpointAuth) | ||
val connection = url.openConnection() as HttpURLConnection | ||
connection.requestMethod = "POST" | ||
connection.setRequestProperty("Content-Type", "application/json; utf-8") | ||
connection.doOutput = true | ||
|
||
val jsonPayload = JSONObject() | ||
jsonPayload.put("clientId", clientId) | ||
jsonPayload.put("payload", payload) | ||
|
||
val writer = OutputStreamWriter(connection.outputStream) | ||
writer.write(jsonPayload.toString()) | ||
writer.flush() | ||
writer.close() | ||
|
||
val reader = BufferedReader(InputStreamReader(connection.inputStream)) | ||
val response = reader.readLine() | ||
reader.close() | ||
|
||
val jsonObject = JSONObject(response) | ||
return SessionData(jsonObject.getString("token")) | ||
} | ||
|
||
fun authenticate(payload: PasscodePayload): SessionData { | ||
val url = URL(endpointAuth) | ||
val connection = url.openConnection() as HttpURLConnection | ||
connection.requestMethod = "PUT" | ||
connection.setRequestProperty("Content-Type", "application/json; utf-8") | ||
connection.doOutput = true | ||
|
||
val jsonPayload = JSONObject() | ||
jsonPayload.put("clientId", clientId) | ||
jsonPayload.put("payload", payload) | ||
|
||
val writer = OutputStreamWriter(connection.outputStream) | ||
writer.write(jsonPayload.toString()) | ||
writer.flush() | ||
writer.close() | ||
|
||
val reader = BufferedReader(InputStreamReader(connection.inputStream)) | ||
val response = reader.readLine() | ||
reader.close() | ||
|
||
val jsonObject = JSONObject(response) | ||
return SessionData(jsonObject.getString("token")) | ||
} | ||
|
||
fun revoke(token: String) { | ||
val url = URL(endpointAuth) | ||
val connection = url.openConnection() as HttpURLConnection | ||
connection.requestMethod = "DELETE" | ||
connection.setRequestProperty("Authorization", "Bearer $token") | ||
connection.connect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.