org.akop.crosswords.activity.SubscriptionActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.akop.crosswords.activity.SubscriptionActivity.java

Source

// Copyright (c) 2014-2015 Akop Karapetyan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package org.akop.crosswords.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;

import org.akop.crosswords.R;
import org.akop.crosswords.fragment.DatePickerFragment;
import org.akop.crosswords.fragment.SubscriptionDetailFragment;
import org.akop.crosswords.fragment.SubscriptionFragment;
import org.akop.crosswords.model.PuzzleSource;
import org.joda.time.DateTime;

public class SubscriptionActivity extends BaseActivity implements DatePickerFragment.OnDateSelectedListener {
    public interface TitledFragment {
        String getTitle();

        String getSubtitle();
    }

    private String mTitle;
    private String mSubtitle;

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

        FragmentManager fm = getSupportFragmentManager();
        if (savedInstanceState == null) {
            fm.beginTransaction().add(R.id.container, new SubscriptionFragment()).commit();
        } else {
            mTitle = savedInstanceState.getString("title");
            mSubtitle = savedInstanceState.getString("subtitle");
        }

        if (mTitle != null) {
            updateActionBar();
        }

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                FragmentManager fm = getSupportFragmentManager();
                Fragment fragment = fm.findFragmentById(R.id.container);

                if (fragment instanceof TitledFragment) {
                    TitledFragment tf = (TitledFragment) fragment;
                    mTitle = tf.getTitle();
                    mSubtitle = tf.getSubtitle();

                    updateActionBar();
                }
            }
        });
    }

    @Override
    protected int getContentViewResource() {
        return R.layout.activity_subscription;
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putString("title", mTitle);
        outState.putString("subtitle", mSubtitle);
    }

    @Override
    public void onDateSelected(DateTime dateTime) {
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.container);

        if (fragment instanceof DatePickerFragment.OnDateSelectedListener) {
            DatePickerFragment.OnDateSelectedListener dsl = (DatePickerFragment.OnDateSelectedListener) fragment;
            dsl.onDateSelected(dateTime);
        }
    }

    private void updateActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle(mTitle);
            actionBar.setSubtitle(mSubtitle);
        }
    }

    public void showSubscriptionDetails(PuzzleSource source) {
        Fragment fragment = new SubscriptionDetailFragment();
        fragment.setArguments(SubscriptionDetailFragment.createArgs(source));

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        ft.setCustomAnimations(R.anim.slide_in_from_right, R.anim.slide_out_to_left, R.anim.slide_in_from_left,
                R.anim.slide_out_to_right);

        ft.replace(R.id.container, fragment);
        ft.addToBackStack(null);
        ft.commit();
    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, SubscriptionActivity.class);
        context.startActivity(intent);
    }
}