Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reusable Composable #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
reusable Composable
  • Loading branch information
GitAmitesh committed Feb 21, 2024
commit 2bf3313511e0a2e405b9c510122a4cfe5c283ed0
1 change: 1 addition & 0 deletions Team 220/frontend/.idea/gradle.xml

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

1 change: 0 additions & 1 deletion Team 220/frontend/.idea/misc.xml

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

Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.sitsocialapp.SideBarMenuItem
import com.example.sitsocialhacknation.SideBarMenuItem

@Composable
fun SideBarDrawer(){
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.sitsocialapp.classes

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory





/**
* Retrofit instance class
*/
class ApiClient {
private lateinit var apiService: ApiService

fun getApiService(): ApiService {

// Initialize ApiService if not initialized yet
if (!::apiService.isInitialized) {
val retrofit = Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()

apiService = retrofit.create(ApiService::class.java)
}

return apiService
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.sitsocialapp.classes

import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.FormUrlEncoded
import retrofit2.http.POST


/**
* Interface for defining REST request functions
*/
interface ApiService {

@POST(Constants.LOGIN_URL)
@FormUrlEncoded
fun login(@Body request: LoginRequest): Call<LoginResponse>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.sitsocialapp.classes


object Constants {

// Endpoints
const val BASE_URL = "https://baseurl.com/"
const val LOGIN_URL = "auth/login"
const val POSTS_URL = "posts"

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.sitsocialapp.classes

import android.os.Bundle

import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import com.example.sitsocialapp.R
import com.example.sitsocialapp.screens.LoginScreen
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class LoginActivity : AppCompatActivity() {
private lateinit var sessionManager: SessionManager
private lateinit var apiClient: ApiClient

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContent(content = {LoginScreen()})

apiClient = ApiClient()
sessionManager = SessionManager(this)

apiClient.getApiService().login(LoginRequest(email = "[email protected]", password = "mypassword"))
.enqueue(object : Callback<LoginResponse> {
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
// Error logging in
}

override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
val loginResponse = response.body()

if (loginResponse?.statusCode == 200 && loginResponse.user != null) {
sessionManager.saveAuthToken(loginResponse.authToken)
} else {
// Error logging in
}
}
})

}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.sitsocialapp.classes

import com.google.gson.annotations.SerializedName

data class LoginRequest (
@SerializedName("email")
var email: String,

@SerializedName("password")
var password: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.sitsocialapp.classes

import com.google.gson.annotations.SerializedName


data class LoginResponse (
@SerializedName("status_code")
var statusCode: Int,

@SerializedName("auth_token")
var authToken: String,

@SerializedName("user")
var user: User
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.sitsocialapp.classes

import android.content.Context
import android.content.SharedPreferences
import com.example.sitsocialapp.R

/**
* Session manager to save and fetch data from SharedPreferences
*/
class SessionManager (context: Context) {
private var prefs: SharedPreferences = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE)

companion object {
const val USER_TOKEN = "user_token"
}

/**
* Function to save auth token
*/
fun saveAuthToken(token: String) {
val editor = prefs.edit()
editor.putString(USER_TOKEN, token)
editor.apply()
}

/**
* Function to fetch auth token
*/
fun fetchAuthToken(): String? {
return prefs.getString(USER_TOKEN, null)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.sitsocialapp.classes

import com.google.gson.annotations.SerializedName

data class User(
@SerializedName("name")
var name: String,

@SerializedName("username")
var username: String,

@SerializedName("email")
var email: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.sitsocialhacknation.screens
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp

@Composable
fun CreatePostScreen(){
Column (horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize().background(Color.Red)){
Text(text = "Create Post Screen",
color = Color.White,
fontSize = 24.sp)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.sitsocialhacknation.screens

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import com.example.sitsocialhacknation.ui.theme.BackgroundColor

@Preview
@Composable
fun NotificationScreen(){
Column (horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize()
.background(BackgroundColor)){
TopBar(screenName = "Notifications")
Notifications()
}
}
Loading