Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / view / book / BookActivity.java
1 package com.moiseum.wolnelektury.view.book;
2
3 import android.content.Context;
4 import android.os.Bundle;
5
6 import com.moiseum.wolnelektury.R;
7 import com.moiseum.wolnelektury.base.AbstractActivity;
8 import com.moiseum.wolnelektury.base.AbstractIntent;
9
10 /**
11  * Created by Piotr Ostrowski on 17.11.2017.
12  */
13
14 public class BookActivity extends AbstractActivity {
15
16         private static final String BOOK_FRAGMENT_TAG = "BookFragmentTag";
17         static final String BOOK_SLUG_KEY = "BookSlugKey";
18         static final String BOOK_TYPE_KEY = "BookTypeKey";
19
20         public static class BookIntent extends AbstractIntent {
21
22                 public BookIntent(String slug, BookType type, Context context) {
23                         super(context, BookActivity.class);
24                         putExtra(BOOK_SLUG_KEY, slug);
25                         putExtra(BOOK_TYPE_KEY, type.name());
26                 }
27         }
28
29         @Override
30         public int getLayoutResourceId() {
31                 return R.layout.activity_blank;
32         }
33
34         @Override
35         public void prepareView(Bundle savedInstanceState) {
36                 setTitle("");
37                 if (!getIntent().hasExtra(BOOK_SLUG_KEY)) {
38                         throw new IllegalStateException("Missing either slug or full ebook model.");
39                 }
40                 if (!getIntent().hasExtra(BOOK_TYPE_KEY)) {
41                         throw new IllegalStateException("Missing book type.");
42                 }
43
44                 String bookSlug = getIntent().getStringExtra(BOOK_SLUG_KEY);
45                 BookType type = BookType.valueOf(getIntent().getStringExtra(BOOK_TYPE_KEY));
46
47                 BookFragment bookFragment = (BookFragment) getSupportFragmentManager().findFragmentByTag(BOOK_FRAGMENT_TAG);
48                 if (bookFragment == null) {
49                         bookFragment = BookFragment.newInstance(bookSlug, type);
50                         getSupportFragmentManager().beginTransaction().add(R.id.flContainer, bookFragment, BOOK_FRAGMENT_TAG).commit();
51                 }
52         }
53 }