ir.isilearning.lmsapp.activity.CategorySelectionActivity.java Source code

Java tutorial

Introduction

Here is the source code for ir.isilearning.lmsapp.activity.CategorySelectionActivity.java

Source

/*
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ir.isilearning.lmsapp.activity;

import android.annotation.SuppressLint;
import android.app.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.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.transition.TransitionInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import ir.isilearning.lmsapp.R;
import ir.isilearning.lmsapp.fragment.CategorySelectionFragment;
import ir.isilearning.lmsapp.helper.ApiLevelHelper;
import ir.isilearning.lmsapp.helper.PreferencesHelper;
import ir.isilearning.lmsapp.model.Player;
import ir.isilearning.lmsapp.persistence.LMSAppDatabaseHelper;
import ir.isilearning.lmsapp.widget.AvatarView;

public class CategorySelectionActivity extends AppCompatActivity {

    private static final String EXTRA_PLAYER = "player";

    public static void start(Activity activity, Player player, ActivityOptionsCompat options) {
        Intent starter = getStartIntent(activity, player);
        ActivityCompat.startActivity(activity, starter, options.toBundle());
    }

    public static void start(Context context, Player player) {
        Intent starter = getStartIntent(context, player);
        context.startActivity(starter);
    }

    @NonNull
    static Intent getStartIntent(Context context, Player player) {
        Intent starter = new Intent(context, CategorySelectionActivity.class);
        starter.putExtra(EXTRA_PLAYER, player);
        return starter;
    }

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

        setContentView(R.layout.activity_category_selection);
        Player player = getIntent().getParcelableExtra(EXTRA_PLAYER);
        if (!PreferencesHelper.isSignedIn(this)) {
            if (player == null) {
                player = PreferencesHelper.getPlayer(this);
            } else {
                PreferencesHelper.writeToPreferences(this, player);
            }
        }
        setUpToolbar(player);
        if (savedInstanceState == null) {
            attachCategoryGridFragment();
        } else {
            setProgressBarVisibility(View.GONE);
        }
        supportPostponeEnterTransition();

    }

    @Override
    protected void onResume() {
        super.onResume();
        //        TextView scoreView = (TextView) findViewById(R.id.score);
        //        final int score = LMSAppDatabaseHelper.getScore(this);
        //        scoreView.setText(getString(R.string.x_points, score));
    }

    private void setUpToolbar(Player player) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_player);
        setSupportActionBar(toolbar);
        //noinspection ConstantConditions
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        final AvatarView avatarView = (AvatarView) toolbar.findViewById(R.id.avatar);
        avatarView.setAvatar(player.getAvatar().getDrawableId());
        //noinspection PrivateResource
        ((TextView) toolbar.findViewById(R.id.contentTitle)).setText(getDisplayName(player));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_category, menu);
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.category_container);
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.sign_out: {
            try {
                signOut();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return true;
        }
        }
        return super.onOptionsItemSelected(item);
    }

    @SuppressLint("NewApi")
    private void signOut() throws InterruptedException {
        PreferencesHelper.signOut(this);
        LMSAppDatabaseHelper.reset(this);
        if (ApiLevelHelper.isAtLeast(Build.VERSION_CODES.LOLLIPOP)) {
            getWindow().setExitTransition(
                    TransitionInflater.from(this).inflateTransition(R.transition.category_enter));
        }
        SignInActivity.start(this, false);
        ActivityCompat.finishAfterTransition(this);
    }

    private String getDisplayName(Player player) {
        return getString(R.string.player_display_name, player.getUserName(), "");
    }

    private void attachCategoryGridFragment() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        Fragment fragment = supportFragmentManager.findFragmentById(R.id.category_container);
        if (!(fragment instanceof CategorySelectionFragment)) {
            fragment = CategorySelectionFragment.newInstance();
        }
        supportFragmentManager.beginTransaction().replace(R.id.category_container, fragment).commit();
        setProgressBarVisibility(View.GONE);

    }

    private void setProgressBarVisibility(int visibility) {
        findViewById(R.id.progress).setVisibility(visibility);
    }

    public void btnAbout_us(MenuItem item) {
        Intent intent = new Intent(this, AboutUsActivity.class);
        startActivity(intent);
    }

    public void click_courseContent(View view) {
        Intent intent = new Intent(this, CourseContentActivity.class);
        startActivity(intent);
    }

    public void click_virtualClasses(View view) {
        Intent intent = new Intent(this, VirtualClassActivity.class);
        startActivity(intent);
    }
}