Move renaming device to dialog

This commit is contained in:
Jonas L 2019-01-14 13:17:19 +01:00
parent 750d9ffb60
commit aa6266ac3f
6 changed files with 117 additions and 73 deletions

View file

@ -53,10 +53,6 @@ import io.timelimit.android.ui.main.AuthenticationFab
import io.timelimit.android.ui.main.FragmentWithCustomTitle
class ManageDeviceFragment : Fragment(), FragmentWithCustomTitle {
companion object {
private const val IS_EDITING_DEVICE_TITLE = "a"
}
private val activity: ActivityViewModelHolder by lazy { getActivity() as ActivityViewModelHolder }
private val logic: AppLogic by lazy { DefaultAppLogic.with(context!!) }
private val auth: ActivityViewModel by lazy { activity.getActivityViewModel() }
@ -65,22 +61,6 @@ class ManageDeviceFragment : Fragment(), FragmentWithCustomTitle {
logic.database.device().getDeviceById(args.deviceId)
}
private var isEditingDeviceTitle = MutableLiveData<Boolean>().apply { value = false }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState != null) {
isEditingDeviceTitle.value = savedInstanceState.getBoolean(IS_EDITING_DEVICE_TITLE)
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(IS_EDITING_DEVICE_TITLE, isEditingDeviceTitle.value!!)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val navigation = Navigation.findNavController(container!!)
val binding = FragmentManageDeviceBinding.inflate(inflater, container, false)
@ -185,27 +165,9 @@ class ManageDeviceFragment : Fragment(), FragmentWithCustomTitle {
}
}
override fun startEditDeviceTitle() {
override fun editDeviceTitle() {
if (auth.requestAuthenticationOrReturnTrue()) {
binding.newDeviceTitleText.setText(binding.deviceTitle)
isEditingDeviceTitle.value = true
}
}
override fun doEditDeviceTitle() {
val newDeviceTitle = binding.newDeviceTitleText.text.toString()
if (newDeviceTitle.isBlank()) {
Snackbar.make(binding.newDeviceTitleText, R.string.manage_device_rename_toast_empty, Snackbar.LENGTH_SHORT).show()
} else {
if (auth.tryDispatchParentAction(
UpdateDeviceNameAction(
deviceId = args.deviceId,
name = newDeviceTitle
)
)) {
isEditingDeviceTitle.value = false
}
UpdateDeviceTitleDialogFragment.newInstance(args.deviceId).show(fragmentManager!!)
}
}
@ -284,8 +246,6 @@ class ManageDeviceFragment : Fragment(), FragmentWithCustomTitle {
binding.isThisDevice = ownDeviceId == args.deviceId
})
isEditingDeviceTitle.observe(this, Observer { binding.isEditingDeviceTitle = it })
ManageDeviceIntroduction.bind(
view = binding.introduction,
database = logic.database,
@ -323,7 +283,6 @@ interface ManageDeviceFragmentHandlers {
fun openUsageStatsSettings()
fun openNotificationAccessSettings()
fun manageDeviceAdmin()
fun startEditDeviceTitle()
fun doEditDeviceTitle()
fun editDeviceTitle()
fun showAuthenticationScreen()
}

View file

@ -0,0 +1,105 @@
/*
* Open TimeLimit Copyright <C> 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.timelimit.android.ui.manage.device.manage
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import io.timelimit.android.R
import io.timelimit.android.coroutines.runAsync
import io.timelimit.android.data.model.Device
import io.timelimit.android.data.model.UserType
import io.timelimit.android.extensions.showSafe
import io.timelimit.android.livedata.waitForNullableValue
import io.timelimit.android.logic.DefaultAppLogic
import io.timelimit.android.sync.actions.UpdateDeviceNameAction
import io.timelimit.android.ui.main.ActivityViewModel
import io.timelimit.android.ui.main.ActivityViewModelHolder
import io.timelimit.android.ui.util.EditTextBottomSheetDialog
class UpdateDeviceTitleDialogFragment: EditTextBottomSheetDialog() {
companion object {
private const val TAG = "UpdateDeviceTitleDialogFragment"
private const val EXTRA_DEVICE_ID = "deviceId"
fun newInstance(deviceId: String) = UpdateDeviceTitleDialogFragment().apply {
arguments = Bundle().apply {
putString(EXTRA_DEVICE_ID, deviceId)
}
}
}
val deviceId: String by lazy { arguments!!.getString(EXTRA_DEVICE_ID) }
val auth: ActivityViewModel by lazy {
(activity as ActivityViewModelHolder).getActivityViewModel()
}
val deviceEntry: LiveData<Device?> by lazy {
DefaultAppLogic.with(context!!).database.device().getDeviceById(deviceId)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
auth.authenticatedUser.observe(this, Observer {
if (it?.second?.type != UserType.Parent) {
dismissAllowingStateLoss()
}
})
deviceEntry.observe(this, Observer {
if (it == null) {
dismissAllowingStateLoss()
}
})
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (savedInstanceState == null) {
runAsync {
deviceEntry.waitForNullableValue()?.let { deviceEntry ->
binding.editText.setText(deviceEntry.name)
didInitField()
}
}
}
binding.title = getString(R.string.manage_device_rename)
}
override fun go() {
val newDeviceTitle = binding.editText.text.toString()
if (newDeviceTitle.isBlank()) {
Toast.makeText(context!!, R.string.manage_device_rename_toast_empty, Toast.LENGTH_SHORT).show()
} else {
if (auth.tryDispatchParentAction(
UpdateDeviceNameAction(
deviceId = deviceId,
name = newDeviceTitle
)
)) {
dismiss()
}
}
}
fun show(fragmentManager: FragmentManager) = showSafe(fragmentManager, TAG)
}

View file

@ -41,6 +41,12 @@ abstract class EditTextBottomSheetDialog: DialogFragment() {
}
}
fun didInitField() {
binding.editText.setSelection(binding.editText.text.length)
binding.editText.requestFocus()
inputMethodManager.showSoftInput(binding.editText, 0)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = EditTextBottomSheetDialogBinding.inflate(inflater, container, false)

View file

@ -55,10 +55,6 @@
name="handlers"
type="io.timelimit.android.ui.manage.device.manage.ManageDeviceFragmentHandlers" />
<variable
name="isEditingDeviceTitle"
type="Boolean" />
<import type="android.view.View" />
<import type="io.timelimit.android.integration.platform.RuntimePermissionStatus" />
<import type="io.timelimit.android.integration.platform.NewPermissionStatus" />
@ -99,9 +95,7 @@
android:layout_height="wrap_content">
<TextView
android:onClick="@{() -> handlers.startEditDeviceTitle()}"
android:visibility="@{safeUnbox(isEditingDeviceTitle) ? View.GONE : View.VISIBLE}"
android:id="@+id/current_device_title"
android:onClick="@{() -> handlers.editDeviceTitle()}"
android:focusable="true"
android:clickable="true"
android:background="?selectableItemBackground"
@ -112,28 +106,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:visibility="@{safeUnbox(isEditingDeviceTitle) ? View.VISIBLE : View.GONE}"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:inputType="text"
android:id="@+id/new_device_title_text"
android:layout_weight="1"
tools:text="Poor childs phone"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:onClick="@{() -> handlers.doEditDeviceTitle()}"
android:id="@+id/new_device_title_btn_ok"
android:background="?selectableItemBackground"
style="?android:buttonStyleSmall"
android:text="@string/generic_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:textAppearance="?android:textAppearanceMedium"
tools:text="Samsung Galaxy S3"

View file

@ -68,5 +68,6 @@
<string name="manage_device_downgrade_title">Downgrade</string>
<string name="manage_device_downgrade_text">Auf diesem Gerät wurde eine neuere Version von TimeLimit durch eine ältere ersetzt</string>
<string name="manage_device_rename">Gerät umbenennen</string>
<string name="manage_device_rename_toast_empty">Der Name für ein Gerät darf nicht leer sein</string>
</resources>

View file

@ -69,5 +69,6 @@
<string name="manage_device_downgrade_title">Downgrade</string>
<string name="manage_device_downgrade_text">On this device, a newer version of TimeLimit was replaced by an older one</string>
<string name="manage_device_rename">Rename device</string>
<string name="manage_device_rename_toast_empty">The name of an device must not be empty.</string>
</resources>