mirror of
https://github.com/gsantner/dandelion
synced 2025-12-15 00:31:11 +01:00
Migrate to opoc/basefragment; Fix redundant casts
This commit is contained in:
parent
a88dc5d13c
commit
08242760bb
15 changed files with 176 additions and 109 deletions
|
|
@ -1,59 +0,0 @@
|
|||
/*
|
||||
This file is part of the dandelion*.
|
||||
|
||||
dandelion* 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.
|
||||
|
||||
dandelion* 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 the dandelion*.
|
||||
|
||||
If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.gsantner.opoc.activity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
|
||||
/**
|
||||
* Customized abstract Fragment class with some useful methods
|
||||
* Created by vanitas on 21.09.16.
|
||||
*/
|
||||
|
||||
public abstract class CustomFragment extends Fragment {
|
||||
|
||||
public static final String TAG = "net.gsantner.opoc.activity.CustomFragment";
|
||||
|
||||
/**
|
||||
* We have an optionsMenu
|
||||
*
|
||||
* @param savedInstanceState state
|
||||
*/
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the tag used to identify the Fragment.
|
||||
*
|
||||
* @return tag
|
||||
*/
|
||||
public abstract String getFragmentTag();
|
||||
|
||||
/**
|
||||
* Return true if the fragment reacted to a back button press, false else.
|
||||
* In case the fragment returned false, the parent activity should handle the backPress.
|
||||
*
|
||||
* @return did we react to the back press?
|
||||
*/
|
||||
public abstract boolean onBackPressed();
|
||||
}
|
||||
|
||||
129
app/src/main/java/net/gsantner/opoc/activity/GsFragmentBase.java
Normal file
129
app/src/main/java/net/gsantner/opoc/activity/GsFragmentBase.java
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
/*#######################################################
|
||||
*
|
||||
* Maintained by Gregor Santner, 2017-
|
||||
* https://gsantner.net/
|
||||
*
|
||||
* License: Apache 2.0
|
||||
* https://github.com/gsantner/opoc/#licensing
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
#########################################################*/
|
||||
package net.gsantner.opoc.activity;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.LayoutRes;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import net.gsantner.opoc.util.ContextUtils;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
/**
|
||||
* A common base fragment to extend from
|
||||
*/
|
||||
public abstract class GsFragmentBase extends Fragment {
|
||||
private boolean _fragmentFirstTimeVisible = true;
|
||||
private final Object _fragmentFirstTimeVisibleSync = new Object();
|
||||
|
||||
protected ContextUtils _cu;
|
||||
protected Bundle _savedInstanceState = null;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inflate the fragments layout. Don't override this method, just supply the needed
|
||||
* {@link LayoutRes} via abstract method {@link #getLayoutResId()}, super does the rest
|
||||
*/
|
||||
@Deprecated
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
_cu = new ContextUtils(inflater.getContext());
|
||||
_cu.setAppLanguage(getAppLanguage());
|
||||
_savedInstanceState = savedInstanceState;
|
||||
View view = inflater.inflate(getLayoutResId(), container, false);
|
||||
ButterKnife.bind(this, view);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
view.postDelayed(() -> {
|
||||
synchronized (_fragmentFirstTimeVisibleSync) {
|
||||
if (getUserVisibleHint() && isVisible() && _fragmentFirstTimeVisible) {
|
||||
_fragmentFirstTimeVisible = false;
|
||||
onFragmentFirstTimeVisible();
|
||||
}
|
||||
}
|
||||
}, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a tag from the fragment, allows faster distinction
|
||||
*
|
||||
* @return This fragments tag
|
||||
*/
|
||||
public abstract String getFragmentTag();
|
||||
|
||||
|
||||
/**
|
||||
* Get the layout to be inflated in the fragment
|
||||
*
|
||||
* @return Layout resource id
|
||||
*/
|
||||
@LayoutRes
|
||||
protected abstract int getLayoutResId();
|
||||
|
||||
/**
|
||||
* Event to be called when the back button was pressed
|
||||
* True should be returned when this was handled by the fragment
|
||||
* and no further handling in the view hierarchy is needed
|
||||
*
|
||||
* @return True if back handled by fragment
|
||||
*/
|
||||
public boolean onBackPressed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the language to be used in this fragment
|
||||
* Defaults to resolve the language from sharedpreferences: pref_key__language
|
||||
*
|
||||
* @return Empty string for system language, or an android locale code
|
||||
*/
|
||||
public String getAppLanguage() {
|
||||
if (getContext() != null) {
|
||||
return getContext().getSharedPreferences("app", Context.MODE_PRIVATE)
|
||||
.getString("pref_key__language", "");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* This will be called when this fragment gets the first time visible
|
||||
*/
|
||||
public void onFragmentFirstTimeVisible() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUserVisibleHint(boolean isVisibleToUser) {
|
||||
super.setUserVisibleHint(isVisibleToUser);
|
||||
synchronized (_fragmentFirstTimeVisibleSync) {
|
||||
if (isVisibleToUser && _fragmentFirstTimeVisible) {
|
||||
_fragmentFirstTimeVisible = false;
|
||||
onFragmentFirstTimeVisible();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue