-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
15 changed files
with
476 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
101 changes: 101 additions & 0 deletions
101
app/src/main/java/net/accelf/itc_lms_unofficial/coursedetail/SendAttendanceDialogFragment.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,101 @@ | ||
package net.accelf.itc_lms_unofficial.coursedetail | ||
|
||
import android.annotation.SuppressLint | ||
import android.app.AlertDialog | ||
import android.app.Dialog | ||
import android.content.DialogInterface | ||
import android.os.Bundle | ||
import android.view.View.GONE | ||
import android.view.View.VISIBLE | ||
import androidx.core.os.bundleOf | ||
import androidx.core.widget.addTextChangedListener | ||
import androidx.fragment.app.DialogFragment | ||
import androidx.fragment.app.activityViewModels | ||
import androidx.fragment.app.setFragmentResult | ||
import kotlinx.android.synthetic.main.dialog_send_attendance.view.* | ||
import net.accelf.itc_lms_unofficial.R | ||
import net.accelf.itc_lms_unofficial.util.Success | ||
import net.accelf.itc_lms_unofficial.util.isNotNullOrEmpty | ||
|
||
@SuppressLint("InflateParams") | ||
class SendAttendanceDialogFragment : DialogFragment() { | ||
|
||
private val layout by lazy { | ||
layoutInflater.inflate(R.layout.dialog_send_attendance, null) | ||
} | ||
|
||
private val viewModel by activityViewModels<CourseDetailViewModel>() | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
return activity?.let { activity -> | ||
AlertDialog.Builder(activity).apply { | ||
setTitle(R.string.dialog_title_send_attendance) | ||
setView(layout) | ||
|
||
setPositiveButton(R.string.button_dialog_send) { _, _ -> | ||
setFragmentResult( | ||
this@SendAttendanceDialogFragment::class.java.simpleName, | ||
bundleOf( | ||
BUNDLE_RESULT to Result.success(SendAttendanceDialogResult( | ||
layout.editTextAttendancePassword.text.toString(), | ||
layout.editTextAttendanceComment.text.toString())) | ||
) | ||
) | ||
} | ||
setNegativeButton(R.string.button_dialog_cancel) { _, _ -> | ||
setFragmentResult( | ||
this@SendAttendanceDialogFragment::class.java.simpleName, | ||
bundleOf(BUNDLE_RESULT to Result.failure<SendAttendanceDialogResult>( | ||
Throwable())) | ||
) | ||
} | ||
}.create().apply { | ||
setOnShowListener { dialog -> | ||
val positiveButton = | ||
(dialog as AlertDialog).getButton(DialogInterface.BUTTON_POSITIVE) | ||
positiveButton.isEnabled = false | ||
|
||
(viewModel.attendanceSend.value as Success).data.let { attendanceSend -> | ||
layout.textAttendanceAlreadySent.visibility = when (attendanceSend.sent) { | ||
true -> VISIBLE | ||
false -> GONE | ||
} | ||
|
||
layout.textAttendanceError.visibility = | ||
when (attendanceSend.errorOnPassword.isNotEmpty() || attendanceSend.errorOnComment.isNotEmpty()) { | ||
true -> VISIBLE | ||
false -> GONE | ||
} | ||
|
||
// Warning: Using apply causes wrong `this` suggestion | ||
layout.inputLayoutAttendancePassword.error = attendanceSend.errorOnPassword | ||
layout.editTextAttendancePassword.setText(attendanceSend.password) | ||
layout.editTextAttendancePassword.addTextChangedListener { | ||
layout.inputLayoutAttendancePassword.isErrorEnabled = false | ||
positiveButton.isEnabled = it.isNotNullOrEmpty() | ||
} | ||
|
||
layout.inputLayoutAttendanceComment.error = attendanceSend.errorOnComment | ||
layout.editTextAttendanceComment.setText(attendanceSend.comment) | ||
layout.editTextAttendanceComment.addTextChangedListener { | ||
layout.inputLayoutAttendanceComment.isErrorEnabled = false | ||
} | ||
} | ||
} | ||
} | ||
} ?: throw IllegalStateException("Activity cannot be null") | ||
} | ||
|
||
override fun onDismiss(dialog: DialogInterface) { | ||
super.onDismiss(dialog) | ||
viewModel.closeSendAttendance() | ||
} | ||
|
||
companion object { | ||
const val BUNDLE_RESULT = "result" | ||
|
||
fun newInstance(): SendAttendanceDialogFragment { | ||
return SendAttendanceDialogFragment() | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/net/accelf/itc_lms_unofficial/coursedetail/SendAttendanceDialogResult.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,6 @@ | ||
package net.accelf.itc_lms_unofficial.coursedetail | ||
|
||
data class SendAttendanceDialogResult( | ||
val attendancePassword: String, | ||
val attendanceComment: String, | ||
) |
71 changes: 71 additions & 0 deletions
71
app/src/main/java/net/accelf/itc_lms_unofficial/models/AttendanceSend.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,71 @@ | ||
package net.accelf.itc_lms_unofficial.models | ||
|
||
import net.accelf.itc_lms_unofficial.network.DocumentConverterFactory | ||
import net.accelf.itc_lms_unofficial.util.popIf | ||
import net.accelf.itc_lms_unofficial.util.toTimeSpan | ||
import okhttp3.ResponseBody | ||
import java.io.Serializable | ||
import java.util.* | ||
|
||
data class AttendanceSend( | ||
val id: String, | ||
val csrf: String, | ||
val sent: Boolean, | ||
val allowedSince: Date?, | ||
val allowedUntil: Date?, | ||
val lateSince: Date?, | ||
val lateUntil: Date?, | ||
val password: String, | ||
val errorOnPassword: String, | ||
val comment: String, | ||
val errorOnComment: String, | ||
val success: Boolean, | ||
) : Serializable { | ||
|
||
class Converter(baseUrl: String) : | ||
DocumentConverterFactory.DocumentConverter<AttendanceSend>(baseUrl) { | ||
|
||
override fun convert(value: ResponseBody): AttendanceSend? { | ||
document(value).let { document -> | ||
document.select("#attendancesSendForm").first().let { form -> | ||
var allowedSince: Date? = null | ||
var allowedUntil: Date? = null | ||
var lateSince: Date? = null | ||
var lateUntil: Date? = null | ||
form?.select(".subblock_contents .subblock_line .subblock_form .important") | ||
?.apply { | ||
first().text().toTimeSpan().apply { | ||
allowedSince = get(0) | ||
allowedUntil = get(1) | ||
} | ||
last().text().toTimeSpan().apply { | ||
lateSince = get(0) | ||
lateUntil = get(1) | ||
} | ||
} | ||
|
||
val passInput = form?.select("input[name=oneTimePass]")?.first() | ||
val commentTextArea = form?.select("textarea[name=comment]")?.first() | ||
val errors = | ||
form?.select(".subblock_form .errorMsg")?.map { it.text() }?.toMutableList() | ||
|
||
return AttendanceSend( | ||
form?.select("input[name=attendanceId]")?.first()?.`val`() ?: "", | ||
form?.select("input[name=_csrf]")?.first()?.`val`() ?: "", | ||
(form?.select("input[name=isSent]")?.first()?.`val`() ?: "true") == "true", | ||
allowedSince, | ||
allowedUntil, | ||
lateSince, | ||
lateUntil, | ||
passInput?.`val`() ?: "", | ||
errors?.popIf { passInput?.hasClass("inputErrorField") ?: false } ?: "", | ||
commentTextArea?.text() ?: "", | ||
errors?.popIf { commentTextArea?.hasClass("inputErrorField") ?: false } | ||
?: "", | ||
document.select("#attendanceSendComplete").isNotEmpty(), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.