Commit 989db212 authored by Ricki Hirner's avatar Ricki Hirner 🐑

Pre-set calendar color; adapt validation

parent 8a6386ec
Pipeline #52635665 failed with stage
in 4 minutes and 9 seconds
......@@ -11,12 +11,11 @@ package at.bitfire.icsdroid.ui
import android.app.Application
import android.app.Dialog
import android.app.ProgressDialog
import android.content.Context
import android.os.Bundle
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import at.bitfire.ical4android.Event
......@@ -41,13 +40,13 @@ class AddCalendarValidationFragment: DialogFragment() {
credentialsModel = ViewModelProviders.of(requireActivity()).get(CredentialsFragment.CredentialsModel::class.java)
validationModel = ViewModelProviders.of(requireActivity()).get(ValidationModel::class.java)
val url = URL(titleColorModel.url.value ?: throw IllegalArgumentException("No URL given"))
validationModel.results(url, credentialsModel.username.value, credentialsModel.password.value).observe(this, Observer { info ->
validationModel.result.observe(this, Observer { info ->
dialog.dismiss()
val errorMessage = info.exception?.localizedMessage
if (errorMessage == null) {
titleColorModel.url.value = info.url.toString()
titleColorModel.color.value = resources.getColor(R.color.colorPrimary)
if (titleColorModel.title.value.isNullOrBlank())
titleColorModel.title.value = info.calendarName ?: info.url?.file
......@@ -60,6 +59,9 @@ class AddCalendarValidationFragment: DialogFragment() {
} else
Toast.makeText(activity, errorMessage, Toast.LENGTH_SHORT).show()
})
val url = URL(titleColorModel.url.value ?: throw IllegalArgumentException("No URL given"))
validationModel.initialize(url, credentialsModel.username.value, credentialsModel.password.value)
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
......@@ -85,22 +87,18 @@ class AddCalendarValidationFragment: DialogFragment() {
application: Application
): AndroidViewModel(application) {
fun results(url: URL, username: String?, password: String?) =
CalendarSourceInfo(getApplication(), url, username, password)
val result = MutableLiveData<ResourceInfo>()
var initialized = false
}
class CalendarSourceInfo(
context: Context,
originalUrl: URL,
username: String?,
password: String?
): LiveData<ResourceInfo>() {
fun initialize(originalUrl: URL, username: String?, password: String?) {
synchronized(initialized) {
if (initialized)
return
initialized = true
}
init {
val info = ResourceInfo()
val downloader = object: CalendarFetcher(context, originalUrl) {
val downloader = object: CalendarFetcher(getApplication(), originalUrl) {
override fun onSuccess(data: InputStream, contentType: MediaType?, eTag: String?, lastModified: Long?) {
InputStreamReader(data, contentType?.charset() ?: Charsets.UTF_8).use { reader ->
val properties = mutableMapOf<String, String>()
......@@ -113,12 +111,12 @@ class AddCalendarValidationFragment: DialogFragment() {
info.eventsFound = events.size
}
postValue(info)
result.postValue(info)
}
override fun onError(error: Exception) {
info.exception = error
postValue(info)
result.postValue(info)
}
}
......
......@@ -14,6 +14,7 @@ import android.graphics.Paint
import android.graphics.drawable.shapes.OvalShape
import android.util.AttributeSet
import android.view.View
import at.bitfire.icsdroid.R
class ColorButton(
context: Context,
......@@ -24,7 +25,6 @@ class ColorButton(
private var paint = Paint()
init {
paint.color = 0xFFFF0000.toInt()
paint.isAntiAlias = true
}
......
......@@ -42,13 +42,7 @@ class TitleColorFragment: Fragment() {
intent.putExtra(ColorPickerActivity.EXTRA_COLOR, it)
}
startActivityForResult(intent, 0)
}
model.color.observe(this, Observer { color ->
if (color != null)
v.color.setColor(color)
})
return v
}
......
......@@ -7,7 +7,7 @@
~ http://www.gnu.org/licenses/gpl.html
-->
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable name="model" type="at.bitfire.icsdroid.ui.TitleColorFragment.TitleColorModel"/>
......@@ -29,9 +29,9 @@
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="1dp"
card_view:contentPadding="10dp">
app:cardCornerRadius="4dp"
app:cardElevation="1dp"
app:contentPadding="10dp">
<LinearLayout
android:layout_width="match_parent"
......@@ -65,6 +65,7 @@
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
app:color="@{model.color}"
tools:ignore="RtlHardcoded"/>
</LinearLayout>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment