Skip to content

Commit 0db720c

Browse files
alperozturk96backportbot[bot]
authored andcommitted
get note in background thread
Signed-off-by: alperozturk <[email protected]>
1 parent 6b81476 commit 0db720c

File tree

4 files changed

+129
-64
lines changed

4 files changed

+129
-64
lines changed

app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedFragment.kt

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,85 @@
44
* SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors
55
* SPDX-License-Identifier: GPL-3.0-or-later
66
*/
7-
package it.niedermann.owncloud.notes.branding;
7+
package it.niedermann.owncloud.notes.branding
88

9-
import android.os.Bundle;
10-
import android.util.TypedValue;
11-
import android.view.Menu;
12-
import android.view.MenuInflater;
13-
14-
import androidx.annotation.ColorInt;
15-
import androidx.annotation.NonNull;
16-
import androidx.annotation.Nullable;
17-
import androidx.appcompat.app.AppCompatActivity;
18-
import androidx.fragment.app.Fragment;
19-
20-
import com.nextcloud.android.common.ui.util.extensions.AppCompatActivityExtensionsKt;
21-
22-
public abstract class BrandedFragment extends Fragment implements Branded {
9+
import android.os.Bundle
10+
import android.util.TypedValue
11+
import android.view.Menu
12+
import android.view.MenuInflater
13+
import androidx.annotation.ColorInt
14+
import androidx.appcompat.app.AppCompatActivity
15+
import androidx.core.view.forEach
16+
import androidx.fragment.app.Fragment
17+
import androidx.lifecycle.lifecycleScope
18+
import com.nextcloud.android.common.ui.util.extensions.adjustUIForAPILevel35
19+
import kotlinx.coroutines.Dispatchers
20+
import kotlinx.coroutines.launch
2321

22+
abstract class BrandedFragment : Fragment(), Branded {
23+
@JvmField
2424
@ColorInt
25-
protected int colorAccent;
25+
protected var colorAccent: Int = 0
26+
27+
@JvmField
2628
@ColorInt
27-
protected int colorPrimary;
29+
protected var colorPrimary: Int = 0
2830

29-
@Override
30-
public void onCreate(@Nullable Bundle savedInstanceState) {
31-
if (getActivity() instanceof AppCompatActivity appCompatActivity) {
32-
AppCompatActivityExtensionsKt.adjustUIForAPILevel35(appCompatActivity);
31+
override fun onCreate(savedInstanceState: Bundle?) {
32+
if (activity is AppCompatActivity) {
33+
val appCompatActivity = activity as AppCompatActivity
34+
appCompatActivity.adjustUIForAPILevel35()
3335
}
34-
super.onCreate(savedInstanceState);
36+
super.onCreate(savedInstanceState)
3537
}
3638

37-
@Override
38-
public void onStart() {
39-
super.onStart();
39+
override fun onStart() {
40+
super.onStart()
41+
42+
val context = requireContext()
43+
val typedValue = TypedValue()
44+
45+
context.theme.resolveAttribute(
46+
com.google.android.material.R.attr.colorAccent,
47+
typedValue,
48+
true
49+
)
50+
colorAccent = typedValue.data
4051

41-
final var context = requireContext();
42-
final var typedValue = new TypedValue();
43-
context.getTheme().resolveAttribute(com.google.android.material.R.attr.colorAccent, typedValue, true);
44-
colorAccent = typedValue.data;
45-
context.getTheme().resolveAttribute(com.google.android.material.R.attr.colorPrimary, typedValue, true);
46-
colorPrimary = typedValue.data;
52+
context.theme.resolveAttribute(
53+
com.google.android.material.R.attr.colorPrimary,
54+
typedValue,
55+
true
56+
)
57+
colorPrimary = typedValue.data
4758

48-
@ColorInt final int color = BrandingUtil.readBrandMainColor(context);
49-
applyBrand(color);
59+
@ColorInt
60+
val color = BrandingUtil.readBrandMainColor(context)
61+
applyBrand(color)
5062
}
5163

52-
@Override
53-
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
54-
super.onCreateOptionsMenu(menu, inflater);
55-
final var utils = BrandingUtil.of(colorAccent, requireContext());
64+
@Suppress("DEPRECATION")
65+
@Deprecated("Deprecated in Java")
66+
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
67+
super.onCreateOptionsMenu(menu, inflater)
68+
val utils = BrandingUtil.of(colorAccent, requireContext())
5669

57-
for (int i = 0; i < menu.size(); i++) {
58-
if (menu.getItem(i).getIcon() != null) {
59-
utils.platform.colorToolbarMenuIcon(requireContext(), menu.getItem(i));
70+
menu.forEach { menu ->
71+
menu.icon?.let { icon ->
72+
utils.platform.colorToolbarMenuIcon(requireContext(), menu)
6073
}
6174
}
6275
}
76+
77+
fun lifecycleScopeIOJob(block: () -> Unit) {
78+
lifecycleScope.launch(Dispatchers.IO) {
79+
block()
80+
}
81+
}
82+
83+
fun onMainThread(block: () -> Unit) {
84+
activity?.runOnUiThread {
85+
block()
86+
}
87+
}
6388
}

app/src/main/java/it/niedermann/owncloud/notes/edit/NoteDirectEditFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class NoteDirectEditFragment : BaseNoteFragment(), Branded {
193193
Log.d(TAG, "loadNoteInWebView() called")
194194

195195
context?.let { context ->
196-
val repository = DirectEditingRepository.getInstance(context)
196+
val repository = DirectEditingRepository.getInstance(context.applicationContext)
197197
val urlDisposable = repository.getDirectEditingUrl(account, note)
198198
.observeOn(AndroidSchedulers.mainThread()).subscribe({ url ->
199199
url?.let {

app/src/main/java/it/niedermann/owncloud/notes/edit/NoteEditFragment.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import it.niedermann.owncloud.notes.persistence.entity.Note;
4141
import it.niedermann.owncloud.notes.shared.model.ISyncCallback;
4242
import it.niedermann.owncloud.notes.shared.util.DisplayUtils;
43+
import kotlin.Unit;
44+
import kotlin.jvm.functions.Function0;
4345

4446
public class NoteEditFragment extends SearchableBaseNoteFragment {
4547

@@ -180,22 +182,35 @@ public void onResume() {
180182
@Override
181183
protected void onNoteLoaded(Note note) {
182184
super.onNoteLoaded(note);
185+
if (binding == null) {
186+
return;
187+
}
188+
183189
if (TextUtils.isEmpty(note.getContent())) {
184190
openSoftKeyboard();
185191
}
186192

187-
binding.editContent.setMarkdownString(note.getContent());
188-
binding.editContent.setEnabled(true);
193+
lifecycleScopeIOJob(() -> {
194+
// load potential big note on IO Dispatchers
195+
final String content = note.getContent();
196+
final var sp = PreferenceManager.getDefaultSharedPreferences(requireContext().getApplicationContext());
189197

190-
final var sp = PreferenceManager.getDefaultSharedPreferences(requireContext().getApplicationContext());
191-
binding.editContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, getFontSizeFromPreferences(requireContext(), sp));
192-
if (sp.getBoolean(getString(R.string.pref_key_font), false)) {
193-
binding.editContent.setTypeface(Typeface.MONOSPACE);
194-
}
198+
onMainThread(() -> {
199+
binding.editContent.setMarkdownString(content);
200+
binding.editContent.setEnabled(true);
201+
binding.editContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, getFontSizeFromPreferences(requireContext(), sp));
195202

196-
if (lastSelection > 0 && binding.editContent.length() >= lastSelection) {
197-
binding.editContent.setSelection(lastSelection);
198-
}
203+
if (sp.getBoolean(getString(R.string.pref_key_font), false)) {
204+
binding.editContent.setTypeface(Typeface.MONOSPACE);
205+
}
206+
207+
if (lastSelection > 0 && binding.editContent.length() >= lastSelection) {
208+
binding.editContent.setSelection(lastSelection);
209+
}
210+
return Unit.INSTANCE;
211+
});
212+
return Unit.INSTANCE;
213+
});
199214
}
200215

201216
private void openSoftKeyboard() {

app/src/main/java/it/niedermann/owncloud/notes/edit/NotePreviewFragment.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@
3434
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
3535
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
3636
import com.nextcloud.android.sso.helper.SingleAccountHelper;
37+
import com.owncloud.android.lib.common.utils.Log_OC;
3738

3839
import it.niedermann.owncloud.notes.R;
3940
import it.niedermann.owncloud.notes.branding.BrandingUtil;
4041
import it.niedermann.owncloud.notes.databinding.FragmentNotePreviewBinding;
4142
import it.niedermann.owncloud.notes.persistence.entity.Note;
43+
import it.niedermann.owncloud.notes.shared.model.ISyncCallback;
4244
import it.niedermann.owncloud.notes.shared.util.SSOUtil;
45+
import kotlin.Unit;
46+
import kotlin.jvm.functions.Function0;
4347

4448
public class NotePreviewFragment extends SearchableBaseNoteFragment implements OnRefreshListener {
4549

@@ -135,11 +139,26 @@ protected void onNoteLoaded(Note note) {
135139
super.onNoteLoaded(note);
136140
noteLoaded = true;
137141
registerInternalNoteLinkHandler();
138-
changedText = note.getContent();
139-
binding.singleNoteContent.setMarkdownString(note.getContent(), setScrollY);
140-
binding.singleNoteContent.getMarkdownString().observe(requireActivity(), (newContent) -> {
141-
changedText = newContent.toString();
142-
saveNote(null);
142+
143+
lifecycleScopeIOJob(() -> {
144+
final String content = note.getContent();
145+
changedText = content;
146+
147+
onMainThread(() -> {
148+
binding.singleNoteContent.setMarkdownString(content, setScrollY);
149+
150+
final var activity = getActivity();
151+
if (activity == null) {
152+
return Unit.INSTANCE;
153+
}
154+
155+
binding.singleNoteContent.getMarkdownString().observe(activity, (newContent) -> {
156+
changedText = newContent.toString();
157+
saveNote(null);
158+
});
159+
return Unit.INSTANCE;
160+
});
161+
return Unit.INSTANCE;
143162
});
144163
}
145164

@@ -176,21 +195,27 @@ protected String getContent() {
176195
public void onRefresh() {
177196
if (noteLoaded && repo.isSyncPossible() && SSOUtil.isConfigured(getContext())) {
178197
binding.swiperefreshlayout.setRefreshing(true);
179-
executor.submit(() -> {
198+
lifecycleScopeIOJob(() -> {
180199
try {
181200
final var account = repo.getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(requireContext()).name);
182-
repo.addCallbackPull(account, () -> executor.submit(() -> {
201+
202+
repo.addCallbackPull(account, () -> {
183203
note = repo.getNoteById(note.getId());
184-
changedText = note.getContent();
185-
requireActivity().runOnUiThread(() -> {
186-
binding.singleNoteContent.setMarkdownString(note.getContent());
204+
final String content = note.getContent();
205+
changedText = content;
206+
207+
onMainThread(() -> {
208+
binding.singleNoteContent.setMarkdownString(content);
187209
binding.swiperefreshlayout.setRefreshing(false);
210+
return Unit.INSTANCE;
188211
});
189-
}));
212+
});
213+
190214
repo.scheduleSync(account, false);
191-
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
192-
e.printStackTrace();
215+
} catch (Exception e) {
216+
Log_OC.e(TAG, "onRefresh exception: " + e);
193217
}
218+
return Unit.INSTANCE;
194219
});
195220
} else {
196221
binding.swiperefreshlayout.setRefreshing(false);

0 commit comments

Comments
 (0)