Skip to content
This repository has been archived by the owner on Jun 23, 2024. It is now read-only.

Commit

Permalink
wtf
Browse files Browse the repository at this point in the history
Signed-off-by: AlexNg <contact@ngjx.org>
  • Loading branch information
caffeine-addictt committed Jun 21, 2024
1 parent 5c066e8 commit 0b154c9
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
package com.example.studentmobileapp.Models

package com.example.studentmobileapp.Models

import kotlinx.serialization.Serializable


@Serializable
public class ApiResponse<T> {
var status: Int = 0
val message: String = ""
val data: T = null!!
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,3 @@ class UserLoginReq {
var username: String = ""
var password: String = ""
}

@Serializable
class UserLoginResp {
var status: Int = 0
var message: String = ""
var data: User? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ class Workshop {
var workshopId: Int = 0
var title: String = ""
var category: Category? = null
var status: String = ""
var requestDate: String = ""
var timeslot: String = ""
var endDate: String = ""
var startDate: String = ""
var saloon: Saloon? = null
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CreateWorkshopRequestReq
var endDate: String = "" // yyyy-mm-dd
var timeslot: Int = 0 // 0-10:12 1-12:14 2-14:16
var category: String = ""
var saloon: Int = 0
var saloon: String = ""
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,19 @@ inline fun <reified T, reified V>HttpPost(endpoint: String, req: V, parseStatus:
os.close()
Log.d("POST", "response code "+con.responseCode);

con.disconnect();

val reader = BufferedReader(InputStreamReader(con.inputStream))
val jsonData = StringBuilder()

if (con.responseCode !in parseStatus) {
return null
}

val reader = BufferedReader(InputStreamReader(con.inputStream))
val jsonData = StringBuilder()

var line: String?
while (reader.readLine().also { line = it } != null) {
jsonData.append(line)
}
reader.close()
con.disconnect();

//Read JSON response and print
try {
Expand All @@ -63,13 +62,13 @@ inline fun <reified T>HttpGet(endpoint: String, parseStatus: List<Int> = listOf(

Log.d("GET", "response code "+con.responseCode);

val reader = BufferedReader(InputStreamReader(con.inputStream))
val jsonData = StringBuilder()

if (con.responseCode !in parseStatus) {
return null
}

val reader = BufferedReader(InputStreamReader(con.inputStream))
val jsonData = StringBuilder()

var line: String?
while (reader.readLine().also { line = it } != null) {
jsonData.append(line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ package com.example.studentmobileapp.Utilities
import android.os.AsyncTask
import android.util.Log
import com.example.studentmobileapp.LoginFragment
import com.example.studentmobileapp.Models.ApiResponse
import com.example.studentmobileapp.Models.User
import com.example.studentmobileapp.Models.UserLoginReq
import com.example.studentmobileapp.Models.UserLoginResp
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStream
import java.net.HttpURLConnection
import java.net.URL


class LoginUserHttpService(private var context: LoginFragment, private var req: UserLoginReq): AsyncTask<Void, Void, Boolean>() {
override fun doInBackground(vararg params: Void?): Boolean {
val response = HttpPost<UserLoginResp, UserLoginReq>(
val response = HttpPost<ApiResponse<User>, UserLoginReq>(
"/api/auth/login",
req,
listOf(200)
Expand All @@ -27,7 +21,7 @@ class LoginUserHttpService(private var context: LoginFragment, private var req:
return false
}

Log.i("Auth", "Authenticated user ${response.data!!.userId}")
Log.i("Auth", "Authenticated user ${response.data.userId}")
Settings.user = response.data
return true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,71 @@
//package com.example.studentmobileapp.Utilities
//
//import com.example.studentmobileapp.WorkshopsFragment
//
//class WorkshopService(private var context: WorkshopsFragment, private var req: UserLoginReq): AsyncTask<Void, Void, Boolean>() {
//
//}
package com.example.studentmobileapp.Utilities

import android.os.AsyncTask
import com.example.studentmobileapp.Models.ApiResponse
import com.example.studentmobileapp.Models.CreateWorkshopRequestReq
import com.example.studentmobileapp.Models.CreateWorkshopRequestResp
import com.example.studentmobileapp.Models.Workshop
import com.example.studentmobileapp.WorkshopRequestAddFragment
import com.example.studentmobileapp.WorkshopRequestFragment
import com.example.studentmobileapp.databinding.WorkshopRequestViewBinding

class CreateWorkshopRequestHttpService(private var context: WorkshopRequestAddFragment, private var req: CreateWorkshopRequestReq): AsyncTask<Void, Void, String?>() {
@Deprecated("Deprecated in Java")
override fun doInBackground(vararg params: Void?): String? {
val request = HttpPost<CreateWorkshopRequestResp, CreateWorkshopRequestReq>(
"/api/workshop-request",
req,
listOf(201)
)

if (request == null) {
return "Failed to create workshop request"
}

return null
}

@Deprecated("Deprecated in Java", ReplaceWith("super.onPreExecute()", "android.os.AsyncTask"))
override fun onPreExecute() {
super.onPreExecute()
}

@Deprecated("Deprecated in Java")
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
if(result == null) {
context.onCreateSuccess()
}
else {
context.onCreateError(result)
}
}
}

class GetWorkshopRequestHttpService(private var context: WorkshopRequestFragment, private var forRequest: Boolean): AsyncTask<Void, Void, List<Workshop>?>() {
@Deprecated("Deprecated in Java")
override fun doInBackground(vararg params: Void?): List<Workshop>? {
val request = HttpGet<ApiResponse<List<Workshop>>>(
"/api/workshop?userid=${Settings.user!!.userId}",
listOf(200)
)

return request?.data
}

@Deprecated("Deprecated in Java", ReplaceWith("super.onPreExecute()", "android.os.AsyncTask"))
override fun onPreExecute() {
super.onPreExecute()
}

@Deprecated("Deprecated in Java")
override fun onPostExecute(result: List<Workshop>?) {
super.onPostExecute(result)
if(result != null) {
context.populateWorkshopRequests(result)
}
else {
context.onFetchError()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package com.example.studentmobileapp
import android.app.DatePickerDialog
import android.icu.util.Calendar
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.DatePicker
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
Expand All @@ -18,8 +17,6 @@ import com.example.studentmobileapp.Utilities.CreateWorkshopRequestHttpService
import com.example.studentmobileapp.Utilities.GetSaloonHttpService
import com.example.studentmobileapp.Utilities.Settings
import com.example.studentmobileapp.databinding.WorkshopRequestAddViewBinding
import java.text.SimpleDateFormat
import java.util.Locale


class WorkshopRequestAddFragment : Fragment() {
Expand Down Expand Up @@ -91,7 +88,7 @@ class WorkshopRequestAddFragment : Fragment() {
binding.endDateBtn.text = formattedDate
}

binding.startDateBtn.setOnClickListener() {
binding.startDateBtn.setOnClickListener {
DatePickerDialog(
requireActivity(),
startDateSetListener,
Expand All @@ -100,7 +97,7 @@ class WorkshopRequestAddFragment : Fragment() {
startCal.get(Calendar.DAY_OF_MONTH)
).show()
}
binding.endDateBtn.setOnClickListener() {
binding.endDateBtn.setOnClickListener {
DatePickerDialog(
requireActivity(),
endDateSetListener,
Expand All @@ -114,11 +111,16 @@ class WorkshopRequestAddFragment : Fragment() {

// Populate Timeslots
val timeslots = resources.getStringArray(R.array.timeslots)
val adapter = ArrayAdapter(requireActivity(), android.R.layout.simple_spinner_dropdown_item, timeslots)
binding.timeslotSP.adapter = adapter
val adapterT = ArrayAdapter(requireActivity(), android.R.layout.simple_spinner_dropdown_item, timeslots)
binding.timeslotSP.adapter = adapterT

// Populate Categories
val categories = resources.getStringArray(R.array.categories)
val adapterC = ArrayAdapter(requireActivity(), android.R.layout.simple_spinner_dropdown_item, categories)
binding.categorySP.adapter = adapterC

// Handle create
binding.createBtn.setOnClickListener() {
binding.createBtn.setOnClickListener {
if (saloons.isEmpty()) {
Toast.makeText(requireActivity(), "Saloons are not yet populated", Toast.LENGTH_SHORT).show()
return@setOnClickListener
Expand Down Expand Up @@ -154,25 +156,24 @@ class WorkshopRequestAddFragment : Fragment() {
return@setOnClickListener
}

if (binding.saloonSP.selectedItem == "Loading...") {
if (binding.saloonSP.selectedItem.toString() == "Loading...") {
Toast.makeText(requireActivity(), "Invalid saloon!", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}

// Make request
val createReq: CreateWorkshopRequestReq = CreateWorkshopRequestReq()
val createReq = CreateWorkshopRequestReq()
createReq.userId = Settings.user!!.userId
createReq.title = binding.titleET.text.toString()
createReq.category = binding.categoryET.text.toString()
createReq.category = binding.categorySP.selectedItem.toString()
createReq.startDate = startDatePicked
createReq.endDate = endDatePicked
createReq.saloon = saloons.get(binding.saloonSP.selectedItemPosition).saloonId
createReq.saloon = binding.saloonSP.selectedItem.toString()

CreateWorkshopRequestHttpService(this, createReq).execute()
}
}


fun populateSaloon(saloonData: List<Saloon?>) {
val saloonDataF = saloonData.filterNotNull()
saloons = saloonDataF
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.example.studentmobileapp

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.studentmobileapp.Utilities.Settings
import com.example.studentmobileapp.Models.Workshop
import com.example.studentmobileapp.Utilities.GetWorkshopRequestHttpService
import com.example.studentmobileapp.databinding.WorkshopRequestViewBinding
import java.util.Locale


class WorkshopRequestFragment : Fragment() {
Expand All @@ -27,6 +32,7 @@ class WorkshopRequestFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)

(requireActivity() as MainActivity).title = "Workshop Requests"
GetWorkshopRequestHttpService(this, true).execute()
binding.fab.setOnClickListener() {
findNavController().navigate(R.id.action_WorkshopRequest_to_WorkshopRequestAdd)
}
Expand All @@ -36,4 +42,33 @@ class WorkshopRequestFragment : Fragment() {
super.onDestroyView()
_binding = null
}

fun populateWorkshopRequests(workshopData: List<Workshop?>) {
val myListAdapter = WorkshopRequestAdapter(requireActivity() as MainActivity, workshopData.filterNotNull())
binding.wrList.adapter = myListAdapter
}

fun onFetchError() {
Toast.makeText(requireActivity(), "Failed to fetch workshop request!", Toast.LENGTH_SHORT).show()
}
}


class WorkshopRequestAdapter(private val context: Activity, private val workshopData: List<Workshop>): ArrayAdapter<String>(context, R.layout.workshop_request_listview_row, workshopData.map{ w -> w.title}) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val inflater = context.layoutInflater
val rowView = inflater.inflate(R.layout.workshop_request_listview_row, null, true)

// Get views
val titleText: TextView = rowView.findViewById(R.id.header)
val subtitleText: TextView = rowView.findViewById(R.id.text)

val data = workshopData[position]
var dateString = if (data.startDate == data.endDate) data.startDate else "${data.startDate} to ${data.endDate}"

titleText.text = "[${data.status.uppercase(Locale.ROOT)}] ${data.title}"
subtitleText.text = "${data.timeslot}\n$dateString\n@ ${data.saloon!!.saloonName}"

return rowView
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class WorkshopsFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

(requireActivity() as MainActivity).title = "Workshops"
(requireActivity() as MainActivity).title = "Home"

if (Settings.user == null) {
binding.workshopReqButton.visibility = View.GONE
Expand Down
13 changes: 0 additions & 13 deletions Mobile/app/src/main/res/layout/login_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@
android:orientation="vertical"
android:padding="16dp">

<!--header-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="25dp" >
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>

<!--start of username-->
<LinearLayout
android:layout_width="match_parent"
Expand Down
9 changes: 5 additions & 4 deletions Mobile/app/src/main/res/layout/workshop_request_add_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wrAdd_category" />
<EditText
android:id="@+id/categoryET"
<Spinner
android:id="@+id/categorySP"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</EditText>
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<!--end of category-->

Expand Down
Loading

0 comments on commit 0b154c9

Please sign in to comment.