summaryrefslogtreecommitdiff
path: root/app/src/main/java/moe/yuuta/flow/MainActivity.java
blob: d827aa6dbaa76312294d2c416b8d46234b619858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package moe.yuuta.flow;

import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import java.util.Arrays;

// You don't need to use AppCompatActivity and AppCompat styles.
public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private FlowFragment mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        Log.i(TAG, "onCreate()");
        if (mFragment == null && savedInstanceState == null) {
            mFragment = new FlowFragment();
            mFragment.setPages(Arrays.asList(new Page1(), new Page2(), new Page3(), new Page4()));
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(android.R.id.content, mFragment)
                    .commitAllowingStateLoss();
            Log.i(TAG, "done");
        }
    }

    public static class Page1 extends PageFragment {
        public Page1() {
            mInfo = new FlowInfo(new HeaderConfig("Page 1", "lol", false), null);
        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            TextView view = new TextView(requireContext());
            view.setText("Page 1");
            return view;
        }
    }

    public static class Page2 extends PageFragment {
        public Page2() {
            mInfo = new FlowInfo(new HeaderConfig("Page 2", "built with love", false), null);
        }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            TextView view = new TextView(requireContext());
            view.setText("Page 2");
            mInfo = new FlowInfo(new HeaderConfig("Page 2 lol", "built with love", false),
                    new NavigationBarConfig("2", "1", View.VISIBLE, View.VISIBLE, View.VISIBLE, getHostFragment().getGeneralFlowNavListener(),
                            getHostFragment().getGeneralFlowNavListener()));
            getHostFragment().notifyCurrentFlowInfoUpdated();
            return view;
        }
    }

    public static class Page3 extends PageFragment {
        public Page3() {
            mInfo = new FlowInfo(new HeaderConfig("Page 3", "zzz~", true), null);
        }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            TextView view = new TextView(requireContext());
            view.setText("Page 3");
            return view;
        }

        @Override
        public boolean onBackPressed() {
            Vibrator vibrator = (Vibrator) requireContext().getSystemService(VIBRATOR_SERVICE);
            vibrator.vibrate(new long[]{50L, 30L}, 1);
            return true;
        }
    }

    public static class Page4 extends PageFragment {
        public Page4() {
            mInfo = new FlowInfo(new HeaderConfig("Page 4", "just a counter", false), null);
        }

        // TODO: The FlowFragment keeps the instance, maybe we don't want that happened to child fragments.
        private int mCount;

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            Button button = new Button(requireContext());
            button.setMinHeight(20);
            button.setText("+1s");
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCount++;
                    HeaderConfig config = mInfo.getHeaderConfig();
                    config.setSubtitleText(Integer.toString(mCount));
                    mInfo.setHeaderConfig(config);
                    getHostFragment().notifyCurrentFlowInfoUpdated();
                }
            });
            return button;
        }
    }

    @Override
    public void onBackPressed() {
        if (mFragment != null && mFragment.onBackPressed()) return;
        super.onBackPressed();
    }
}