First commit: customized structure from Nextcloud SSO Example: https://github.com/matiasdelellis/app-tutorial-android

This commit is contained in:
Daniele Verducci (ZenPenguin)
2021-08-27 13:06:02 +02:00
commit cf7221eddc
86 changed files with 4131 additions and 0 deletions

View File

@ -0,0 +1,76 @@
/*
* Nextcloud Geofavorites for Android
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.activity.about;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import it.danieleverducci.nextcloudmaps.BuildConfig;
import it.danieleverducci.nextcloudmaps.R;
public class AboutActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
}
fillAboutActivity();
}
private void fillAboutActivity () {
TextView tvVersion = findViewById(R.id.about_version);
tvVersion.setText(Html.fromHtml(getString(R.string.about_version, "v" + BuildConfig.VERSION_NAME)));
Button btLicence = findViewById(R.id.about_app_license_button);
btLicence.setOnClickListener(view -> openUtl(getString(R.string.url_license)));
TextView tvSource = findViewById(R.id.about_source);
tvSource.setText(Html.fromHtml(getString(R.string.about_source, getString(R.string.url_source))));
tvSource.setOnClickListener(view -> openUtl(getString(R.string.url_source)));
TextView tvIssues = findViewById(R.id.about_issues);
tvIssues.setText(Html.fromHtml(getString(R.string.about_issues, getString(R.string.url_issues))));
tvIssues.setOnClickListener(view -> openUtl(getString(R.string.url_issues)));
}
@Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
private void openUtl(String url) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}

View File

@ -0,0 +1,125 @@
/*
* Nextcloud Geofavorites for Android
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.activity.login;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.api.NextcloudAPI;
import com.nextcloud.android.sso.exceptions.AccountImportCancelledException;
import com.nextcloud.android.sso.exceptions.AndroidGetAccountsPermissionNotGranted;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotInstalledException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import com.nextcloud.android.sso.ui.UiExceptionManager;
import it.danieleverducci.nextcloudmaps.R;
import it.danieleverducci.nextcloudmaps.activity.main.MainActivity;
import it.danieleverducci.nextcloudmaps.api.ApiProvider;
public class LoginActivity extends AppCompatActivity {
protected ApiProvider mApi;
protected ProgressBar progress;
protected Button button;
protected SingleSignOnAccount ssoAccount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
progress = findViewById(R.id.progress);
button = findViewById(R.id.chose_button);
button.setOnClickListener(view -> {
progress.setVisibility(View.VISIBLE);
openAccountChooser();
});
try {
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(getApplicationContext());
SingleAccountHelper.setCurrentAccount(getApplicationContext(), ssoAccount.name);
accountAccessDone();
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
}
}
private void openAccountChooser() {
try {
AccountImporter.pickNewAccount(this);
} catch (NextcloudFilesAppNotInstalledException | AndroidGetAccountsPermissionNotGranted e) {
UiExceptionManager.showDialogForException(this, e);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
AccountImporter.onActivityResult(requestCode, resultCode, data, this, new AccountImporter.IAccountAccessGranted() {
NextcloudAPI.ApiConnectedListener callback = new NextcloudAPI.ApiConnectedListener() {
@Override
public void onConnected() {
// ignore this one… see 5)
}
@Override
public void onError(Exception ex) {
// TODO handle errors
}
};
@Override
public void accountAccessGranted(SingleSignOnAccount account) {
Context l_context = getApplicationContext();
SingleAccountHelper.setCurrentAccount(l_context, account.name);
accountAccessDone();
}
});
} catch (AccountImportCancelledException e) {
e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
AccountImporter.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
private void accountAccessDone() {
Context l_context = getApplicationContext();
mApi = new ApiProvider(l_context);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}

View File

@ -0,0 +1,296 @@
/*
* Nextcloud Maps Geofavorites for Android
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.activity.main;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import java.util.ArrayList;
import java.util.List;
import it.danieleverducci.nextcloudmaps.R;
import it.danieleverducci.nextcloudmaps.activity.about.AboutActivity;
import it.danieleverducci.nextcloudmaps.activity.login.LoginActivity;
import it.danieleverducci.nextcloudmaps.activity.main.NavigationAdapter.NavigationItem;
import it.danieleverducci.nextcloudmaps.activity.main.SortingOrderDialogFragment.OnSortingOrderListener;
import it.danieleverducci.nextcloudmaps.api.ApiProvider;
import it.danieleverducci.nextcloudmaps.model.Note;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static it.danieleverducci.nextcloudmaps.activity.main.NoteAdapter.*;
import static it.danieleverducci.nextcloudmaps.activity.main.NoteAdapter.SORT_BY_CREATED;
import static it.danieleverducci.nextcloudmaps.activity.main.NoteAdapter.SORT_BY_TITLE;
public class MainActivity extends AppCompatActivity implements MainView, OnSortingOrderListener {
private static final int INTENT_ADD = 100;
private static final int INTENT_EDIT = 200;
public static final String NAVIGATION_KEY_ADD_NOTE = "add";
public static final String NAVIGATION_KEY_SHOW_ABOUT = "about";
public static final String NAVIGATION_KEY_SWITCH_ACCOUNT = "switch_account";
private SharedPreferences preferences;
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private MaterialCardView homeToolbar;
private SearchView searchView;
private SwipeRefreshLayout swipeRefresh;
private RecyclerView recyclerView;
private StaggeredGridLayoutManager layoutManager;
private FloatingActionButton fab;
private MainPresenter presenter;
private NoteAdapter noteAdapter;
private ItemClickListener itemClickListener;
NavigationAdapter navigationCommonAdapter;
private ApiProvider mApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int sortRule = preferences.getInt(getString(R.string.setting_sort_by), SORT_BY_CREATED);
boolean gridViewEnabled = preferences.getBoolean(getString(R.string.setting_grid_view_enabled), true);
recyclerView = findViewById(R.id.recycler_view);
layoutManager = new StaggeredGridLayoutManager(gridViewEnabled ? 2 : 1, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
presenter = new MainPresenter(this);
itemClickListener = ((view, position) -> {
Note note = noteAdapter.get(position);
/*Intent intent = new Intent(this, EditorActivity.class);
intent.putExtra("note", note);
startActivityForResult(intent, INTENT_EDIT);*/
});
noteAdapter = new NoteAdapter(getApplicationContext(), itemClickListener);
recyclerView.setAdapter(noteAdapter);
noteAdapter.setSortRule(sortRule);
swipeRefresh = findViewById(R.id.swipe_refresh);
swipeRefresh.setOnRefreshListener(() -> presenter.getNotes());
fab = findViewById(R.id.add);
fab.setOnClickListener(view -> add_note());
toolbar = findViewById(R.id.toolbar);
homeToolbar = findViewById(R.id.home_toolbar);
searchView = findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
noteAdapter.getFilter().filter(query);
return false;
}
});
searchView.setOnCloseListener(() -> {
if (toolbar.getVisibility() == VISIBLE && TextUtils.isEmpty(searchView.getQuery())) {
updateToolbars(true);
return true;
}
return false;
});
setSupportActionBar(toolbar);
setupNavigationMenu();
homeToolbar.setOnClickListener(view -> updateToolbars(false));
AppCompatImageView sortButton = findViewById(R.id.sort_mode);
sortButton.setOnClickListener(view -> openSortingOrderDialogFragment(getSupportFragmentManager(), noteAdapter.getSortRule()));
drawerLayout = findViewById(R.id.drawerLayout);
AppCompatImageButton menuButton = findViewById(R.id.menu_button);
menuButton.setOnClickListener(view -> drawerLayout.openDrawer(GravityCompat.START));
AppCompatImageView viewButton = findViewById(R.id.view_mode);
viewButton.setOnClickListener(view -> {
boolean gridEnabled = layoutManager.getSpanCount() == 1;
onGridIconChosen(gridEnabled);
});
updateSortingIcon(sortRule);
updateGridIcon(gridViewEnabled);
mApi = new ApiProvider(getApplicationContext());
presenter.getNotes();
}
private void setupNavigationMenu() {
ArrayList<NavigationItem> navItems = new ArrayList<>();
navigationCommonAdapter = new NavigationAdapter(this, item -> {
switch (item.id) {
case NAVIGATION_KEY_ADD_NOTE:
add_note();
break;
case NAVIGATION_KEY_SHOW_ABOUT:
show_about();
break;
case NAVIGATION_KEY_SWITCH_ACCOUNT:
switch_account();
break;
}
});
navItems.add(new NavigationItem(NAVIGATION_KEY_ADD_NOTE, getString(R.string.new_geobookmark), R.drawable.ic_add));
navItems.add(new NavigationItem(NAVIGATION_KEY_SHOW_ABOUT, getString(R.string.about), R.drawable.ic_info_grey));
navItems.add(new NavigationItem(NAVIGATION_KEY_SWITCH_ACCOUNT, getString(R.string.switch_account), R.drawable.ic_logout_grey));
navigationCommonAdapter.setItems(navItems);
RecyclerView navigationMenuCommon = findViewById(R.id.navigationCommon);
navigationMenuCommon.setAdapter(navigationCommonAdapter);
}
private void updateToolbars(boolean disableSearch) {
homeToolbar.setVisibility(disableSearch ? VISIBLE : GONE);
toolbar.setVisibility(disableSearch ? GONE : VISIBLE);
if (disableSearch) {
searchView.setQuery(null, true);
}
searchView.setIconified(disableSearch);
}
private void openSortingOrderDialogFragment(FragmentManager supportFragmentManager, int sortOrder) {
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
SortingOrderDialogFragment.newInstance(sortOrder).show(fragmentTransaction, SortingOrderDialogFragment.SORTING_ORDER_FRAGMENT);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == INTENT_ADD && resultCode == RESULT_OK) {
presenter.getNotes();
} else if (requestCode == INTENT_EDIT && resultCode == RESULT_OK) {
presenter.getNotes();
}
}
private void add_note() {
/*startActivityForResult(
new Intent(this, EditorActivity.class), INTENT_ADD);
*/
}
private void show_about() {
startActivity(new Intent(this, AboutActivity.class));
}
private void switch_account() {
SingleAccountHelper.setCurrentAccount(this, null);
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
@Override
public void showLoading() {
swipeRefresh.setRefreshing(true);
}
@Override
public void hideLoading() {
swipeRefresh.setRefreshing(false);
}
@Override
public void onGetResult(List<Note> note_list) {
noteAdapter.setNoteList(note_list);
}
@Override
public void onErrorLoading(String message) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
@Override
public void onSortingOrderChosen(int sortSelection) {
noteAdapter.setSortRule(sortSelection);
updateSortingIcon(sortSelection);
preferences.edit().putInt(getString(R.string.setting_sort_by), sortSelection).apply();
}
public void updateSortingIcon(int sortSelection) {
AppCompatImageView sortButton = findViewById(R.id.sort_mode);
switch (sortSelection) {
case SORT_BY_TITLE:
sortButton.setImageResource(R.drawable.ic_alphabetical_asc);
break;
case SORT_BY_CREATED:
sortButton.setImageResource(R.drawable.ic_modification_asc);
break;
}
}
public void onGridIconChosen(boolean gridEnabled) {
layoutManager.setSpanCount(gridEnabled ? 2 : 1);
updateGridIcon(gridEnabled);
preferences.edit().putBoolean(getString(R.string.setting_grid_view_enabled), gridEnabled).apply();
}
public void updateGridIcon(boolean gridEnabled) {
AppCompatImageView viewButton = findViewById(R.id.view_mode);
viewButton.setImageResource(gridEnabled ? R.drawable.ic_view_list : R.drawable.ic_view_module);
}
}

