|
1 | | -/* |
2 | | - * Nextcloud - Android Client |
3 | | - * |
4 | | - * SPDX-FileCopyrightText: 2017 Tobias Kaminsky <tobias@kaminsky.me> |
5 | | - * SPDX-FileCopyrightText: 2017 Nextcloud GmbH |
6 | | - * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only |
7 | | - */ |
8 | | -package com.owncloud.android.ui; |
9 | | - |
10 | | -import android.annotation.SuppressLint; |
11 | | -import android.content.Context; |
12 | | -import android.preference.SwitchPreference; |
13 | | -import android.util.AttributeSet; |
14 | | -import android.view.View; |
15 | | -import android.view.ViewGroup; |
16 | | -import android.widget.Switch; |
17 | | - |
18 | | -import com.owncloud.android.MainApp; |
19 | | -import com.owncloud.android.utils.theme.ViewThemeUtils; |
20 | | - |
21 | | -import javax.inject.Inject; |
22 | | - |
23 | | -/** |
24 | | - * Themeable switch preference TODO Migrate to androidx |
25 | | - */ |
26 | | -public class ThemeableSwitchPreference extends SwitchPreference { |
| 1 | +package com.owncloud.android.ui |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.preference.SwitchPreference |
| 5 | +import android.util.AttributeSet |
| 6 | +import android.view.View |
| 7 | +import com.google.android.material.materialswitch.MaterialSwitch |
| 8 | +import com.owncloud.android.MainApp |
| 9 | +import com.owncloud.android.R |
| 10 | +import com.owncloud.android.utils.theme.ViewThemeUtils |
| 11 | +import javax.inject.Inject |
| 12 | + |
| 13 | +@Suppress("DEPRECATION") |
| 14 | +class ThemeableSwitchPreference : SwitchPreference { |
27 | 15 | @Inject |
28 | | - ViewThemeUtils viewThemeUtils; |
| 16 | + lateinit var viewThemeUtils: ViewThemeUtils |
29 | 17 |
|
30 | | - public ThemeableSwitchPreference(Context context) { |
31 | | - super(context); |
32 | | - MainApp.getAppComponent().inject(this); |
| 18 | + /** |
| 19 | + * Do not delete constructor. These are used. |
| 20 | + */ |
| 21 | + constructor(context: Context) : super(context) { |
| 22 | + init() |
33 | 23 | } |
34 | 24 |
|
35 | | - public ThemeableSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
36 | | - super(context, attrs, defStyleAttr); |
37 | | - MainApp.getAppComponent().inject(this); |
| 25 | + constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) { |
| 26 | + init() |
38 | 27 | } |
39 | 28 |
|
40 | | - public ThemeableSwitchPreference(Context context, AttributeSet attrs) { |
41 | | - super(context, attrs); |
42 | | - MainApp.getAppComponent().inject(this); |
| 29 | + constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { |
| 30 | + init() |
43 | 31 | } |
44 | 32 |
|
45 | | - @Override |
46 | | - protected void onBindView(View view) { |
47 | | - super.onBindView(view); |
48 | | - |
49 | | - if (view instanceof ViewGroup) { |
50 | | - findSwitch((ViewGroup) view); |
51 | | - } |
| 33 | + private fun init() { |
| 34 | + MainApp.getAppComponent().inject(this) |
| 35 | + setWidgetLayoutResource(R.layout.themeable_switch) |
52 | 36 | } |
53 | 37 |
|
54 | | - private void findSwitch(ViewGroup viewGroup) { |
55 | | - for (int i = 0; i < viewGroup.getChildCount(); i++) { |
56 | | - View child = viewGroup.getChildAt(i); |
57 | | - |
58 | | - if (child instanceof @SuppressLint("UseSwitchCompatOrMaterialCode") Switch switchView) { |
59 | | - viewThemeUtils.platform.colorSwitch(switchView); |
60 | | - |
61 | | - break; |
62 | | - } else if (child instanceof ViewGroup) { |
63 | | - findSwitch((ViewGroup) child); |
64 | | - } |
| 38 | + @Deprecated("Deprecated in Java") |
| 39 | + override fun onBindView(view: View) { |
| 40 | + super.onBindView(view) |
| 41 | + val checkable = view.findViewById<View>(R.id.switch_widget) |
| 42 | + if (checkable is MaterialSwitch) { |
| 43 | + checkable.setChecked(isChecked) |
| 44 | + viewThemeUtils.material.colorMaterialSwitch(checkable) |
65 | 45 | } |
66 | 46 | } |
67 | 47 | } |
0 commit comments