diff options
author | Niedermann IT-Dienstleistungen <info@niedermann.it> | 2018-11-04 12:53:37 +0300 |
---|---|---|
committer | Niedermann IT-Dienstleistungen <info@niedermann.it> | 2018-11-04 12:53:37 +0300 |
commit | 4e76c7a8678c5649bdd96e15f3e2637fa6c0938d (patch) | |
tree | 4d10e1df363eed1e8d8bfcb4bf68d24be89fdc34 | |
parent | 370ba841793946015812b2055a57f64e07da8cb9 (diff) |
Use real IDs for fetching stacks and cards instead of "0"-mock
-rw-r--r-- | app/src/main/AndroidManifest.xml | 2 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java (renamed from app/src/main/java/it/niedermann/nextcloud/deck/ui/board/MainActivity.java) | 85 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/board/BoardAdapter.java | 57 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardFragment.java | 17 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/login/LoginDialogFragment.java | 7 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackAdapter.java | 5 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java | 36 | ||||
-rw-r--r-- | app/src/main/res/layout/activity_main.xml | 18 | ||||
-rw-r--r-- | app/src/main/res/layout/fragment_board_item.xml | 13 | ||||
-rw-r--r-- | app/src/main/res/layout/fragment_card.xml | 8 |
10 files changed, 78 insertions, 170 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8bfd89152..c9d277170 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,7 +13,7 @@ android:theme="@style/AppTheme"> <activity - android:name=".ui.board.MainActivity" + android:name=".ui.MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/MainActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java index 84c6659bd..32e0fc8ac 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/MainActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java @@ -1,6 +1,7 @@ -package it.niedermann.nextcloud.deck.ui.board; +package it.niedermann.nextcloud.deck.ui; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; @@ -10,9 +11,8 @@ import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -41,15 +41,11 @@ public class MainActivity extends AppCompatActivity @BindView(R.id.fab) FloatingActionButton fab; @BindView(R.id.drawer_layout) DrawerLayout drawer; @BindView(R.id.nav_view) NavigationView navigationView; - @BindView(R.id.recycler_view) RecyclerView recyclerView; - @BindView(R.id.stackLayout) - TabLayout stackLayout; - @BindView(R.id.viewPager) - ViewPager viewPager; - private StackAdapter stackAdapter; + @BindView(R.id.stackLayout) TabLayout stackLayout; + @BindView(R.id.viewPager) ViewPager viewPager; + private StackAdapter stackAdapter; private LoginDialogFragment loginDialogFragment; - private BoardAdapter adapter = null; private SyncManager syncManager; @Override @@ -61,7 +57,7 @@ public class MainActivity extends AppCompatActivity fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Snackbar.make(view, "Creating new Boards is not yet supported", Snackbar.LENGTH_LONG).show(); + Snackbar.make(view, "Creating new Cards is not yet supported", Snackbar.LENGTH_LONG).show(); } }); @@ -71,12 +67,9 @@ public class MainActivity extends AppCompatActivity toggle.syncState(); navigationView.setNavigationItemSelectedListener(this); - - initRecyclerView(); - + syncManager = new SyncManager(getApplicationContext(), this); stackAdapter = new StackAdapter(getSupportFragmentManager()); - syncManager = new SyncManager(getApplicationContext(), this); if(this.syncManager.hasAccounts()) { Account account = syncManager.readAccounts().get(0); String accountName = account.getName(); @@ -85,49 +78,48 @@ public class MainActivity extends AppCompatActivity syncManager.getBoards(account.getId(), new IResponseCallback<List<Board>>() { @Override public void onResponse(List<Board> boards) { - adapter.setBoardList(boards); - } - - @Override - public void onError(Throwable throwable) { - throwable.printStackTrace(); - } - }); - syncManager.getStacks(0, 0, new IResponseCallback<List<Stack>>() { - @Override - public void onResponse(List<Stack> response) { - for(Stack stack: response) { - stackAdapter.addFragment(new StackFragment(), stack.getTitle()); + Menu menu = navigationView.getMenu(); + for(Board board: boards) { + // ToDo at least use https://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java + menu.add(Menu.NONE, (int) board.getId(), Menu.NONE, board.getTitle()); } - viewPager.setAdapter(stackAdapter); - stackLayout.setupWithViewPager(viewPager); } @Override public void onError(Throwable throwable) { - + throwable.printStackTrace(); } }); + displayStacksForId(0); } else { loginDialogFragment = new LoginDialogFragment(); loginDialogFragment.show(this.getSupportFragmentManager(), "NoticeDialogFragment"); } } - @Override - protected void onResume() { - super.onResume(); - } - public void onAccountChoose(SingleSignOnAccount account) { getSupportFragmentManager().beginTransaction().remove(loginDialogFragment).commit(); this.syncManager.createAccount(account.name); } - public void initRecyclerView() { - adapter = new BoardAdapter(); - recyclerView.setLayoutManager(new LinearLayoutManager(this)); - recyclerView.setAdapter(adapter); + private void displayStacksForId(long id) { + syncManager.getStacks(0, id, new IResponseCallback<List<Stack>>() { + @Override + public void onResponse(List<Stack> response) { + stackAdapter.clear(); + for(Stack stack: response) { + stackAdapter.addFragment(StackFragment.newInstance(stack.getId()), stack.getTitle()); + } + viewPager.setAdapter(stackAdapter); + stackLayout.setupWithViewPager(viewPager); + } + + @Override + public void onError(Throwable throwable) { + Log.e("Deck", throwable.getMessage()); + throwable.printStackTrace(); + } + }); } @Override @@ -148,25 +140,16 @@ public class MainActivity extends AppCompatActivity @Override public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); - - //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } - return super.onOptionsItemSelected(item); } - @SuppressWarnings("StatementWithEmptyBody") @Override - public boolean onNavigationItemSelected(MenuItem item) { - // Handle navigation view item clicks here. - int id = item.getItemId(); - + public boolean onNavigationItemSelected(@NonNull MenuItem item) { + displayStacksForId(item.getItemId()); drawer.closeDrawer(GravityCompat.START); return true; } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/BoardAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/BoardAdapter.java deleted file mode 100644 index 0a0ecf989..000000000 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/BoardAdapter.java +++ /dev/null @@ -1,57 +0,0 @@ -package it.niedermann.nextcloud.deck.ui.board; - -import android.support.annotation.NonNull; -import android.support.v7.widget.RecyclerView; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -import java.util.ArrayList; -import java.util.List; - -import butterknife.BindView; -import butterknife.ButterKnife; -import it.niedermann.nextcloud.deck.R; -import it.niedermann.nextcloud.deck.model.Board; - -class BoardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { - - - private List<Board> boardList = new ArrayList<>(); - - @NonNull - @Override - public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) { - View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_board_item, viewGroup, false); - return new BoardViewHolder(v); - } - - @Override - public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { - Board board = boardList.get(position); - ((BoardViewHolder) viewHolder).boardTitle.setText(board.getTitle()); - } - - @Override - public int getItemCount() { - return boardList.size(); - } - - void setBoardList(@NonNull List<Board> boardList) { - this.boardList = boardList; - notifyDataSetChanged(); - } - - - - static class BoardViewHolder extends RecyclerView.ViewHolder { - @BindView(R.id.card_title) - TextView boardTitle; - - private BoardViewHolder(View view) { - super(view); - ButterKnife.bind(this, view); - } - } -} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardFragment.java deleted file mode 100644 index 8f7736810..000000000 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardFragment.java +++ /dev/null @@ -1,17 +0,0 @@ -package it.niedermann.nextcloud.deck.ui.card; - -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import it.niedermann.nextcloud.deck.R; - -public class CardFragment extends Fragment { - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(R.layout.fragment_card, container, false); - } -}
\ No newline at end of file diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/login/LoginDialogFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/login/LoginDialogFragment.java index 90048fbcd..b3aee31eb 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/login/LoginDialogFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/login/LoginDialogFragment.java @@ -12,18 +12,17 @@ import com.nextcloud.android.sso.AccountImporter; import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotInstalledException; import com.nextcloud.android.sso.model.SingleSignOnAccount; -import it.niedermann.nextcloud.deck.ui.board.MainActivity; +import it.niedermann.nextcloud.deck.ui.MainActivity; public class LoginDialogFragment extends DialogFragment { @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { - try { AccountImporter.pickNewAccount(this); } catch (NextcloudFilesAppNotInstalledException e) { - Log.d("Deck", "============================================================="); - Log.d("Deck", "Nextcloud app is not installed. Cannot choose account"); + Log.w("Deck", "============================================================="); + Log.w("Deck", "Nextcloud app is not installed. Cannot choose account"); // ToDo redirect to Play Store e.printStackTrace(); } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackAdapter.java index b1bf16949..bd686d5eb 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackAdapter.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackAdapter.java @@ -26,6 +26,11 @@ public class StackAdapter extends FragmentStatePagerAdapter { mFragmentTitleList.add(title); } + public void clear() { + mFragmentList.clear(); + mFragmentTitleList.clear(); + } + @Nullable @Override public CharSequence getPageTitle(int position) { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java index 39e208157..e4f51607a 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/StackFragment.java @@ -4,11 +4,11 @@ import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import java.util.ArrayList; import java.util.List; import butterknife.BindView; @@ -21,20 +21,39 @@ import it.niedermann.nextcloud.deck.ui.card.CardAdapter; public class StackFragment extends Fragment { - SyncManager syncManager = null; - CardAdapter adapter = null; - @BindView(R.id.recycler_view) + private long id = 0; + private CardAdapter adapter = null; + private @BindView(R.id.recycler_view) RecyclerView recyclerView; + + /** + * @see <a href="https://gunhansancar.com/best-practice-to-instantiate-fragments-with-arguments-in-android/">Best Practice to Instantiate Fragments with Arguments in Android</a> + * @param id of the current stack + * @return new fragment instance + */ + public static StackFragment newInstance(long id) { + Bundle bundle = new Bundle(); + bundle.putLong("id", id); + + StackFragment fragment = new StackFragment(); + fragment.setArguments(bundle); + + return fragment; + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view =inflater.inflate(R.layout.fragment_stack, container, false); + View view = inflater.inflate(R.layout.fragment_stack, container, false); ButterKnife.bind(this, view); adapter = new CardAdapter(); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setAdapter(adapter); - syncManager = new SyncManager(getActivity().getApplicationContext(), getActivity()); - syncManager.getCards(0, 0, new IResponseCallback<List<Card>>() { + if(savedInstanceState != null) { + this.id = savedInstanceState.getLong("id"); + } + SyncManager syncManager = new SyncManager(getActivity().getApplicationContext(), getActivity()); + syncManager.getCards(0, id, new IResponseCallback<List<Card>>() { @Override public void onResponse(List<Card> response) { adapter.setCardList(response); @@ -42,7 +61,8 @@ public class StackFragment extends Fragment { @Override public void onError(Throwable throwable) { - + Log.e("Deck", throwable.getMessage()); + throwable.printStackTrace(); } }); return view ; diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8c2b4f023..a490adf89 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -10,7 +10,7 @@ <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".ui.board.MainActivity"> + tools:context=".ui.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" @@ -62,20 +62,8 @@ android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" - android:fitsSystemWindows="false"> - <android.support.v7.widget.LinearLayoutCompat - android:orientation="vertical" - android:layout_height="match_parent" - android:layout_width="match_parent"> - - <include layout="@layout/nav_header_main" /> - - <android.support.v7.widget.RecyclerView - android:id="@+id/recycler_view" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:scrollbars="vertical" /> - </android.support.v7.widget.LinearLayoutCompat> + android:fitsSystemWindows="false" + app:headerLayout="@layout/nav_header_main"> </android.support.design.widget.NavigationView> diff --git a/app/src/main/res/layout/fragment_board_item.xml b/app/src/main/res/layout/fragment_board_item.xml deleted file mode 100644 index 11bf68238..000000000 --- a/app/src/main/res/layout/fragment_board_item.xml +++ /dev/null @@ -1,13 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<android.support.constraint.ConstraintLayout - xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" - android:layout_height="wrap_content" - android:padding="@dimen/mtrl_card_spacing"> - - <TextView - android:id="@+id/card_title" - android:padding="@dimen/mtrl_card_spacing" - android:layout_width="match_parent" - android:layout_height="wrap_content" /> - -</android.support.constraint.ConstraintLayout>
\ No newline at end of file diff --git a/app/src/main/res/layout/fragment_card.xml b/app/src/main/res/layout/fragment_card.xml index 8ae3d04f5..ee99bd957 100644 --- a/app/src/main/res/layout/fragment_card.xml +++ b/app/src/main/res/layout/fragment_card.xml @@ -11,15 +11,15 @@ <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" - android:orientation="vertical"> + android:orientation="vertical" + android:padding="16dp"> <TextView android:id="@+id/card_title" - android:padding="@dimen/mtrl_card_spacing" android:layout_width="match_parent" - android:layout_height="wrap_content" /> + android:layout_height="wrap_content" + android:textSize="18sp" /> <TextView android:id="@+id/card_description" - android:padding="@dimen/mtrl_card_spacing" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> |