View File

@ -0,0 +1,64 @@
/*
* Nextcloud Notes Tutorial for Android
*
* @copyright Copyright (c) 2020 John Doe <john@doe.com>
* @author John Doe <john@doe.com>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.activity.main;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
import it.danieleverducci.nextcloudmaps.api.ApiProvider;
import it.danieleverducci.nextcloudmaps.model.Note;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainPresenter {
private MainView view;
public MainPresenter(MainView view) {
this.view = view;
}
public void getNotes() {
view.showLoading();
Call<List<Note>> call = ApiProvider.getAPI().getNotes();
call.enqueue(new Callback<List<Note>>() {
@Override
public void onResponse(@NonNull Call<List<Note>> call, @NonNull Response<List<Note>> response) {
((AppCompatActivity) view).runOnUiThread(() -> {
view.hideLoading();
if (response.isSuccessful() && response.body() != null) {
view.onGetResult(response.body());
}
});
}
@Override
public void onFailure(@NonNull Call<List<Note>> call, @NonNull Throwable t) {
((AppCompatActivity) view).runOnUiThread(() -> {
view.hideLoading();
view.onErrorLoading(t.getLocalizedMessage());
});
}
});
}
}

View File

@ -0,0 +1,32 @@
/*
* Nextcloud Notes Tutorial for Android
*
* @copyright Copyright (c) 2020 John Doe <john@doe.com>
* @author John Doe <john@doe.com>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.activity.main;
import java.util.List;
import it.danieleverducci.nextcloudmaps.model.Note;
public interface MainView {
void showLoading();
void hideLoading();
void onGetResult(List<Note> notes);
void onErrorLoading(String message);
}

View File

@ -0,0 +1,134 @@
/*
* Nextcloud Notes Tutorial for Android
*
* @copyright Copyright (c) 2020 John Doe <john@doe.com>
* @author John Doe <john@doe.com>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.activity.main;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import it.danieleverducci.nextcloudmaps.R;
public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.ViewHolder> {
@NonNull
private final Context context;
public static class NavigationItem {
@NonNull
public String id;
@NonNull
public String label;
@DrawableRes
public int icon;
public NavigationItem(@NonNull String id, @NonNull String label, @DrawableRes int icon) {
this.id = id;
this.label = label;
this.icon = icon;
}
}
class ViewHolder extends RecyclerView.ViewHolder {
@NonNull
private final View view;
@NonNull
private final TextView name;
@NonNull
private final ImageView icon;
private NavigationItem currentItem;
ViewHolder(@NonNull View itemView, @NonNull final ClickListener clickListener) {
super(itemView);
view = itemView;
this.name = itemView.findViewById(R.id.navigationItemLabel);
this.icon = itemView.findViewById(R.id.navigationItemIcon);
icon.setOnClickListener(view -> clickListener.onItemClick(currentItem));
itemView.setOnClickListener(view -> clickListener.onItemClick(currentItem));
}
private void bind(@NonNull NavigationItem item) {
int color = view.getResources().getColor(R.color.accent);
currentItem = item;
name.setText(item.label);
name.setTextColor(color);
icon.setImageDrawable(DrawableCompat.wrap(icon.getResources().getDrawable(item.icon)));
icon.setColorFilter(color);
icon.setVisibility(View.VISIBLE);
}
}
public interface ClickListener {
void onItemClick(NavigationItem item);
}
@NonNull
private List<NavigationItem> items = new ArrayList<>();
private String selectedItem = null;
@NonNull
private final ClickListener clickListener;
public NavigationAdapter(@NonNull Context context, @NonNull ClickListener clickListener) {
this.context = context;
this.clickListener = clickListener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_navigation, parent, false);
return new ViewHolder(v, clickListener);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.bind(items.get(position));
}
@Override
public int getItemCount() {
return items.size();
}
public void setItems(@NonNull List<NavigationItem> items) {
this.items = items;
notifyDataSetChanged();
}
}

View File

@ -0,0 +1,180 @@
/*
* Nextcloud Notes Tutorial for Android
*
* @copyright Copyright (c) 2020 John Doe <john@doe.com>
* @author John Doe <john@doe.com>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.activity.main;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import it.danieleverducci.nextcloudmaps.R;
import it.danieleverducci.nextcloudmaps.model.Note;
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.RecyclerViewAdapter> implements Filterable {
public static final int SORT_BY_TITLE = 0;
public static final int SORT_BY_CREATED = 1;
private Context context;
private ItemClickListener itemClickListener;
private List<Note> noteList = new ArrayList<>();
private List<Note> noteListFiltered = new ArrayList<>();
private int sortRule = SORT_BY_CREATED;
public NoteAdapter(Context context, ItemClickListener itemClickListener) {
this.context = context;
this.itemClickListener = itemClickListener;
}
public void setNoteList(@NonNull List<Note> noteList) {
this.noteList = noteList;
this.noteListFiltered = new ArrayList<>(noteList);
performSort();
notifyDataSetChanged();
}
public Note get(int position) {
return noteListFiltered.get(position);
}
public int getSortRule() {
return sortRule;
}
public void setSortRule(int sortRule) {
this.sortRule = sortRule;
performSort();
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerViewAdapter onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_note, parent, false);
return new RecyclerViewAdapter(view, itemClickListener);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter holder, int position) {
Note note = noteListFiltered.get(position);
holder.tv_title.setText(Html.fromHtml(note.getTitle().trim()));
holder.tv_content.setText(note.getContent().trim());
holder.card_item.setCardBackgroundColor(context.getResources().getColor(R.color.defaultNoteColor));
}
@Override
public int getItemCount() {
return noteListFiltered.size();
}
@Override
public Filter getFilter() {
return filter;
}
Filter filter = new Filter() {
@Override
// Run on Background thread.
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults filterResults = new FilterResults();
List <Note> filteredNotes = new ArrayList<>();
if (charSequence.toString().isEmpty()) {
filteredNotes.addAll(noteList);
} else {
for (Note note: noteList) {
String query = charSequence.toString().toLowerCase();
if (note.getTitle().toLowerCase().contains(query)) {
filteredNotes.add(note);
} else if (note.getContent().toLowerCase().contains(query)) {
filteredNotes.add(note);
}
}
}
filterResults.values = filteredNotes;
return filterResults;
}
//Run on ui thread
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
noteListFiltered.clear();
noteListFiltered.addAll((Collection<? extends Note>) filterResults.values);
performSort();
notifyDataSetChanged();
}
};
class RecyclerViewAdapter extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView card_item;
TextView tv_title, tv_content;
ItemClickListener itemClickListener;
RecyclerViewAdapter(@NonNull View itemView, ItemClickListener itemClickListener) {
super(itemView);
card_item = itemView.findViewById(R.id.card_item);
tv_title = itemView.findViewById(R.id.title);
tv_content = itemView.findViewById(R.id.content);
this.itemClickListener = itemClickListener;
card_item.setOnClickListener(this);
tv_content.setOnClickListener(this);
}
@Override
public void onClick(View view) {
itemClickListener.onItemClick(view, getAdapterPosition());
}
}
private void performSort() {
if (sortRule == SORT_BY_TITLE) {
Collections.sort(noteListFiltered, Note.ByTitleAZ);
} else if (sortRule == SORT_BY_CREATED) {
Collections.sort(noteListFiltered, Note.ByLastCreated);
}
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}

View File

@ -0,0 +1,172 @@
/*
* Nextcloud Notes Tutorial for Android
*
* @copyright Copyright (c) 2020 John Doe <john@doe.com>
* @author John Doe <john@doe.com>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.activity.main;
import android.app.Dialog;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.fragment.app.DialogFragment;
import it.danieleverducci.nextcloudmaps.R;
/**
* Dialog to show and choose the sorting order for the file listing.
*/
public class SortingOrderDialogFragment extends DialogFragment {
private final static String TAG = SortingOrderDialogFragment.class.getSimpleName();
public static final String SORTING_ORDER_FRAGMENT = "SORTING_ORDER_FRAGMENT";
private static final String KEY_SORT_ORDER = "SORT_ORDER";
private View mView;
private View[] mTaggedViews;
private Button mCancel;
private int mCurrentSortOrder;
public static SortingOrderDialogFragment newInstance(int sortOrder) {
SortingOrderDialogFragment dialogFragment = new SortingOrderDialogFragment();
Bundle args = new Bundle();
args.putInt(KEY_SORT_ORDER, sortOrder);
dialogFragment.setArguments(args);
return dialogFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// keep the state of the fragment on configuration changes
setRetainInstance(true);
mView = null;
Bundle arguments = getArguments();
if (arguments == null) {
throw new IllegalArgumentException("Arguments may not be null");
}
mCurrentSortOrder = arguments.getInt(KEY_SORT_ORDER, NoteAdapter.SORT_BY_TITLE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.sorting_order_fragment, container, false);
setupDialogElements(mView);
setupListeners();
return mView;
}
/**
* find all relevant UI elements and set their values.
*
* @param view the parent view
*/
private void setupDialogElements(View view) {
mCancel = view.findViewById(R.id.cancel);
mTaggedViews = new View[4];
mTaggedViews[0] = view.findViewById(R.id.sortByTitleAscending);
mTaggedViews[0].setTag(NoteAdapter.SORT_BY_TITLE);
mTaggedViews[1] = view.findViewById(R.id.sortByTitleAscendingText);
mTaggedViews[1].setTag(NoteAdapter.SORT_BY_TITLE);
mTaggedViews[2] = view.findViewById(R.id.sortByCreationDateDescending);
mTaggedViews[2].setTag(NoteAdapter.SORT_BY_CREATED);
mTaggedViews[3] = view.findViewById(R.id.sortByCreationDateDescendingText);
mTaggedViews[3].setTag(NoteAdapter.SORT_BY_CREATED);
setupActiveOrderSelection();
}
/**
* tints the icon reflecting the actual sorting choice in the apps primary color.
*/
private void setupActiveOrderSelection() {
for (View view: mTaggedViews) {
if (mCurrentSortOrder != (int) view.getTag()) {
continue;
}
if (view instanceof ImageButton) {
Drawable normalDrawable = ((ImageButton) view).getDrawable();
Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
DrawableCompat.setTint(wrapDrawable, this.getResources().getColor(R.color.defaultNoteTint));
}
if (view instanceof TextView) {
((TextView)view).setTextColor(this.getResources().getColor(R.color.defaultNoteTint));
((TextView)view).setTypeface(Typeface.DEFAULT_BOLD);
}
}
}
/**
* setup all listeners.
*/
private void setupListeners() {
mCancel.setOnClickListener(view -> dismiss());
OnSortOrderClickListener sortOrderClickListener = new OnSortOrderClickListener();
for (View view : mTaggedViews) {
view.setOnClickListener(sortOrderClickListener);
}
}
@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super.onCreateDialog(savedInstanceState);
}
@Override
public void onDestroyView() {
if (getDialog() != null && getRetainInstance()) {
getDialog().setDismissMessage(null);
}
super.onDestroyView();
}
private class OnSortOrderClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
dismissAllowingStateLoss();
((SortingOrderDialogFragment.OnSortingOrderListener) getActivity())
.onSortingOrderChosen((int) v.getTag());
}
}
public interface OnSortingOrderListener {
void onSortingOrderChosen(int sortSelection);
}
}

