Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NoteListWidgetConfigurationActivity.java « notelist « widget « notes « owncloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3d7122dad6a291017c0a775fa686d8750a561338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package it.niedermann.owncloud.notes.widget.notelist;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import it.niedermann.owncloud.notes.LockedActivity;
import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ActivityNoteListConfigurationBinding;
import it.niedermann.owncloud.notes.main.navigation.NavigationAdapter;
import it.niedermann.owncloud.notes.main.navigation.NavigationClickListener;
import it.niedermann.owncloud.notes.main.navigation.NavigationItem;
import it.niedermann.owncloud.notes.persistence.NotesRepository;
import it.niedermann.owncloud.notes.persistence.entity.Account;
import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData;

import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_ALL;
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_CATEGORY;
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_STARRED;
import static it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType.RECENT;


public class NoteListWidgetConfigurationActivity extends LockedActivity {
    private static final String TAG = Activity.class.getSimpleName();

    private final ExecutorService executor = Executors.newCachedThreadPool();

    private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    private Account localAccount = null;

    private ActivityNoteListConfigurationBinding binding;
    private NoteListViewModel viewModel;
    private NavigationAdapter adapterCategories;
    private NotesRepository repo = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setResult(RESULT_CANCELED);

        repo = NotesRepository.getInstance(this);
        final Bundle extras = getIntent().getExtras();

        if (extras != null) {
            appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            Log.d(TAG, "INVALID_APPWIDGET_ID");
            finish();
        }

        viewModel = new ViewModelProvider(this).get(NoteListViewModel.class);
        binding = ActivityNoteListConfigurationBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        adapterCategories = new NavigationAdapter(this, new NavigationClickListener() {
            @Override
            public void onItemClick(NavigationItem item) {
                NotesListWidgetData data = new NotesListWidgetData();

                data.setId(appWidgetId);
                if (item.type != null) {
                    switch (item.type) {
                        case RECENT: {
                            data.setMode(MODE_DISPLAY_ALL);
                            break;
                        }
                        case FAVORITES: {
                            data.setMode(MODE_DISPLAY_STARRED);
                            break;
                        }
                        case UNCATEGORIZED: {
                            data.setMode(MODE_DISPLAY_CATEGORY);
                            data.setCategory(null);
                        }
                        case DEFAULT_CATEGORY:
                        default: {
                            if (item.getClass() == NavigationItem.CategoryNavigationItem.class) {
                                data.setMode(MODE_DISPLAY_CATEGORY);
                                data.setCategory(((NavigationItem.CategoryNavigationItem) item).category);
                            } else {
                                data.setMode(MODE_DISPLAY_ALL);
                                Log.e(TAG, "Unknown item navigation type. Fallback to show " + RECENT);
                            }
                        }
                    }
                } else {
                    data.setMode(MODE_DISPLAY_ALL);
                    Log.e(TAG, "Unknown item navigation type. Fallback to show " + RECENT);
                }

                data.setAccountId(localAccount.getId());
                data.setThemeMode(NotesApplication.getAppTheme(getApplicationContext()).getModeId());

                executor.submit(() -> {
                    repo.createOrUpdateNoteListWidgetData(data);

                    final Intent updateIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, getApplicationContext(), NoteListWidget.class)
                            .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                    setResult(RESULT_OK, updateIntent);
                    getApplicationContext().sendBroadcast(updateIntent);
                    finish();
                });
            }

            public void onIconClick(NavigationItem item) {
                onItemClick(item);
            }
        });

        binding.recyclerView.setAdapter(adapterCategories);

        executor.submit(() -> {
            try {
                this.localAccount = repo.getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(this).name);
            } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
                e.printStackTrace();
                Toast.makeText(this, R.string.widget_not_logged_in, Toast.LENGTH_LONG).show();
                // TODO Present user with app login screen
                Log.w(TAG, "onCreate: user not logged in");
                finish();
            }
            runOnUiThread(() -> viewModel.getAdapterCategories(localAccount.getId()).observe(this, (navigationItems) -> adapterCategories.setItems(navigationItems)));
        });
    }

    @Override
    public void applyBrand(int mainColor, int textColor) {
    }
}