View File

@ -0,0 +1,55 @@
/*
* Nextcloud Notes Tutorial for Android
*
* @copyright Copyright (c) 2020 John Doe <john@doe.com>
* @author John Doe <john@doe.com>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.api;
import java.util.List;
import it.danieleverducci.nextcloudmaps.model.Note;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;
public interface API {
String mApiEndpoint = "/index.php/apps/notestutorial/api/0.1";
@GET("/notes")
Call<List<Note>> getNotes();
@POST("/notes")
Call<Note> create(
@Body Note note
);
@PUT("/notes/{id}")
Call<Note> updateNote(
@Path("id") int id,
@Body Note note
);
@DELETE("/notes/{id}")
Call<Note> deleteNote(
@Path("id") int id
);
}

View File

@ -0,0 +1,81 @@
/*
* Nextcloud Notes Tutorial for Android
*
* @copyright Copyright (c) 2020 John Doe <john@doe.com>
* @author John Doe <john@doe.com>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.api;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.gson.GsonBuilder;
import com.nextcloud.android.sso.api.NextcloudAPI;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import retrofit2.NextcloudRetrofitApiBuilder;
public class ApiProvider {
private final String TAG = ApiProvider.class.getCanonicalName();
@NonNull
protected Context context;
protected static API mApi;
protected static String ssoAccountName;
public ApiProvider(Context context) {
this.context = context;
initSsoApi(new NextcloudAPI.ApiConnectedListener() {
@Override
public void onConnected() {
// Ignore..
}
@Override
public void onError(Exception ex) {
// Ignore...
}
});
}
public void initSsoApi(final NextcloudAPI.ApiConnectedListener callback) {
try {
SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
NextcloudAPI nextcloudAPI = new NextcloudAPI(context, ssoAccount, new GsonBuilder().create(), callback);
ssoAccountName = ssoAccount.name;
mApi = new NextcloudRetrofitApiBuilder(nextcloudAPI, API.mApiEndpoint).create(API.class);
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
Log.d(TAG, "setAccout() called with: ex = [" + e + "]");
}
}
public static API getAPI() {
return mApi;
}
public static String getAccountName() {
return ssoAccountName;
}
}

View File

@ -0,0 +1,67 @@
/*
* Nextcloud Notes Tutorial for Android
*
* @copyright Copyright (c) 2020 John Doe <john@doe.com>
* @author John Doe <john@doe.com>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.Comparator;
public class Note implements Serializable {
@Expose
@SerializedName("id") private int id;
@Expose
@SerializedName("title") private String title;
@Expose
@SerializedName("content") private String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public static Comparator<Note> ByTitleAZ = (note, t1) -> note.title.compareTo(t1.title);
public static Comparator<Note> ByLastCreated = (note, t1) -> t1.id - note.id;
}

View File

@ -0,0 +1,38 @@
/*
* Nextcloud Notes Tutorial for Android
*
* @copyright Copyright (c) 2020 John Doe <john@doe.com>
* @author John Doe <john@doe.com>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.nextcloudmaps.util;
import android.graphics.drawable.Drawable;
import android.view.MenuItem;
import androidx.annotation.ColorInt;
import androidx.core.graphics.drawable.DrawableCompat;
public class ColorUtil {
public static void menuItemTintColor(MenuItem item, @ColorInt int color) {
Drawable normalDrawable = item.getIcon();
Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
DrawableCompat.setTint(wrapDrawable, color);
item.setIcon(wrapDrawable);
}
}